This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,140 @@
using System;
using CacheManager.Core;
using CacheManager.Core.Internal;
namespace Kreta.Framework.Caching
{
public abstract class GenericCache<TCacheValueType> : Cache, IDisposable
{
private ICacheManager<TCacheValueType> cache;
protected GenericCache(CacheManager cacheManager, string cacheName) : base(cacheManager)
{
cache = CacheFactory.FromConfiguration<TCacheValueType>(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<string, TCacheValueType> 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<string, string, TCacheValueType> valueFactory)
{
return cache.GetOrAdd(key, region, valueFactory);
}
protected TCacheValueType AddOrUpdate(string key, TCacheValueType addValue, Func<TCacheValueType, TCacheValueType> updateValueFactory)
{
return cache.AddOrUpdate(key, addValue, updateValueFactory);
}
protected TCacheValueType AddOrUpdateWithRegion(string key, string region, TCacheValueType addValue, Func<TCacheValueType, TCacheValueType> 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<TCacheValueType, TCacheValueType> valueFactory)
{
cache.Update(key, valueFactory);
}
protected void Remove(string key)
{
cache.Remove(key);
}
protected void RemoveFromRegion(string key, string region)
{
cache.Remove(key, region);
}
/// <summary>
///Clear the cache region, removeing all items from the specific region only
/// </summary>
/// <param name="region"></param>
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<TCacheValueType>(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);
}
}
}