void.cat/VoidCat/Services/BasicCacheStore.cs

55 lines
1.2 KiB
C#
Raw 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>
{
2022-06-13 10:29:16 +00:00
protected readonly ICache Cache;
2022-03-07 13:38:53 +00:00
protected BasicCacheStore(ICache cache)
{
2022-06-13 10:29:16 +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-06-13 10:29:16 +00:00
return Cache.Get<TStore>(MapKey(id));
2022-03-07 13:38:53 +00:00
}
2022-06-13 10:29:16 +00:00
/// <inheritdoc />
2022-06-06 21:51:25 +00:00
public virtual async ValueTask<IReadOnlyList<TStore>> Get(Guid[] ids)
{
var ret = new List<TStore>();
foreach (var id in ids)
{
2022-06-13 10:29:16 +00:00
var r = await Cache.Get<TStore>(MapKey(id));
2022-06-06 21:51:25 +00:00
if (r != null)
{
ret.Add(r);
}
}
return ret;
}
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-06-13 10:29:16 +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-06-13 10:29:16 +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
}