mirror of
https://git.v0l.io/Kieran/void.cat.git
synced 2025-11-15 08:46:26 +01:00
Postgres paywall support
Various fixes
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Newtonsoft.Json;
|
||||
using VoidCat.Services.Abstractions;
|
||||
|
||||
namespace VoidCat.Services.InMemory;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class InMemoryCache : ICache
|
||||
{
|
||||
private readonly IMemoryCache _cache;
|
||||
@@ -12,30 +14,38 @@ public class InMemoryCache : ICache
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask<T?> Get<T>(string key)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<T?>(key));
|
||||
var json = _cache.Get<string>(key);
|
||||
if (string.IsNullOrEmpty(json)) return default;
|
||||
|
||||
return ValueTask.FromResult(JsonConvert.DeserializeObject<T?>(json));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask Set<T>(string key, T value, TimeSpan? expire = null)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(value);
|
||||
if (expire.HasValue)
|
||||
{
|
||||
_cache.Set(key, value, expire.Value);
|
||||
_cache.Set(key, json, expire.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
_cache.Set(key, value);
|
||||
_cache.Set(key, json);
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask<string[]> GetList(string key)
|
||||
{
|
||||
return ValueTask.FromResult(_cache.Get<string[]>(key) ?? Array.Empty<string>());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask AddToList(string key, string value)
|
||||
{
|
||||
var list = new HashSet<string>(GetList(key).Result);
|
||||
@@ -44,6 +54,7 @@ public class InMemoryCache : ICache
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask RemoveFromList(string key, string value)
|
||||
{
|
||||
var list = new HashSet<string>(GetList(key).Result);
|
||||
@@ -52,10 +63,10 @@ public class InMemoryCache : ICache
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask Delete(string key)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
return ValueTask.CompletedTask;
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user