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:
46
VoidCat/Services/Redis/RedisPaywallStore.cs
Normal file
46
VoidCat/Services/Redis/RedisPaywallStore.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
using VoidCat.Model.Paywall;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.Redis;
|
||||
|
||||
public class RedisPaywallStore : IPaywallStore
|
||||
{
|
||||
private readonly IDatabase _database;
|
||||
|
||||
public RedisPaywallStore(IDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async ValueTask<PaywallConfig?> GetConfig(Guid id)
|
||||
{
|
||||
var json = await _database.StringGetAsync(ConfigKey(id));
|
||||
var cfg = JsonConvert.DeserializeObject<PaywallConfig>(json);
|
||||
return cfg?.Service switch
|
||||
{
|
||||
PaywallServices.Strike => JsonConvert.DeserializeObject<StrikePaywallConfig>(json),
|
||||
_ => default
|
||||
};
|
||||
}
|
||||
|
||||
public async ValueTask SetConfig(Guid id, PaywallConfig config)
|
||||
{
|
||||
await _database.StringSetAsync(ConfigKey(id), JsonConvert.SerializeObject(config));
|
||||
}
|
||||
|
||||
public async ValueTask<PaywallOrder?> GetOrder(Guid id)
|
||||
{
|
||||
var json = await _database.StringGetAsync(OrderKey(id));
|
||||
return JsonConvert.DeserializeObject<PaywallOrder>(json);
|
||||
}
|
||||
|
||||
public async ValueTask SaveOrder(PaywallOrder order)
|
||||
{
|
||||
await _database.StringSetAsync(OrderKey(order.Id), JsonConvert.SerializeObject(order));
|
||||
}
|
||||
|
||||
private RedisKey ConfigKey(Guid id) => $"paywall:config:{id}";
|
||||
private RedisKey OrderKey(Guid id) => $"paywall:order:{id}";
|
||||
}
|
||||
51
VoidCat/Services/Redis/RedisStatsController.cs
Normal file
51
VoidCat/Services/Redis/RedisStatsController.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using StackExchange.Redis;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.Redis;
|
||||
|
||||
public class RedisStatsController : IStatsReporter, IStatsCollector
|
||||
{
|
||||
private const string GlobalEgress = "stats:egress:global";
|
||||
private const string GlobalIngress = "stats:ingress:global";
|
||||
private readonly IDatabase _redis;
|
||||
|
||||
public RedisStatsController(IDatabase redis)
|
||||
{
|
||||
_redis = redis;
|
||||
}
|
||||
|
||||
public async ValueTask<Bandwidth> GetBandwidth()
|
||||
{
|
||||
var egress = _redis.StringGetAsync(GlobalEgress);
|
||||
var ingress = _redis.StringGetAsync(GlobalIngress);
|
||||
await Task.WhenAll(egress, ingress);
|
||||
|
||||
return new((ulong)ingress.Result, (ulong)egress.Result);
|
||||
}
|
||||
|
||||
public async ValueTask<Bandwidth> GetBandwidth(Guid id)
|
||||
{
|
||||
var egress = _redis.StringGetAsync(formatEgressKey(id));
|
||||
var ingress = _redis.StringGetAsync(formatIngressKey(id));
|
||||
await Task.WhenAll(egress, ingress);
|
||||
|
||||
return new((ulong)ingress.Result, (ulong)egress.Result);
|
||||
}
|
||||
|
||||
public async ValueTask TrackIngress(Guid id, ulong amount)
|
||||
{
|
||||
await Task.WhenAll(
|
||||
_redis.StringIncrementAsync(GlobalIngress, amount),
|
||||
_redis.StringIncrementAsync(formatIngressKey(id), amount));
|
||||
}
|
||||
|
||||
public async ValueTask TrackEgress(Guid id, ulong amount)
|
||||
{
|
||||
await Task.WhenAll(
|
||||
_redis.StringIncrementAsync(GlobalEgress, amount),
|
||||
_redis.StringIncrementAsync(formatEgressKey(id), amount));
|
||||
}
|
||||
|
||||
private RedisKey formatIngressKey(Guid id) => $"stats:{id}:ingress";
|
||||
private RedisKey formatEgressKey(Guid id) => $"stats:{id}:egress";
|
||||
}
|
||||
Reference in New Issue
Block a user