我使用給定的單例模式來緩存ASP.NET中的類對象。任何人都可以強調這種方法的缺點嗎?Disadvanage給定Singleton緩存模式
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'所做的,你只需要添加特定於你的應用程序的helper。我沒有看到任何問題。 – Yuck 2013-03-06 13:33:41
它不是線程安全的,但在此之前 - 你想在getInstance和ReportRemovedCallback方法中做什麼? – YavgenyP 2013-03-06 13:38:43