Skip to content

Instantly share code, notes, and snippets.

@Antonytm
Created April 11, 2018 09:19
Show Gist options
  • Save Antonytm/286f2c5e4bcf3c92b9807f187aaafb53 to your computer and use it in GitHub Desktop.
Save Antonytm/286f2c5e4bcf3c92b9807f187aaafb53 to your computer and use it in GitHub Desktop.
Discovering what is inside Sitecore HTML Cache
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HtmlCache.aspx.cs" Inherits="Sitecore.Utils.HtmlCacheList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:Repeater ID="WebsiteCache" runat="server" ItemType="System.Tuple`2 [System.String, System.String]">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="Key" Text='<%# Item.Item1 %>' />
</td>
<td>
<asp:Label runat="server" ID="Value" Text='<%# Item.Item2 %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using Sitecore.Caching;
namespace Sitecore.Utils
{
public partial class HtmlCacheList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var websiteCache = GetCaches("website");
WebsiteCache.DataSource = websiteCache;
WebsiteCache.DataBind();
}
private List<Tuple<string, string>> GetCaches(string sitename)
{
var list = new List<Tuple<string, string>>();
HtmlCache htmlCache = CacheManager.GetHtmlCache(Sitecore.Sites.SiteContextFactory.GetSiteContext(sitename));
if (htmlCache != null)
{
FieldInfo fi = typeof (CustomCache).GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance);
var cache = (Cache) fi.GetValue(htmlCache);
var fi2 = typeof (Cache).GetField("data", BindingFlags.NonPublic | BindingFlags.Instance);
var hashTable = (Hashtable) fi2.GetValue(cache);
foreach (var cacheKey in hashTable.Keys)
{
list.Add(new Tuple<string, string>(cacheKey.ToString(), HttpUtility.HtmlEncode(((Cache.CacheEntry) hashTable[cacheKey]).Data.ToString())));
}
}
return list;
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Sitecore.Utils {
public partial class HtmlCacheList {
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// WebsiteCache control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Repeater WebsiteCache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment