1

Base class for a service providing data for an API controller on a 8.2 update-4 instance.

Code's context is the web database and it's a simple case of using content search to find an item based on a query of its fields and then using the context to get the item with its Glass interface. (Context being ISitecoreContext injected into the contructor)

var doc = result.Document;
var model = Context.GetItem<TGlass>(doc.ItemId.Guid);

All standard Sitecore caching is active and the Glass interface has caching enabled on its type attribute.

Nothing is cached when the code runs. Checked Sitecore.Context.Site.Properties["glassCache"] and by manually updating the web db item with small changes which are immediately shown upon refresh.

Is there anything obvious that I am missing?

[SitecoreType(Cachable = true, TemplateId = Templates.Tip.ID, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public interface ITip: IApiDataItem
{
    [SitecoreField(FieldId = Templates.Tip.Fields.Title)]
    string Title { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Summary)]
    string Summary { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Overview, Setting = SitecoreFieldSettings.RichTextRaw)]
    string Overview { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Highlight)]
    string Highlight { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Why)]
    string Why { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Body, Setting = SitecoreFieldSettings.RichTextRaw)]
    string Body { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Category)]
    Taxonomy.Models.ICategoryItem Category { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Type)]
    Taxonomy.Models.ICategoryItem Type { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.Image)]
    Media.Models.IFrontEndImage Image { get; set; }

    [SitecoreField(FieldId = Templates.Tip.Fields.AssociatedAnswers)]
    IEnumerable<IAnswer> AssociatedAnswers { get; set; }
}
2

Looking at Glass 4.4 the DI resolver, by default, uses the NetMemoryCacheManager to handle caching (see Glass.Mapper.Sc.IoC.DependencyResolver).

This in turn uses a System.Runtime.Caching.MemoryCache object that is initialised with the name Glass.Mapper. If you want to view the contents of this cache you will need to interrogate it directly as follows:

var cache = new MemoryCache("Glass.Mapper", null);
var count = cache.GetCount( null );

Entries are added to the cache using a sliding window expiry of 1200 seconds (though it is possible to use an Absolute expiry with additional configuration). The key for each is generated using a many contextual elements by the Glass.Mapper.Sc.Caching.CacheKeyGenerator::Generate(). It's hard to replicate the key (which is deterministic) because elements of context like Item Language and Revision are included in the calculation. I've included the calculation below for reference if you want to give it a go.

Long and the short of it is that it probably is caching, and you're just looking in the wrong spot for your evidence.

Glass.Mapper.Sc.Caching.CacheKeyGenerator::Generate() [Glass.Mapper.Sc, Version 4.4.0.199]

...
object[] name = new object[] { context.SitecoreService.GlassContext.Name, null, null, null, null, null, null, null };
name[1] = (Context.Site == null ? string.Empty : Context.Site.Name);
name[2] = context.Item.ID;
name[3] = context.Item["__Revision"];
name[4] = context.Item.Language.Name;
name[5] = context.Item.Database.Name;
name[6] = context.RequestedType.FullName;
name[7] = context.IsLazy;
return string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.