Try our conversational search powered by Generative AI!

Thomas Krantz
Apr 6, 2012
  5134
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog