2022-03-07 13:38:53 +00:00
|
|
|
|
using VoidCat.Services.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace VoidCat.Services;
|
|
|
|
|
|
2022-06-13 10:29:16 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-07 13:38:53 +00:00
|
|
|
|
public abstract class BasicCacheStore<TStore> : IBasicStore<TStore>
|
|
|
|
|
{
|
2022-09-08 09:41:31 +00:00
|
|
|
|
protected readonly ICache _cache;
|
2022-03-07 13:38:53 +00:00
|
|
|
|
|
|
|
|
|
protected BasicCacheStore(ICache cache)
|
|
|
|
|
{
|
2022-09-08 09:41:31 +00:00
|
|
|
|
_cache = cache;
|
2022-03-07 13:38:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:29:16 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-07 13:38:53 +00:00
|
|
|
|
public virtual ValueTask<TStore?> Get(Guid id)
|
|
|
|
|
{
|
2022-09-08 09:41:31 +00:00
|
|
|
|
return _cache.Get<TStore>(MapKey(id));
|
2022-06-06 21:51:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:29:16 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual ValueTask Add(Guid id, TStore obj)
|
2022-03-07 13:38:53 +00:00
|
|
|
|
{
|
2022-09-08 09:41:31 +00:00
|
|
|
|
return _cache.Set(MapKey(id), obj);
|
2022-03-07 13:38:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:29:16 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-07 13:38:53 +00:00
|
|
|
|
public virtual ValueTask Delete(Guid id)
|
|
|
|
|
{
|
2022-09-08 09:41:31 +00:00
|
|
|
|
return _cache.Delete(MapKey(id));
|
2022-03-07 13:38:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:29:16 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Map an id to a key in the KV store
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected abstract string MapKey(Guid id);
|
2022-03-07 13:38:53 +00:00
|
|
|
|
}
|