SaaS CMS has officially launched! Learn more now.

Thomas Krantz
Apr 6, 2012
  5184
(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
Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024