Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active August 26, 2020 20:20
Show Gist options
  • Save mattbrailsford/32e814e989c47ffaef8a3e92e67a0246 to your computer and use it in GitHub Desktop.
Save mattbrailsford/32e814e989c47ffaef8a3e92e67a0246 to your computer and use it in GitHub Desktop.
using System.Web.Http;
using Umbraco.Core.Cache;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
using Umbraco.Web.Cache;
using Umbraco.Core.Services.Changes;
namespace Our.Umbraco.Web.Controllers
{
// Accessed via /umbraco/api/CacheHelperApi/ClearCaches
public class CacheHelperApiController : UmbracoApiController // UmbracoAuthorizedApiController
{
private AppCaches _appCaches;
private DistributedCache _distributedCache;
public CacheHelperApiController(AppCaches appCaches, DistributedCache distributedCache)
{
_appCaches = appCaches;
_distributedCache = distributedCache;
}
[HttpGet]
public void ClearCaches()
{
// Clear system caches
_appCaches.RequestCache.Clear();
_appCaches.RuntimeCache.Clear();
_appCaches.IsolatedCaches.ClearAllCaches();
// Clear NuCache memory cache
_distributedCache.RefreshByPayload(ContentCacheRefresher.UniqueId, new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) });
_distributedCache.RefreshByPayload(MediaCacheRefresher.UniqueId, new[] { new MediaCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) });
_distributedCache.RefreshByPayload(DomainCacheRefresher.UniqueId, new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment