voidkitty/VoidCat/Services/BasicCacheStore.cs

39 lines
855 B
C#
Raw Permalink Normal View History

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>
{
protected readonly ICache Cache;
2022-03-07 13:38:53 +00:00
protected BasicCacheStore(ICache cache)
{
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)
{
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
{
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)
{
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
}