Sujit Senapati
Oct 28, 2024
  144
(2 votes)

Mastering clearing Cache in Optimizely CMS with ISynchronizedObjectInstanceCache & MemoryCache

In the fast-paced world of web development, efficient memory management is essential for optimizing application performance and enhancing user experience. Among the many optimization techniques available, clearing memory cache using a cache key is a fundamental approach. When storing objects in cache, developers often set an expiration policy, such as an absolute or sliding cache eviction policy, to control how long the data remains. While cache clearing is manageable in lower CMS environments, production environments can be challenging, especially when sliding cache policies make it nearly impossible to expire cached items in real time.

In Optimizely, most cache management is handled by ISynchronizedObjectInstanceCache. However, Optimizely doesn’t offer a straightforward method to retrieve all cache items in memory.

Understanding Memory Cache and Cache Keys

  • Memory Cache: This is where your web application stores data that can be accessed more quickly than if it were stored in a database or on a disk. It's a portion of RAM reserved for temporarily holding data.
  • Cache Key: This is a unique identifier used to store and retrieve specific data from the cache. Using cache keys ensures you're managing the right data.

Why Clear the Cache?

  • Performance: Over time, the cache can become cluttered with outdated data, slowing down access speeds.
  • Data Integrity: Clearing cache ensures your application uses the latest data, vital for dynamic content.
  • Memory Management: Freeing up memory can prevent memory leaks and improve overall performance.

Implementation Steps in Optimizely CMS

Step 1: Identify Your Cache System

First, identify the caching system your application uses. In Optimizely CMS, the common choice is ISynchronizedObjectInstanceCache, which relies on MemoryCache internally.

Step 2: Determine the Cache Key

  • Static Keys: For fixed data, like user profile information, a static key pattern like "user_" + userID can be effective. In Optimizely CMS and Commerce, most caches utilize static keys, making cache management straightforward.
  • Dynamic Keys: For frequently changing data (e.g., session-based), keys might incorporate timestamps or session IDs.

Step 3: Clearing the Cache

With ISynchronizedObjectInstanceCache, you can only clear individual items by specifying their cache key. However, if dynamic values such as user IDs, class names, content IDs, or language IDs are used, determining the exact key can be challenging.

To overcome this, you can retrieve the private property containing all cached items in MemoryCache using BindingFlags. This allows you to access cached entries, making it possible to locate and manage items based on a keyword search within the cache keys.

Here’s an example approach to find all cache keys matching a keyword (e.g., "cacheKey") and then remove them from the cache using ISynchronizedObjectInstanceCache.

var field = typeof(MemoryCache).GetProperty("StringKeyEntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);
//_memoryCache is an instance of IMemoryCache
var collection = field?.GetValue(_memoryCache) as ICollection;
var items = new List<string>();
if (collection != null)
{
	foreach (var item in collection)
	{
		var methodInfo = item.GetType().GetProperty("Key");
		var val = methodInfo?.GetValue(item);
		if (val != null)
			items.Add(val.ToString());
	}
}

var cacheKeysTobeCleaned =
	items.Where(key => key.StartsWith(cacheKey, StringComparison.OrdinalIgnoreCase)).ToList(); //cacheKey is the keyword we are searching for in cacheKey

if (!cacheKeysTobeCleaned.Any())
	return NotFound();

foreach (string cacheKeyToBeCleaned in cacheKeysTobeCleaned)
{
//_cache is an instance of ISynchronizedObjectInstanceCache
	this._cache.RemoveLocal(cacheKeyToBeCleaned);
	this._cache.RemoveRemote(cacheKeyToBeCleaned);
}
			
This method uses reflection to access the MemoryCache entries, finds keys matching a prefix, and clears them using ISynchronizedObjectInstanceCache.

Conclusion

Managing cache in Optimizely CMS requires strategic approaches, especially when dealing with a large number of cache entries or dynamic keys. By understanding how to interact with ISynchronizedObjectInstanceCache and leveraging reflection to access MemoryCache, you can maintain a clean, efficient cache system. This not only enhances performance but also ensures your web application remains responsive and up-to-date with the latest data.
Oct 28, 2024

Comments

valdis
valdis Oct 29, 2024 06:12 AM

nice, this might become handy for my plugin. +1

Sanjay Kumar
Sanjay Kumar Oct 29, 2024 12:27 PM

Really nice article and helpful for me.

Please login to comment.
Latest blogs
XSS Vulnerabilities Patched with TinyMCE 6.8.4

Two different XSS vulnerabilities were fixed in the latest update of the NuGet package EPiServer.CMS.TinyMce. Update today!

Tomas Hensrud Gulla | Oct 30, 2024 | Syndicated blog

Free AI-Assistant Plugin for All Optimizely Partners

As Optimizely solution partners, it is vital to stay updated, particularly in the era of AI. Being informed and gaining hands-on experience with ne...

Luc Gosso (MVP) | Oct 26, 2024 | Syndicated blog

Bring your own Cloudflare (BYOCF) to Optimizely DXP

The Optimizely Digital Experience Platform (DXP) cloud service includes Cloudflare as a packaged component for CDN services and WAF protection. The...

Ronil Rangaiya | Oct 24, 2024 | Syndicated blog