Blog

Cache Dependencies in SharePoint


by Tobias Lekman, 04 December, 2008

While writing a data layer based on data stored in a SharePoint list, I implemented the collection as a static singleton that forced refreshes of the data every 15 minutes.

A colleague was doing something similar in a roll-up web part and we started discussing Cache Dependency objects used for the HttpContext. Could this work with SharePoint objects?

No, says "The Kid". But he gave me a good idea.

Therefore, I implemented the SPListItemCacheDependency and SPListCacheDependency objects.

To use the SPListItemCacheDependency:

 1: HttpContext.Current.Cache.Add("YourKey",

 2:     dataToCache,

 3: new SPListItemCacheDependency(sourceItem),

 4:     Cache.NoAbsoluteExpiration,

 5:     Cache.NoSlidingExpiration,

 6:     CacheItemPriority.Normal,

 7: null);


To use the SPListCacheDependency:



 1: HttpContext.Current.Cache.Add("YourKey",

 2:     dataToCache,

 3: new SPListCacheDependency(sourceList),

 4:     Cache.NoAbsoluteExpiration,

 5:     Cache.NoSlidingExpiration,

 6:     CacheItemPriority.Normal,

 7: null);


To monitor schema changes and property changes as well within the SPList and to decrease the change check poll from 10 seconds to 1 minute:



 1: HttpContext.Current.Cache.Add("YourKey",

 2:     dataToCache,

 3: new SPListCacheDependency(sourceList, true, true, 60000),

 4:     Cache.NoAbsoluteExpiration,

 5:     Cache.NoSlidingExpiration,

 6:     CacheItemPriority.Normal,

 7: null);


Download the code and documentation.