在编写代码时,如果您使用常量(如下所示),您只能在缓存键上获得智能感知,但缺点是在添加新缓存项时必须保持常量值。
ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem(CacheKeys.SAMPLE_KEY)
public class CacheKeys
{
public const string SAMPLE_KEY = "some-example-key";
}
在调试时,您可以按如下方式查看缓存键;在后台 IRuntimeCacheProvider (ApplicationContext.ApplicationCache.RuntimeCache) 使用 HttpRuntime 缓存,因此虽然您不能直接迭代 RuntimeCache 属性中的缓存项,但您可以使用 HttpRuntime.Cache 像:
var keys = new StringBuilder();
foreach (DictionaryEntry cacheItem in HttpRuntime.Cache)
{
keys.AppendLine(cacheItem.Key.ToString());
}
通过 Umbraco 提供程序添加到运行时缓存的项目包含前缀“umbrtmche-”,因此您可能希望过滤结果:
HttpRuntime.Cache.Cast<DictionaryEntry>()
.Where(x => x.Key.ToString().StartsWith("umbrtmche"))
.Select(x => x.Key.ToString().Replace("umbrtmche-", ""))
.ToList();
最后要注意的是,Umbraco 使用缓存本身,因此您不仅会看到您添加的缓存键,如果您希望过滤这些我建议添加您自己的前缀,以便您可以过滤自己的缓存Umbraco 的钥匙。