2018-11-02 21:04:49 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
2018-11-07 04:33:22 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-11-02 21:04:49 +00:00
|
|
|
|
|
|
|
|
|
namespace Roadie.Library.Caching
|
|
|
|
|
{
|
|
|
|
|
public abstract class CacheManagerBase : ICacheManager
|
|
|
|
|
{
|
2018-11-22 23:12:57 +00:00
|
|
|
|
public const string SystemCacheRegionUrn = "urn:system";
|
|
|
|
|
|
2018-11-02 21:04:49 +00:00
|
|
|
|
protected readonly CachePolicy _defaultPolicy = null;
|
2018-11-11 14:46:09 +00:00
|
|
|
|
protected ILogger Logger { get; }
|
2018-11-02 21:04:49 +00:00
|
|
|
|
protected readonly JsonSerializerSettings _serializerSettings = null;
|
|
|
|
|
|
|
|
|
|
public CacheManagerBase(ILogger logger, CachePolicy defaultPolicy)
|
|
|
|
|
{
|
2018-11-11 14:46:09 +00:00
|
|
|
|
this.Logger = logger;
|
2018-11-02 21:04:49 +00:00
|
|
|
|
this._defaultPolicy = defaultPolicy;
|
|
|
|
|
this._serializerSettings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ContractResolver = new IgnoreJsonAttributesResolver(),
|
|
|
|
|
Formatting = Formatting.Indented
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract bool Add<TCacheValue>(string key, TCacheValue value);
|
|
|
|
|
|
|
|
|
|
public abstract bool Add<TCacheValue>(string key, TCacheValue value, CachePolicy policy);
|
|
|
|
|
|
2018-11-06 21:55:31 +00:00
|
|
|
|
public abstract bool Add<TCacheValue>(string key, TCacheValue value, string region);
|
|
|
|
|
|
2018-11-02 21:04:49 +00:00
|
|
|
|
public abstract bool Add<TCacheValue>(string key, TCacheValue value, string region, CachePolicy policy);
|
|
|
|
|
|
|
|
|
|
public abstract void Clear();
|
|
|
|
|
|
|
|
|
|
public abstract void ClearRegion(string region);
|
|
|
|
|
|
|
|
|
|
public abstract bool Exists<TOut>(string key);
|
|
|
|
|
|
|
|
|
|
public abstract bool Exists<TOut>(string key, string region);
|
|
|
|
|
|
|
|
|
|
public abstract TOut Get<TOut>(string key);
|
|
|
|
|
|
|
|
|
|
public abstract TOut Get<TOut>(string key, string region);
|
|
|
|
|
|
|
|
|
|
public abstract TOut Get<TOut>(string key, Func<TOut> getItem, string region);
|
|
|
|
|
|
|
|
|
|
public abstract TOut Get<TOut>(string key, Func<TOut> getItem, string region, CachePolicy policy);
|
|
|
|
|
|
2018-11-07 04:33:22 +00:00
|
|
|
|
public abstract Task<TOut> GetAsync<TOut>(string key, Func<Task<TOut>> getItem, string region);
|
|
|
|
|
|
2018-11-02 21:04:49 +00:00
|
|
|
|
public abstract bool Remove(string key);
|
|
|
|
|
|
|
|
|
|
public abstract bool Remove(string key, string region);
|
|
|
|
|
|
|
|
|
|
protected TOut Deserialize<TOut>(string s)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(s))
|
|
|
|
|
{
|
|
|
|
|
return default(TOut);
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<TOut>(s, this._serializerSettings);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-11-11 21:13:19 +00:00
|
|
|
|
this.Logger.LogError(ex);
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
return default(TOut);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string Serialize(object o)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.SerializeObject(o, this._serializerSettings);
|
|
|
|
|
}
|
2018-11-11 16:20:33 +00:00
|
|
|
|
|
|
|
|
|
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|