2017-07-10 102 views
-7

林上的MemoryCache在這裏工作是我的幾個代碼的C#的MemoryCache擅自改變數據

緩存類

private static string policy = "__"; 
    public static void SaveTocache(string cacheKey, object savedItem,int hour = 43800) 
    { 
     if (!cacheKey.StartsWith(policy)) cacheKey = policy + cacheKey; 
     CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); 
     cacheItemPolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddHours(hour)); 
     RemoveFromCache(cacheKey); 
     MemoryCache.Default.Add(cacheKey, savedItem, cacheItemPolicy); 
    } 
    public static T GetFromCache<T>(string cacheKey) where T : class 
    { 
     if (!cacheKey.StartsWith(policy)) cacheKey = policy + cacheKey; 
     return MemoryCache.Default[cacheKey] as T; 
    } 
    public static void RemoveFromCache(string cacheKey) 
    { 
     if(!cacheKey.StartsWith(policy))cacheKey = policy + cacheKey; 
     foreach (string key in MemoryCache.Default.Where(obj=> obj.Key.StartsWith(cacheKey)).Select(obj=> obj.Key)) 
     { 
      MemoryCache.Default.Remove(key); 
     } 
    } 
    public static bool IsInCache(string cacheKey) 
    { 
     if (!cacheKey.StartsWith(policy)) cacheKey = policy + cacheKey; 
     return MemoryCache.Default[cacheKey] != null; 
    } 

所以我有一個叫做日誌當我添加一個新的日誌類那裏有一個bug,繼承人代碼

 Models.Log l = new Models.Log(); 
     l.LogId = Guid.NewGuid(); 
     l.Topic = topic; 
     l.PIC = pic; 
     l.LogStatusId = logStatus; 
     l.LogRarityId = logRarity; 
     l.LogTypeId = logType; 
     l.SavedUser = savedUser; 
     l.RequestedUser = requestedUser; 
     l.Division = division; 
     l.InsertedDate = DateTime.Now; 
     l.Description = description; 
     List<Models.Log> list = getData(); 
     Utility.Connect.Agla15Context.Logs.Add(l); 
     Utility.Connect.Agla15Context.SaveChanges(); 
     string caches = cacheName; 
     list.Add(l); 
     Utility.CacheHelper.SaveTocache(caches, list); 

該錯誤是例如myprevious對象(緩存的值): 3對象A,B,C 我的下一個對象(緩存值)例如: 4對象A,B,C,d

所以我想通過調用RemoveFromCache和添加對象(緩存值)A,取出對象A,B,C,B,C,d

當我走帶緩存通過調用GetFromCache有時我得到對象(緩存值)A,B,C,有時我得到對象(緩存值)A,B,C,D什麼發生在我的代碼。

或精簡一看這樣

List<String> a = new List<String>(){"A","B","C"}; 
Utility.CacheHelper.SaveTocache("MyKey",a); 
List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey");//A,B,C 
b.add("D"); 
Utility.CacheHelper.SaveTocache("MyKey",b); 
for(int i=0;i<100;i++) 
{ 
    List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey");//Sometimes A,B,C sometimes A,B,C,D 
} 
+1

「A,B,C」和「A,B,C,D」是什麼意思? – mjwills

+0

所有這些'policy + cacheKey;'語句都不會增加問題,但會分散注意力。 –

+1

不完整且不能令人信服。創建一個[mcve]。 –

回答

-1

變化:

Utility.CacheHelper.SaveTocache("MyKey",a); 

到:

Utility.CacheHelper.SaveTocache("MyKey",a.ToList()); 

和:

List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey"); 

到:

List<String> b = Utility.CacheHelper.GetFromCache<List<String>>("MyKey").ToList(); 

技術上只是要求後者改變 - 前者將使它更加防彈。

這是必要的,因爲如果添加到的MemoryCache任何條目將shared amongst callers - 所以,如果一個來電者操縱List然後其他呼叫者也將看到,操作列表。

通過致電ToList確保每個呼叫者都獲得自己的List副本 - 因此添加到其中的項目將不會在其他項目中看到。