The Sitecore caching engine is a simple yet powerful tool where Sitecore not only stores it’s own data for fast retrieval, but allows you to store your own data of any kind. Sitecore have a built in overview of the contents of the caches, but the overview lacks the possibility to clear caches individually.
The cache admin is located on the /sitecore/admin/cache.aspx URL, and as you can see, you can only clear all caches:
Clearing one cache at a time is very easy:
Sitecore.Caching.Cache cache = Sitecore.Caching.CacheManager.FindCacheByName("name of cache"); cache.Clear();
So it is not that hard to create your own cache admin page that can clear each cache individually:
This is a simple .aspx page that lists all caches, displays some nice cache stats, and sports a nice “Clear Cache” button that clears that specific cache. The code is simple:
<%@ Page language="c#" EnableEventValidation="false" AutoEventWireup="true" EnableViewState="false" %> <%@ Import Namespace="System.Security.Permissions" %> <%@ Import Namespace="System.Collections.Generic" %> <%@ Import Namespace="System.Threading" %> <%@ Import Namespace="System.Security.Principal" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) { Response.Buffer = false; Response.BufferOutput = false; DataBind(); } IEnumerable<Sitecore.Caching.Cache> Caches { get { return Sitecore.Caching.CacheManager.GetAllCaches().OrderBy(c => c.Name); } } double PercentageUsed(Sitecore.Caching.Cache cache) { if (cache.MaxSize == 0) return 0; return Math.Round(((double)cache.Size/(double)cache.MaxSize*100), 0); } string PercentageColor(double percentage) { if (percentage >= 0 && percentage <= 50) return "green"; if (percentage >= 50 && percentage <= 75) return "orange"; return "red"; } private void repCaches_Command(object source, RepeaterCommandEventArgs e) { switch (e.CommandName) { case "ClearCache": string cacheName = e.CommandArgument as string; Sitecore.Caching.Cache cache = Sitecore.Caching.CacheManager.FindCacheByName(cacheName); cache.Clear(); repCaches.DataBind(); break; } } long TotalMaxSize() { long ac = 0; foreach (var cache in Caches) ac = ac + cache.MaxSize; return ac; } long TotalSize() { long ac = 0; foreach (var cache in Caches) ac += cache.Size; return ac; } double TotalPercentageUsed() { if (TotalMaxSize() == 0) return 0; return Math.Round(((double)TotalSize()/(double)TotalMaxSize()*100), 1); } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>Cache Plus</title> <style type="text/css"> body { font: normal 12pt arial, verdana; padding:20px; } .box { padding:20px; margin:20px; border: solid 1px black; background-color:#efefef; } input { height: 30px; width: 100px; } td { border-bottom: solid 1px #aaa; padding-right: 20px; padding-left: 5px; padding-top: 5px; padding-bottom: 5px; } table { width:100%; } thead td { font-weight: bold; border-bottom: solid 1px #aaa; padding-right: 20px; } </style> </head> <body style="font-size:14px"> <form runat="server"> <div style="padding:20px; background-color:#eaeaea; border-bottom: solid 1px #777777; font-size:16px"> <%# Sitecore.StringUtil.GetSizeString(TotalSize()) %> of <%# Sitecore.StringUtil.GetSizeString(TotalMaxSize()) %> used <strong>(<%# TotalPercentageUsed() %>%)</strong> </div> <asp:Repeater ID="repCaches" runat="server" EnableViewState="false" OnItemCommand="repCaches_Command" DataSource="<%# Caches %>"> <HeaderTemplate> <table> <thead> <tr> <td>Name</td> <td></td> <td>Size</td> <td>MaxSize</td> <td>% Used</td> </tr> </thead> </HeaderTemplate> <FooterTemplate> </table> </FooterTemplate> <ItemTemplate> <tr> <td style="width:250px"> <%# (Container.DataItem as Sitecore.Caching.Cache).Name %> </td> <td style="width:100px"> <asp:Button ID="btnClearCache" runat="server" CommandArgument="<%# (Container.DataItem as Sitecore.Caching.Cache).Name %>" CommandName="ClearCache" text="Clear Cache"/> </td> <td style="text-align: right; width:80px"> <%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.Cache).Size) %> </td> <td style="text-align: right; width:80px"> <%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.Cache).MaxSize) %> </td> <td> <div style="width:<%# PercentageUsed((Container.DataItem as Sitecore.Caching.Cache)) %>%; height:30px; background-color:<%# PercentageColor(PercentageUsed((Container.DataItem as Sitecore.Caching.Cache))) %>; float:left;"></div> <div style="width:<%# (100 - PercentageUsed((Container.DataItem as Sitecore.Caching.Cache))) %>%; height:30px; background-color:#ccffcc; float:left;"></div> </td> </tr> </ItemTemplate> </asp:Repeater> </form> </body> </html>
Copy the code to a file named .aspx, and put the file in /sitecore modules/shell.
MORE TO READ:
- Sitecore custom cache that is cleared on publish by briancaos
Thanks to Richard Hauuer @richardhauer for the inspiration to write this article.
