#7614 Fixed locking cache-key. (#7617)

Fixes #7614
This commit is contained in:
Kamil
2017-03-21 18:26:30 +01:00
committed by Sébastien Ros
parent d02028df40
commit 80546b463a

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
namespace Orchard.Caching { namespace Orchard.Caching {
public interface ICacheManager { public interface ICacheManager {
@@ -7,9 +8,13 @@ namespace Orchard.Caching {
} }
public static class CacheManagerExtensions { public static class CacheManagerExtensions {
private static readonly ConcurrentDictionary<object, object> _locks = new ConcurrentDictionary<object, object>();
public static TResult Get<TKey, TResult>(this ICacheManager cacheManager, TKey key, bool preventConcurrentCalls, Func<AcquireContext<TKey>, TResult> acquire) { public static TResult Get<TKey, TResult>(this ICacheManager cacheManager, TKey key, bool preventConcurrentCalls, Func<AcquireContext<TKey>, TResult> acquire) {
if (preventConcurrentCalls) { if (preventConcurrentCalls) {
lock(key) { var lockKey = _locks.GetOrAdd(key, _ => new object());
lock (lockKey) {
return cacheManager.Get(key, acquire); return cacheManager.Get(key, acquire);
} }
} }