using System; using CacheManager.Core; using CacheManager.Core.Internal; namespace Kreta.Framework.Caching { public abstract class GenericCache : Cache, IDisposable { private ICacheManager cache; protected GenericCache(CacheManager cacheManager, string cacheName) : base(cacheManager) { cache = CacheFactory.FromConfiguration(cacheName); cache.OnRemoveByHandle += Cache_OnRemoveByHandle; } protected bool Exists(string key) { return cache.Exists(key); } protected TCacheValueType Get(string key) { return cache.Get(key); } protected TCacheValueType GetByKeyAndRegion(string key, string region) { return cache.Get(key, region); } protected TCacheValueType GetOrAdd(string key, Func valueFactory) { return cache.GetOrAdd(key, valueFactory); } protected bool Add(string key, string region, TCacheValueType value) { return cache.Add(key, value, region); } protected TCacheValueType GetOrAddWithRegion(string key, string region, Func valueFactory) { return cache.GetOrAdd(key, region, valueFactory); } protected TCacheValueType AddOrUpdate(string key, TCacheValueType addValue, Func updateValueFactory) { return cache.AddOrUpdate(key, addValue, updateValueFactory); } protected TCacheValueType AddOrUpdateWithRegion(string key, string region, TCacheValueType addValue, Func updateValueFactory) { return cache.AddOrUpdate(key, region, addValue, updateValueFactory); } protected void Put(string key, TCacheValueType value) { cache.Put(key, value); } protected void PutWithRegion(string key, TCacheValueType value, string region) { cache.Put(key, value, region); } protected void Update(string key, Func valueFactory) { cache.Update(key, valueFactory); } protected void Remove(string key) { cache.Remove(key); } protected void RemoveFromRegion(string key, string region) { cache.Remove(key, region); } /// ///Clear the cache region, removeing all items from the specific region only /// /// protected void ClearRegion(string region) { cache.ClearRegion(region); } protected void Expire(string key, ExpirationMode mode, TimeSpan timeout) { cache.Expire(key, mode, timeout); } protected void AddWithAbsoluteExpiration(string key, TimeSpan timeSpan, TCacheValueType value) { cache.Add(new CacheItem(key, value, ExpirationMode.Absolute, timeSpan)); } protected virtual void Cache_OnRemoveByHandle(object sender, CacheItemRemovedEventArgs e) { } public override void Reset() { cache.Clear(); } bool disposed; protected virtual void Dispose(bool disposing) { if (!disposed && disposing) { if (cache != null) { cache.Dispose(); cache = null; } } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~GenericCache() { Dispose(false); } } }