是否可以获得HttpRuntime.Cache对象的Cache项优先级?
如果是这样,最好的方法是什么?
答案 0 :(得分:2)
您可以使用此:
public static DateTime GetCachePriority(string strKey)
{
object cacheProvider = HttpRuntime.Cache.GetType().GetProperty("InternalCache", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(HttpRuntime.Cache, null);
object intenralCacheStore = cacheProvider?.GetType().GetField("_cacheInternal", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(cacheProvider);
if(intenralCacheStore != null)
{
Type cacheOption = HttpRuntime.Cache.GetType().Assembly.GetTypes().FirstOrDefault(d => d.Name == "CacheGetOptions");
MethodInfo methodInfo = intenralCacheStore.GetType().GetMethod("DoGet", BindingFlags.NonPublic | BindingFlags.Instance, null, CallingConventions.Any,
new[] { typeof(bool), typeof(string), cacheOption }, null);
if(methodInfo != null)
{
dynamic cacheEntry = methodInfo.Invoke(intenralCacheStore, new Object[] { true, strKey, 1 });
PropertyInfo usageBucketProperty = cacheEntry.GetType().GetProperty("UsageBucket", BindingFlags.NonPublic | BindingFlags.Instance);
byte byPriority = (byte) usageBucketProperty.GetValue(cacheEntry, null);
CacheItemPriority prriority = byPriority == byte.MaxValue ? CacheItemPriority.NotRemovable : (CacheItemPriority)byPriority + 1;
strPriority = prriority.ToString();
}
}
}
谢谢。