Sorry – it’s been a while. The school holidays happened! I thought I’d ease myself back in to the blogging with some changes that Sitecore have introduced with Sitecore 8.2 (specifically some changes to the Caching stuff).
We’ve got some helper methods which handle adding and removing stuff from the Sitecore Cache (or optionally A N Other cache).
All was working fine and dandy until the Sitecore 8.2 upgrade. Here’s what we had to change (other changes may be necessary for your solution).
Change 1: Interfaces! 🙂
private Sitecore.Caching.Cache SitecoreCache(bool isGlobal = false, string siteName = "website", string databaseName = "master")
private ICache SitecoreCache(bool isGlobal = false, string siteName = "website", string databaseName = "master")
Hooray! Sitecore have started using Interfaces. So their GetNamedInstance method has started returning that Interface instead of a concrete Cache item. Happy days.
But wait! Where has GetNamedInstance gone?
Change 2: CacheManager not Cache
cache = Sitecore.Caching.Cache.GetNamedInstance(cacheKey, Sitecore.StringUtil.ParseSizeString(Sitecore.Configuration.Settings.GetSetting("Caching.CacheSize", "100MB")));
cache = Sitecore.Caching.CacheManager.GetNamedInstance(cacheKey, Sitecore.StringUtil.ParseSizeString(Sitecore.Configuration.Settings.GetSetting("Caching.CacheSize", "100MB")), true);
This seems to work – although I’ll be doing a spot more testing before we go to production 😉 Notice the change to the class referenced and also the additional “true” this is to say that we want to create the cache if it doesn’t exist.
Change 3: No Cache Size on Add
This line no longer appears to work:
cache.Add(key.ToLower(), cachingData, cacheSize, slidingCache, absoluteCache);
It’s because there’s no overload with the cacheSize in any more – I’m guessing that Sitecore is handling this differently now but I’ve made a note for further investigation. So that line becomes:
cache.Add(key.ToLower(), cachingData, slidingCache, absoluteCache);
Change 4: GetCacheKeys() Returns Strings
foreach (object key in cache.GetCacheKeys())
Can now become this:
foreach (string key in cache.GetCacheKeys())
*Opens up the floor for an argument about using “var”*
*Agrees with you*
*Blames code copied from the Internet*
*Goes about his day*
Oh – also make sure you’ve got all your projects set to .Net 4.5.2 at least. And don’t patch in a config with this line:
<database id="master" singleInstance="true" type="Sitecore.Data.Database, Sitecore.Kernel">
Because it’s changed to this:
<database id="master" singleInstance="true" type="Sitecore.Data.DefaultDatabase, Sitecore.Kernel">
Use this instead (like the clever people who created Unicorn do):