Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A fix to make https://briancaos.wordpress.com/2017/05/01/sitecore-caching-clear-caches-individually/ work on Sitecore 8, plus a quick hack to view the contents of each cache
<%@ 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.ICacheInfo> Caches
{
get
{
return Sitecore.Caching.CacheManager.GetAllCaches().OrderBy(c => c.Name);
}
}
double PercentageUsed(Sitecore.Caching.ICacheInfo 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;
//var cache = Sitecore.Caching.CacheManager.FindCacheByName(cacheName);
var cache = Sitecore.Caching.CacheManager.GetAllCaches().Where(c => c.Name == cacheName).FirstOrDefault();
if (cache == null)
{
Response.Write("<div>NULL</div>");
}
else
{
cache.Clear();
repCaches.DataBind();
}
break;
case "ViewCache":
string cn = e.CommandArgument as string;
var cc = Sitecore.Caching.CacheManager.GetAllCaches().Where(c => c.Name == cn).FirstOrDefault() as Sitecore.Caching.Generics.ICache<string>;
var keys = cc.GetCacheKeys();
foreach(var key in keys)
{
Response.Write("<div>" + key + " -- " + cc[key] + "</div>");
}
Response.End();
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.ICacheInfo).Name %>
</td>
<td style="width:100px">
<asp:Button ID="btnClearCache" runat="server" CommandArgument="<%# (Container.DataItem as Sitecore.Caching.ICacheInfo).Name %>" CommandName="ClearCache" text="Clear Cache"/>
<asp:Button ID="btnViewCache" runat="server" CommandArgument="<%# (Container.DataItem as Sitecore.Caching.ICacheInfo).Name %>" CommandName="ViewCache" text="View Cache"/>
</td>
<td style="text-align: right; width:80px">
<%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.ICacheInfo).Size) %>
</td>
<td style="text-align: right; width:80px">
<%# Sitecore.StringUtil.GetSizeString((Container.DataItem as Sitecore.Caching.ICacheInfo).MaxSize) %>
</td>
<td>
<div style="width:<%# PercentageUsed((Container.DataItem as Sitecore.Caching.ICacheInfo)) %>%; height:30px; background-color:<%# PercentageColor(PercentageUsed((Container.DataItem as Sitecore.Caching.ICacheInfo))) %>; float:left;"></div>
<div style="width:<%# (100 - PercentageUsed((Container.DataItem as Sitecore.Caching.ICacheInfo))) %>%; height:30px; background-color:#ccffcc; float:left;"></div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment