Try our conversational search powered by Generative AI!

Thomas Krantz
Apr 6, 2012
  5176
(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
Integrating HubSpot CRM without the MA Connector

Have HubSpot CRM? Want to push user data into it from Optimizely? Don’t have any personalisation requirements with that data? Don’t want to pay $80...

Matt Pallatt | Jun 27, 2024

Keeping the website secure by updating external packages

Did you see the latest warning from Optimizely to update this package with a critical security warning? https://world.optimizely.com/documentation/...

Daniel Ovaska | Jun 27, 2024

Optimizely CMS image anonymization now available for Linux!

The famous image anonymization add-on for Optimizely CMS, with at least 5 downloads, is now finally available for use on Linux. Supports simultaneo...

Tomas Hensrud Gulla | Jun 25, 2024 | Syndicated blog

Remove a segment from the URL in CMS 12

Problem : I have created thousands of pages dynamically using schedule jobs with different templates (e.g. one column, two columns, etc..) and stor...

Sanjay Kumar | Jun 21, 2024