mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-11-15 08:56:27 +01:00
Add strike paywall (incomplete)
This commit is contained in:
37
VoidCat/Services/InMemory/InMemoryPaywallStore.cs
Normal file
37
VoidCat/Services/InMemory/InMemoryPaywallStore.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
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);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
52
VoidCat/Services/InMemory/InMemoryStatsController.cs
Normal file
52
VoidCat/Services/InMemory/InMemoryStatsController.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.InMemory;
|
||||
|
||||
public class InMemoryStatsController : IStatsCollector, IStatsReporter
|
||||
{
|
||||
private static readonly Guid Global = new Guid("{A98DFDCC-C4E1-4D42-B818-912086FC6157}");
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public InMemoryStatsController(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public ValueTask TrackIngress(Guid id, ulong amount)
|
||||
{
|
||||
Incr(IngressKey(id), amount);
|
||||
Incr(IngressKey(Global), amount);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask TrackEgress(Guid id, ulong amount)
|
||||
{
|
||||
Incr(EgressKey(id), amount);
|
||||
Incr(EgressKey(Global), amount);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask<Bandwidth> GetBandwidth()
|
||||
=> ValueTask.FromResult(GetBandwidthInternal(Global));
|
||||
|
||||
public ValueTask<Bandwidth> GetBandwidth(Guid id)
|
||||
=> ValueTask.FromResult(GetBandwidthInternal(id));
|
||||
|
||||
private Bandwidth GetBandwidthInternal(Guid id)
|
||||
{
|
||||
var i = _cache.Get(IngressKey(id)) as ulong?;
|
||||
var o = _cache.Get(EgressKey(id)) as ulong?;
|
||||
return new(i ?? 0UL, o ?? 0UL);
|
||||
}
|
||||
|
||||
private void Incr(string k, ulong amount)
|
||||
{
|
||||
ulong v;
|
||||
_cache.TryGetValue(k, out v);
|
||||
_cache.Set(k, v + amount);
|
||||
}
|
||||
|
||||
private string IngressKey(Guid id) => $"stats:ingress:{id}";
|
||||
private string EgressKey(Guid id) => $"stats:egress:{id}";
|
||||
}
|
||||
Reference in New Issue
Block a user