我正在使用给定的单例模式在 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; }
}