2013-12-10 30 views
0

我使用Microsoft企業庫的高速緩存塊進行高速緩存時存在間歇性問題,因爲從高速緩存檢索項目時出現Index was outside the bounds of the array異常。這只是在我們的生產服務器上間歇性地發生,我認爲正在發生的事情是,當cacheManager.Keys.CopyTo(currentCacheKeys, 0);行執行時,緩存管理器密鑰已被另一個進程更新,並且上面定義的currentCacheKeys陣列不再足夠容納新條目,因此Index was outside the bounds of the array例外。企業庫高速緩存管理器「索引超出了數組的範圍」錯誤

這是一個合理的假設,有沒有人有任何建議來解決這個問題?我唯一能想到的就是使用object lock type of approach,但我不確定這是否會起作用,因爲我需要鎖定什麼?

public static string[] GetCacheKeys(int? instanceId) 
{ 
    string[] cacheKeys = null; 

    ICacheManager cacheManager = CacheFactory.GetCacheManager(); 
    if (cacheManager != null) 
    { 
     int cachedItemCount = cacheManager.Count; 
     if (cachedItemCount > 0) 
     { 
      object[] currentCacheKeys = new object[cachedItemCount]; 
      cacheManager.Keys.CopyTo(currentCacheKeys, 0); 

      if (currentCacheKeys != null && currentCacheKeys.Length > 0) 
      { 
       ArrayList cacheKeyList = new ArrayList(); 

       // omitted for brevity 

       cacheKeys = (string[])cacheKeyList.ToArray(typeof(string)); 
      } 
     } 
    } 

    return cacheKeys; 
} 

堆棧跟蹤

Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. 

    at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices) 
    at System.Collections.Hashtable.CopyKeys(Array array, Int32 arrayIndex) 
    at ProjectName.Library.Helpers.CacheHelper.GetCacheKeys(Nullable`1 instanceId) 
    ... 

回答

0

這是從企業庫的自定義實現返回的CacheManager鍵時造成的 - 發生了什麼事是正被定義爲存儲密鑰的陣列,而是通過密鑰複製到陣列上的時間不夠大,因爲新的密鑰已添加到緩存中。它是通過使用傳入的CacheManager構造緩存對象的CurrrentCacheState屬性進行解析,而不是realCache.Keys

public ICollection Keys 
    { 
     get { return realCache.CurrentCacheState.Keys; } 
    } 

查看完整描述here

相關問題