CacheResultAdvice: lock on write to hashtable (#199)

Co-authored-by: Roberto Paterlini <r.paterlini@almaviva.it>
This commit is contained in:
Roberto Paterlini
2021-01-09 09:32:27 +01:00
committed by GitHub
parent 1054af4504
commit e8bd9f2290

View File

@@ -29,6 +29,7 @@ using AopAlliance.Intercept;
using Spring.Caching;
using Spring.Util;
using System;
using System.Collections.Concurrent;
#endregion
@@ -66,20 +67,18 @@ namespace Spring.Aspects.Cache
}
}
private readonly Hashtable _cacheResultAttributeCache = new Hashtable();
private readonly ConcurrentDictionary<MethodInfo, CacheResultInfo> _cacheResultAttributeCache = new();
private CacheResultInfo GetCacheResultInfo(MethodInfo method)
{
CacheResultInfo cacheResultInfo = (CacheResultInfo)_cacheResultAttributeCache[method];
// no need for locking here - last one wins
if (cacheResultInfo == null)
var cacheResultInfo = _cacheResultAttributeCache.GetOrAdd(method, _ =>
{
CacheResultAttribute resultInfo = (CacheResultAttribute)GetCustomAttribute(method, typeof(CacheResultAttribute));
CacheResultItemsAttribute[] itemInfoArray = (CacheResultItemsAttribute[])GetCustomAttributes(method, typeof(CacheResultItemsAttribute));
var resultInfo = (CacheResultAttribute) GetCustomAttribute(method, typeof(CacheResultAttribute));
var itemInfoArray = (CacheResultItemsAttribute[]) GetCustomAttributes(method, typeof(CacheResultItemsAttribute));
return new CacheResultInfo(resultInfo, itemInfoArray);
});
cacheResultInfo = new CacheResultInfo(resultInfo, itemInfoArray);
_cacheResultAttributeCache[method] = cacheResultInfo;
}
return cacheResultInfo;
}