using System; using System.Collections.Generic; using NzbDrone.Common.EnsureThat; namespace NzbDrone.Common.Cache { public interface ICacheManager { ICached GetCache(Type host); ICached GetCache(Type host, string name); ICached GetRollingCache(Type host, string name, TimeSpan defaultLifeTime); ICachedDictionary GetCacheDictionary(Type host, string name, Func> fetchFunc = null, TimeSpan? lifeTime = null); void Clear(); ICollection Caches { get; } } public class CacheManager : ICacheManager { private readonly ICached _cache; public CacheManager() { _cache = new Cached(); } public void Clear() { _cache.Clear(); } public ICollection Caches => _cache.Values; public ICached GetCache(Type host) { Ensure.That(host, () => host).IsNotNull(); return GetCache(host, host.FullName); } public ICached GetCache(Type host, string name) { Ensure.That(host, () => host).IsNotNull(); Ensure.That(name, () => name).IsNotNullOrWhiteSpace(); return (ICached)_cache.Get(host.FullName + "_" + name, () => new Cached()); } public ICached GetRollingCache(Type host, string name, TimeSpan defaultLifeTime) { Ensure.That(host, () => host).IsNotNull(); Ensure.That(name, () => name).IsNotNullOrWhiteSpace(); return (ICached)_cache.Get(host.FullName + "_" + name, () => new Cached(defaultLifeTime, true)); } public ICachedDictionary GetCacheDictionary(Type host, string name, Func> fetchFunc = null, TimeSpan? lifeTime = null) { Ensure.That(host, () => host).IsNotNull(); Ensure.That(name, () => name).IsNotNullOrWhiteSpace(); return (ICachedDictionary)_cache.Get("dict_" + host.FullName + "_" + name, () => new CachedDictionary(fetchFunc, lifeTime)); } } }