5

Ran into an odd caching issue with glass mapper (v. 4.2.1.188). We have 2 languages in our site (Chinese and English). Here's the scenario (could replicate after every iisreset):

  1. Load the page in Chinese - all content displays correctly (in Chinese)
  2. Switch the page to English - content seems to be 'stuck' in Chinese.

The code is pretty straight-forward - in a controller I just get the current item:

ICustomPageGlass currentPage = GetContextItem<ICustomPageGlass>(inferType: true);

When switching from Chinese to English Sitecore.Context.Language is correct ("en"), but currentPage.__Language is actually wrong (Chinese).

This works fine when I wrap my code in using (new DisableCache()) { ... } block.

  • Does your ICustomPageGlass has the [SitecoreType(Cachable = true)] attribute? – Artsem Prashkovich Jun 13 '17 at 19:49
  • CustomPageGlass does have it: [SitecoreType(TemplateId = "e95c6068-db3e-4ead-9605-fe0a7965a119", AutoMap = true, Cachable = true)] – alexp83 Jun 13 '17 at 20:55
8

If Cachable attribute equals true, it means that Glass Mapper will apply cache for this object. I've decompiled the Glass.Mapper.Sc.dll, where CacheKeyGenerator is defined and we can see that it doesn't consider the language in the key for cache:

namespace Glass.Mapper.Sc.Caching
{
  public class CacheKeyGenerator : ICacheKeyGenerator
  {
    public string Generate(ObjectConstructionArgs args)
    {
      SitecoreTypeCreationContext typeCreationContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;
      return string.Format("{0}{1}{2}{3}{4}{5}{6}", (object) typeCreationContext.SitecoreService.GlassContext.Name, Sitecore.Context.Site == null ? (object) string.Empty : (object) Sitecore.Context.Site.Name, (object) typeCreationContext.Item.ID, (object) typeCreationContext.Item["__Revision"], (object) typeCreationContext.Item.Database.Name, (object) typeCreationContext.RequestedType.FullName, (object) typeCreationContext.IsLazy);
    }
  }
}

It means the key for each language will be the same.

I guess if you set Cachable = false, your issue will be solved.

The other way is to override the CacheManager and add contextual language to the key.

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.