Glass Mapper V4 – Caching

Following on in my series looking at the new features in Glass Mapper v4.

One of my favourite features of Glass V4 is the new Caching feature. This at at in its simplest form allows you to cache the object that Glass produces, much like we do in regular .net development for slower running processes. In this post I will describe some more about caching, how to set it up and what it does.

Glass is amazing at doing what it does – making Sitecore easier to work with and allowing us to work in a more ‘.net’ way. In my solutions in truth, I rarely even refer to Sitecore in code. I would even go so far as to say that – with Glass involved, I have never had a need for fakeDb (not to say it’s not a great project) and very rarely need to get involved with anything directly involved with the Sitecore API where data access is concerned. Glass’ performance also has been great through the V3 lifecycle and is even better in V4 (ranging somewhere between 80% and 130% of the speed of raw Sitecore depending on what you are trying to achieve with it). I believe it compares pretty well and wouldn’t be surprised if it is one of the faster of the current offerings for Sitecore ORM’s.

That said, there are always going to be occasions where the whole: Get Item From Sitecore > Map To Poco > Get results from a query > map them would seem to be a little on the unnecessary side, this is particularly the case where html caching is not possible – especially when you have things like forms on the page. For this purpose (and many others) Glass introduced a caching mechanism to return your object directly.


Using attribute based configuration, we can mark a class / interface as cachable by adding it to the SitecoreType attribute on the object.

    [SitecoreType(Cachable = true)]
    public interface ISitecoreItem
        string Name { get; set; }

        string Url { get; set; }

        ID Id { get; set; }

Similarly, just using .Cachable() in the fluent configuration api will set up that type as being cachable.

Inherited types

If for example (like me) you use inheritance on your types – like so:

    public interface ISuperSitecoreItem : ISitecoreItem
        string Name { get; set; }

        string Url { get; set; }

        ID Id { get; set; }

It is worth noting that the ISuperSitecoreItem WILL NOT be pulled from the cache even though its inherited from a cacheable interface. Glass does this by storing the requested type as part of the cache key.

This will of course mean that – since it is an inclusive policy, you will have to explicitly mark all classes / interfaces as cachable where you require the feature.

Using your own cache provider

Glass allows the changing of cache managers through the registration api. You will have to write a class that inherits from Glass’ ICacheManager. This is a more in depth topic and I will shortly write a post specifically on overriding the cache manager to use an alternative cache provider.

Caching in the page editor / preview

Glass by design avoids caching unless the site is running in the normal page mode. This should mean that under normal circumstances, you will not get objects back from the cache during edit / preview modes.

Cache Clearing

Glass’ cache is cleared on any publish, this should mean that new content is always respected.

3 thoughts on “Glass Mapper V4 – Caching

  1. Pingback: Glass Mapper V4 – Redis Cache Provider | CardinalCore

  2. Great post.
    I usually rely on the TDS code generation to generate the classes. Do you know if there is a way to configure some of the templates in the TDS project to be cachable?

  3. Remal

    You can update the TDS template to make use of the Custom Data property of an item. Select a TDS a template item in Visual Studio and press F4 to bring up the property window and you should see the field.

    If you look at the T4 template you will be able to see how the Custom Data field is used to control the generation of properties. Just follow a similar pattern for your template attribute.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s