我正在使用给定的单例模式来在ASP.NET中缓存类对象。有人可以指出这种方法的缺点吗?
public class CacheManager
{
private static CacheManager _instance;
protected CacheManager(string key) { id = key; }
public static CacheManager getInstance(string key)
{
if (HttpContext.Current.Cache[key] == null)
HttpContext.Current.Cache.Insert(key, _instance = new CacheManger(key), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(120),CacheItemPriority.Default,new CacheItemRemovedCallback(ReportRemovedCallback));
return (CacheManager)HttpContext.Current.Cache[key];
}
private static void ReportRemovedCallback(string CacheManager, object value, CacheItemRemovedReason reason)
{
_instance = null;
}
public string id { get; private set; }
public string field1 { get; set; }
public string field2 { get; set; }
public string field3 { get; set; }
}
HttpContext.Current
所做的,你只是在其中添加了特定于你的应用程序的辅助方法。我对此没有任何问题。 - YuckgetInstance
和ReportRemovedCallback
方法中试图做什么? - YavgenyP