24 lines
No EOL
763 B
C#
24 lines
No EOL
763 B
C#
using System.Text.Json;
|
|
using Humanizer;
|
|
using StackExchange.Redis;
|
|
|
|
namespace Catalogger.Backend.Database.Redis;
|
|
|
|
public class RedisService(Config config)
|
|
{
|
|
private readonly ConnectionMultiplexer _multiplexer = ConnectionMultiplexer.Connect(config.Database.Redis!);
|
|
|
|
public IDatabase GetDatabase(int db = -1) => _multiplexer.GetDatabase(db);
|
|
|
|
public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null)
|
|
{
|
|
var json = JsonSerializer.Serialize(value);
|
|
await GetDatabase().StringSetAsync(key, json, expiry);
|
|
}
|
|
|
|
public async Task<T?> GetAsync<T>(string key)
|
|
{
|
|
var value = await GetDatabase().StringGetAsync(key);
|
|
return value.IsNull ? default : JsonSerializer.Deserialize<T>(value!);
|
|
}
|
|
} |