Solution for Caching issue if Lambda Expression used with Glass Mapper Models

Hello Sitecore Family,

Recently, we were doing a POC of upgrading a Sitecore solution from Sitecore 7.0 to Sitecore 8.0 and while moving there (as in, not InPlace upgrade but by Migrating to new instance), we were doing a number of foundational tasks – like, upgrading  the .Net framework to 4.5, upgrading ASP.NET version to 5.1, upgrading Glass Mapper to 4.0.5.54 and some others.

Challenge:

Now this solution we have is a multisite Sitecore solution and after upgrading a pretty basic website project with the above mentioned assemblies, we found that a couple of locations were showing images from some other site! weird.

We later found, that whichever site was loaded first, that image got loaded and that image was shown on other sites of the similar structure.

CACHING ISSUE! — Oh yes!

Oh but it was a simple Glass Mapper Html Helper, how can it lead us to caching issue?

@Html.Raw(GlassHtml.RenderImage(Model, x => Model.Logo, null, true))

With all these questions, I contacted Mike Edwards. — Oh well, I first contacted Mark Stiles by mistake and explained him every bit of the issue I faced — Bummer!

Solution:

Firstly, thanks to Mike Edwards for being approachable on Slack chat and giving a solution to the problem that we had. Glass Mapper is really well documented, and that’s where the solution was:

http://www.glass.lu/Mapper/Sc/Documentation/GlassHtml

Quoting Mike Edwards here:

If you have written your lambda expression with reference to the Model as you have in your example you will need to turn off the lambda cache.

That’s what we did and bingo, the error was solved!

What we did?

We added the following code in the method called CreateResolver in the class GlassMapperScCustom in App_Start folder:

config.UseGlassHtmlLambdaCache = false;

A small suggestion Mike,  documentation is great but search is good to have on Glass Mapper site. 🙂

Happy Solving Caching Issues! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s