我使用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)
...