Anders Hattestad
May 25, 2012
  4248
(2 votes)

Cache Dynamic Data Store items

When I use DDS I always use a pattern where I have a public static Items implementation that I can do my query against. This logic is placed inside a common class all my DDS tables inherit from

Code Snippet
  1. public class BaseData<T> : IDynamicData, ISaveMe where T : BaseData<T>, new()
  2. {
  3.     public EPiServer.Data.Identity Id { get; set; }
  4.     public static IOrderedQueryable<T> Items
  5.     {
  6.         get
  7.         {
  8.             return Store.Items<T>();
  9.  
  10.         }
  11.     }

So when I needed to speed things up a bit I changed my Items implementation to instead return a memory list with all my items,

Code Snippet
  1. public static IQueryable<T> Items
  2. {
  3.     get
  4.     {
  5.         if (Cache())
  6.             return ItemsMemory;
  7.  
  8.         return Store.Items<T>();
  9.  
  10.     }
  11. }
  12.  
  13. public static bool Cache()
  14. {
  15.     string tmp = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Cache_"+typeof(T).Name);
  16.     if (!string.IsNullOrEmpty(tmp))
  17.         return true;
  18.     return false;
  19. }
  20.  
  21. static IQueryable<T> ItemsMemory
  22. {
  23.     get
  24.     {
  25.         if (_itemsMemory == null)
  26.         {
  27.             WriteNewCacheData();
  28.         }
  29.         return _itemsMemory.AsQueryable();
  30.     }
  31. }
  32. static List<T> _itemsMemory = null;
  33. static object lockObject = new object();
  34. static DateTime _lastCleard = DateTime.MinValue;
  35. public static void WriteNewCacheData()
  36. {
  37.     if (Cache())
  38.     {
  39.         WriteNewCacheData((from item2 in Store.Items<T>() select item2).ToList());
  40.     }
  41. }
  42. static void WriteNewCacheData(List<T> data)
  43. {
  44.     lock (lockObject)
  45.     {
  46.         _lastCleard = DateTime.Now;
  47.         _itemsMemory = data;
  48.     }
  49. }

Since all my DDS classes inherits from the same base, I made my change so I could turn on memory cache using appsettings.

When I retrieve a object and want to make a change to I just make the change and save it using my LazyDDSSave class. Even before the item is saved all new querys will access the changed object since it’s the same object. One could make a CreateWritebleClone implementation update the cache when one have done the save, but I didn’t.

Its only when we create a new object or delete a object we need to change the number of elements in the memory cache.

Code Snippet
  1. public virtual void Save()
  2. {
  3.     LazyDSSSave.Current.AddToSave(this);
  4. }
  5. public static void Save(T item)
  6. {
  7.     LazyDSSSave.Current.AddToSave(item);
  8. }
  9. public static void Delete(T item)
  10. {
  11.     Store.Delete(item);
  12.     if (Cache())
  13.     {
  14.         WriteNewCacheData((from item2 in Store.Items<T>() select item2).ToList());
  15.     }
  16. }
  17. public void SaveMe()
  18. {
  19.     Store.Save(this);
  20.     if (Cache())
  21.     {
  22.         var list = (_itemsMemory as List<T>);
  23.         if (list != null)
  24.             if (list.IndexOf(this as T) == -1)
  25.             {
  26.                 WriteNewCacheData((from item in Store.Items<T>() select item).ToList());
  27.             }
  28.     }
  29. }

I have selected a full reread from the data store when I delete or add a new item, but this could also be changed to just add or delete the object from the memory list.

If you are in a enterprise load balance server situation  one could either implement a event based reload or one could reload the memory list after a fixed amount of time.

I gain a lot performance by just using my implementation above in a project I worked on. I had many updates on objects, and not very many delete or new ones.

Since I then cache all my DDS tables (or most of them) I don’t need to cache my results from query's against my DDS tables So when updates are done I don’t need to invalidate my aggregate cache. That saves me a lot of worries Smile.

Have uploaded the base class in the code section here

May 25, 2012

Comments

May 28, 2012 08:12 AM

I have a similar setup when working with DDS.
Great inspiration how to improve it - Thanks!

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024