using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Abp.Dependency; using Abp.Runtime.Caching.Configuration; namespace Abp.Runtime.Caching { /// /// Base class for cache managers. /// public abstract class CacheManagerBase : ICacheManager, ISingletonDependency { protected readonly IIocManager IocManager; protected readonly ICachingConfiguration Configuration; protected readonly ConcurrentDictionary Caches; /// /// Constructor. /// /// /// protected CacheManagerBase(IIocManager iocManager, ICachingConfiguration configuration) { IocManager = iocManager; Configuration = configuration; Caches = new ConcurrentDictionary(); } public IReadOnlyList GetAllCaches() { return Caches.Values.ToImmutableList(); } public virtual ICache GetCache(string name) { Check.NotNull(name, nameof(name)); return Caches.GetOrAdd(name, (cacheName) => { var cache = CreateCacheImplementation(cacheName); var configurators = Configuration.Configurators.Where(c => c.CacheName == null || c.CacheName == cacheName); foreach (var configurator in configurators) { configurator.InitAction?.Invoke(cache); } return cache; }); } public virtual void Dispose() { DisposeCaches(); Caches.Clear(); } protected virtual void DisposeCaches() { foreach (var cache in Caches) { IocManager.Release(cache.Value); } } /// /// Used to create actual cache implementation. /// /// Name of the cache /// Cache object protected abstract ICache CreateCacheImplementation(string name); } }