mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-24 19:08:18 +08:00
Use ConcurrentDictionary to simplify code
--HG-- branch : dev
This commit is contained in:
parent
82cb85bba1
commit
b2be561e3f
@ -1,27 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Castle.Core;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Orchard.Caching {
|
||||
public class DefaultCacheHolder : ICacheHolder {
|
||||
private readonly IDictionary<CacheKey, object> _caches = new Dictionary<CacheKey, object>();
|
||||
private readonly ConcurrentDictionary<CacheKey, object> _caches = new ConcurrentDictionary<CacheKey, object>();
|
||||
|
||||
class CacheKey : Pair<Type, Pair<Type, Type>> {
|
||||
class CacheKey : Tuple<Type, Type, Type> {
|
||||
public CacheKey(Type component, Type key, Type result)
|
||||
: base(component, new Pair<Type, Type>(key, result)) {
|
||||
: base(component, key, result) {
|
||||
}
|
||||
}
|
||||
|
||||
public ICache<TKey, TResult> GetCache<TKey, TResult>(Type component) {
|
||||
var cacheKey = new CacheKey(component, typeof(TKey), typeof(TResult));
|
||||
lock (_caches) {
|
||||
object value;
|
||||
if (!_caches.TryGetValue(cacheKey, out value)) {
|
||||
value = new Cache<TKey, TResult>();
|
||||
_caches[cacheKey] = value;
|
||||
}
|
||||
return (ICache<TKey, TResult>)value;
|
||||
}
|
||||
var result = _caches.GetOrAdd(cacheKey, k => new Cache<TKey, TResult>());
|
||||
return (Cache<TKey, TResult>)result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user