Deep from the mines of Glass.Mapper.Sc I have been working on a new feature to allow Glass implementations to take more advantage of the internal caching mechanisms. This feature is called Always On, and as the name suggests this feature allows the Glass.Mapper.Sc cache to always be enabled.

"So what?" you might be asking, "Couldn't we do this before?".

The answer is yes but only with lazy loading had to be disabled. This meant that you couldn't use caches with lazy loading. It also means that complex object models needed to be load as soon as the root model was requested rather than being deferred to a later point in time.

Hare are list of key points about the changes made to support this feature:

  • Always On will allow you to cache models and still support lazy loading. This was not possible in previous versions which created problems for recursive models.
  • In V4 the SitecoreContext contained methods for accessing the current context but also inherited the SitecoreService. This meant that the SitecoreContext had multiple responsibilities, i.e. context and data access. Therefore the lifestyle of the SitecoreContext need to be per request which would cause problems when lazy loading.
  • Separation of context classes, i.e. MVCContext, RequestContext, from the SitecoreService allows for the introduction of always on caching.
  • URL generation needed to be update so that the correct site is used when lazy loading cached models and generating URLs.

Always on should allow you to use caching on all your models going forward. Currently the changes are in the develop branch and can be downloaded as part of the nightly build.

If you want to try out Always On download the nightly build nuget packages and then add the following line to your Glass configuration:

    public static  class GlassMapperScCustom
    {
		public static IDependencyResolver CreateResolver(){
			var config = new Glass.Mapper.Sc.Config();
            config.Cache.AlwaysOn = true;

			var dependencyResolver = new DependencyResolver(config);
			// add any changes to the standard resolver here
			return dependencyResolver;
		}

The advantages of caching being on is of course performance. By taking advantage of the Glass cache your pages will render much faster. It is also worth remembering that cached models are reused regardless of the page being requested. So once you have built the models for your navigation they will be reused over and over again.

- Mike

Glass needs your support!

Glass.Mappper.Sc is supported by the generous donations of the Glass.Mapper.Sc community! Their donations help fund the time, effort and hosting for the project.

These supporters are AMAZING and a huge thank you to all of you. You can find our list of supporters on the Rockstars page.

If you use Glass.Mapper.Sc and find it useful please consider supporting the project. Not only will you help the project but you could also get a discount on the Glass.Mapper.Sc training and join the Rockstars page.

Become a Rockstart