Try our conversational search powered by Generative AI!

Thomas Krantz
Apr 6, 2012
  5150
(0 votes)

Using a bit of System.Runtime.Caching with EPiServer

I have been poking around in System.Runtime.Caching that was introduced with .NET 4, and more specifically the MemoryCache.

The MemoryCache is virtually the same as the good old ASP.NET Cache, except it’s not dependent on System.Web which means you can use it without an HttpContext.

ChangeMonitors monitors changes in the state of data which a cache item depends on, and I’ve written a simple custom ChangeMonitor called PageChangeMonitor to monitor published EPiServer pages.

public class PageChangeMonitor : ChangeMonitor
    {
        private PageReference _pageLink;
 
        public PageChangeMonitor(PageReference pageLink)
        {
            if(PageReference.IsNullOrEmpty(pageLink))
            {
                throw new ArgumentNullException("pageLink");
            }   
 
            bool init = false;
            try
            {
                _pageLink = pageLink;
                DataFactory.Instance.PublishedPage += PublishedPage;
 
                init = true;
            }
            finally
            {
                base.InitializationComplete();
                if(!init)
                {
                    base.Dispose();
                }
            }
        }
 
        void PublishedPage(object sender, PageEventArgs e)
        {
            if(e.PageLink.ID  == _pageLink.ID)
            {
                OnChanged(e.PageLink);
            }
        }
 
        protected override void Dispose(bool disposing)
        {
            DataFactory.Instance.PublishedPage -= PublishedPage;
        }
 
        public override string UniqueId
        {
            get { return Guid.NewGuid().ToString(); }
        }

The PageChangeMonitor can be used to expire the cache item when a certain page is published. An example:

public string GetSomeDataForPage(PageData page)
       {
           var cacheKey = string.Format("some-data-{0}", page.PageLink.ID);
 
           var cache = MemoryCache.Default;
           var someData = (string) cache.Get(cacheKey);
           if(someData != null)
           {
               // data was cached
               return someData;
           }
 
           // data was not in cache. Either it has never been cached,
           // or the PageChangeMonitor expired the cache when the page 
           // was published.
           someData = DoSomeHeavyLiftingAndReturnData(page);
 
           var policy = new CacheItemPolicy();
 
           // create the PageChangeMonitor that should monitor the page
           var monitor = new PageChangeMonitor(page.PageLink);
           policy.ChangeMonitors.Add(monitor);
 
           cache.Add(cacheKey, someData, policy);
 
           return someData;
       }

Enjoy! And remember – cache is king.

Apr 06, 2012

Comments

valdis
valdis Apr 11, 2012 04:47 PM

Cool! Is first code snippet complete?

Thomas Krantz
Thomas Krantz Apr 12, 2012 11:44 PM

Not entirely.. missing some using-statements and a finishing } bracket it seems like. But it should work.

Please login to comment.
Latest blogs
Do not upgrade to EPiServer.CMS.Core 12.21.4 without reading this!

Todays update of EPiServer.CMS.Core 12.21.4 alters default sort order in an unexpected way, when you are working with muligple languages and have...

Tomas Hensrud Gulla | May 14, 2024 | Syndicated blog

Upgrade Optimizely CMS From v11 to v12

Why Upgrade? There are many improvements implemented in version 12, but most importantly is that the whole framework is migrated from .Net Framewor...

MilosR | May 13, 2024

Configured Commerce - Infrastructure Updates Ahoy!

I'm very happy to share an important milestone - we no longer have any customers in our legacy v1 environment!  This means that the Configured...

John McCarroll | May 10, 2024

A day in the life of an Optimizely Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024