mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-11-15 08:56:27 +01:00
Add user accounts base
This commit is contained in:
46
VoidCat/Services/InMemory/InMemoryCache.cs
Normal file
46
VoidCat/Services/InMemory/InMemoryCache.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.InMemory;
|
||||
|
||||
public class InMemoryCache : ICache
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public InMemoryCache(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public ValueTask<T?> Get<T>(string key)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<T?>(key));
|
||||
}
|
||||
|
||||
public ValueTask Set<T>(string key, T value, TimeSpan? expire = null)
|
||||
{
|
||||
if (expire.HasValue)
|
||||
{
|
||||
_cache.Set(key, value, expire.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
_cache.Set(key, value);
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask<string[]> GetList(string key)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<string[]>(key));
|
||||
}
|
||||
|
||||
public ValueTask AddToList(string key, string value)
|
||||
{
|
||||
var list = new HashSet<string>(GetList(key).Result);
|
||||
list.Add(value);
|
||||
_cache.Set(key, list.ToArray());
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using VoidCat.Model.Paywall;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.InMemory;
|
||||
|
||||
public class InMemoryPaywallStore : IPaywallStore
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public InMemoryPaywallStore(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public ValueTask<PaywallConfig?> GetConfig(Guid id)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get(id) as PaywallConfig);
|
||||
}
|
||||
|
||||
public ValueTask SetConfig(Guid id, PaywallConfig config)
|
||||
{
|
||||
_cache.Set(id, config);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask<PaywallOrder?> GetOrder(Guid id)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get(id) as PaywallOrder);
|
||||
}
|
||||
|
||||
public ValueTask SaveOrder(PaywallOrder order)
|
||||
{
|
||||
_cache.Set(order.Id, order,
|
||||
order.Status == PaywallOrderStatus.Paid ? TimeSpan.FromDays(1) : TimeSpan.FromSeconds(5));
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using VoidCat.Model;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.InMemory;
|
||||
|
||||
Reference in New Issue
Block a user