下面的函数对于访问量较高的网站是否是一个好的实践?死锁或线程安全问题有什么缺点吗?
public static T GetInitializedTempCache<T>(string key, Func<T> getData, int minutes, bool skip = false)
{
if (!skip)
{
var value = HttpRuntime.Cache[key];
if (value == null || value.GetType() != typeof(T))
{
T data = getData();
if (data != null && Config.Debugging.EnableCaching)
{
HttpRuntime.Cache.Add(key, data, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
}
return data;
}
return (T)value;
}
else
{
return getData();
}
}
public static void RemoveTempCacheContains(string key)
{
var keys = new List<string>();
var enumerator = HttpRuntime.Cache.GetEnumerator();
// copy all keys that currently exist in Cache
while (enumerator.MoveNext())
{
var cacheKey = (string)enumerator.Key;
if (cacheKey.Contains(key))
{
keys.Add(cacheKey);
}
}
for (int i = 0; i < keys.Count; i++)
{
HttpRuntime.Cache.Remove(keys[i]);
}
}