This post focuses on content and object cache invalidation inside Optimizely CMS. It does not cover CDN cache, browser cache, or output cache.
Background
Most of the time, Optimizely handles cache invalidation automatically and very well. In normal publishing workflows, you usually do not need to clear cache manually.
That said, manual cache invalidation can still be useful in a few special cases: diagnostics, custom integrations, load-balanced proof-of-concepts, or controlled troubleshooting where you want to force content or object cache to refresh.
Below I have reordered the examples so the latest Optimizely CMS 12 and CMS 13 approach comes first, followed by older EPiServer examples for legacy projects.
Optimizely CMS 12 and CMS 13
For modern Optimizely CMS solutions, the recommended APIs are:
IContentCacheRemoverfor content cache invalidationISynchronizedObjectInstanceCachefor general object cache invalidation
Clear the full content cache
using EPiServer;
public class CacheService
{
private readonly IContentCacheRemover _contentCacheRemover;
public CacheService(IContentCacheRemover contentCacheRemover)
{
_contentCacheRemover = contentCacheRemover;
}
public void ClearContentCache()
{
_contentCacheRemover.Clear();
}
}
Clear the cache for a specific page or content item
using EPiServer;
using EPiServer.Core;
public class CacheService
{
private readonly IContentCacheRemover _contentCacheRemover;
public CacheService(IContentCacheRemover contentCacheRemover)
{
_contentCacheRemover = contentCacheRemover;
}
public void ClearSpecificContentCache()
{
_contentCacheRemover.Remove(ContentReference.StartPage);
}
}
Clear a specific language branch
_contentCacheRemover.RemoveLanguage(ContentReference.StartPage, "en");
Clear the general object cache
If you previously used CacheManager, the newer approach is to use ISynchronizedObjectInstanceCache.
using EPiServer.Framework.Cache;
public class CacheService
{
private readonly ISynchronizedObjectInstanceCache _synchronizedCache;
public CacheService(ISynchronizedObjectInstanceCache synchronizedCache)
{
_synchronizedCache = synchronizedCache;
}
public void ClearObjectCache()
{
_synchronizedCache.Clear();
}
}
Example endpoint for internal diagnostics
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.Cache;
using Microsoft.AspNetCore.Mvc;
[Route("util/cache")]
public class CacheController : Controller
{
private readonly IContentCacheRemover _contentCacheRemover;
private readonly ISynchronizedObjectInstanceCache _synchronizedCache;
public CacheController(
IContentCacheRemover contentCacheRemover,
ISynchronizedObjectInstanceCache synchronizedCache)
{
_contentCacheRemover = contentCacheRemover;
_synchronizedCache = synchronizedCache;
}
[HttpPost("content/clear")]
public IActionResult ClearContentCache()
{
_contentCacheRemover.Clear();
return Content("Ok, content cache cleared.");
}
[HttpPost("content/{id:int}")]
public IActionResult ClearContent(int id)
{
_contentCacheRemover.Remove(new ContentReference(id));
return Content("Ok, content cache cleared.");
}
[HttpPost("object/clear")]
public IActionResult ClearObjectCache()
{
_synchronizedCache.Clear();
return Content("Ok, object cache cleared.");
}
}
Legacy EPiServer Examples
The examples below are for older EPiServer projects and are mainly useful when working with legacy CMS 6 to CMS 11 solutions.
Invalidate the cache for a specific EPiServer page
EPiServer 9.9+
var contentCacheRemover = ServiceLocator.Current.GetInstance<EPiServer.IContentCacheRemover>();
contentCacheRemover.Remove(ContentReference.StartPage);
EPiServer 6
DataFactoryCache.RemovePage(ContentReference.StartPage);
Invalidate the cache for an EPiServer site on a server
EPiServer 7
EPiServer.CacheManager.Clear();
EPiServer 6
EPiServer.DataFactoryCache.Clear();
A small web service to invalidate site cache:
public void ProcessRequest(HttpContext context)
{
EPiServer.CacheManager.Clear();
context.Response.ContentType = "text/plain";
context.Response.Write("Ok, site cache cleared.");
}
Final Note
For most projects, it is best to let Optimizely manage cache invalidation automatically. Manual cache clearing should be reserved for troubleshooting, controlled utilities, and special integration scenarios.
Thanks to Wałdis Iljuczonok for previously highlighting the newer public API approach using IContentCacheRemover instead of older cache APIs.
0 comments :
Post a Comment