2024-10-14 14:56:40 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Database.Redis;
|
|
|
|
|
|
|
|
|
|
public class RedisService(Config config)
|
|
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
private readonly ConnectionMultiplexer _multiplexer = ConnectionMultiplexer.Connect(
|
|
|
|
|
config.Database.Redis!
|
|
|
|
|
);
|
2024-08-16 22:28:05 +02:00
|
|
|
|
2024-11-19 00:10:12 +01:00
|
|
|
private readonly JsonSerializerOptions _options = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
|
|
|
|
};
|
2024-08-19 16:12:28 +02:00
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
public IDatabase GetDatabase(int db = -1) => _multiplexer.GetDatabase(db);
|
|
|
|
|
|
2024-10-18 22:13:23 +02:00
|
|
|
public async Task SetStringAsync(string key, string value, TimeSpan? expiry = null) =>
|
|
|
|
|
await GetDatabase().StringSetAsync(key, value, expiry);
|
|
|
|
|
|
|
|
|
|
public async Task<string?> GetStringAsync(string key, bool delete = false)
|
|
|
|
|
{
|
|
|
|
|
var db = GetDatabase();
|
|
|
|
|
return delete ? await db.StringGetDeleteAsync(key) : await db.StringGetAsync(key);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null)
|
|
|
|
|
{
|
2024-08-19 16:12:28 +02:00
|
|
|
var json = JsonSerializer.Serialize(value, _options);
|
2024-08-16 22:28:05 +02:00
|
|
|
await GetDatabase().StringSetAsync(key, json, expiry);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 17:12:00 +01:00
|
|
|
public async Task DeleteAsync(string[] keys) =>
|
|
|
|
|
await GetDatabase().KeyDeleteAsync(keys.Select(k => new RedisKey(k)).ToArray());
|
|
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
public async Task<T?> GetAsync<T>(string key)
|
|
|
|
|
{
|
|
|
|
|
var value = await GetDatabase().StringGetAsync(key);
|
2024-08-19 16:12:28 +02:00
|
|
|
return value.IsNull ? default : JsonSerializer.Deserialize<T>(value!, _options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SetHashAsync<T>(string hashKey, string fieldKey, T value)
|
|
|
|
|
{
|
|
|
|
|
var json = JsonSerializer.Serialize(value, _options);
|
|
|
|
|
await GetDatabase().HashSetAsync(hashKey, fieldKey, json);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public async Task SetHashAsync<T>(
|
|
|
|
|
string hashKey,
|
|
|
|
|
IEnumerable<T> values,
|
|
|
|
|
Func<T, string> keySelector
|
|
|
|
|
)
|
2024-08-19 16:12:28 +02:00
|
|
|
{
|
|
|
|
|
var hashEntries = values
|
2024-10-09 17:35:11 +02:00
|
|
|
.Select(v => new
|
|
|
|
|
{
|
|
|
|
|
Key = keySelector(v),
|
|
|
|
|
Value = JsonSerializer.Serialize(v, _options),
|
|
|
|
|
})
|
2024-08-19 16:12:28 +02:00
|
|
|
.Select(v => new HashEntry(v.Key, v.Value));
|
|
|
|
|
await GetDatabase().HashSetAsync(hashKey, hashEntries.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<T?> GetHashAsync<T>(string hashKey, string fieldKey)
|
|
|
|
|
{
|
|
|
|
|
var value = await GetDatabase().HashGetAsync(hashKey, fieldKey);
|
|
|
|
|
return value.IsNull ? default : JsonSerializer.Deserialize<T>(value!, _options);
|
2024-08-16 22:28:05 +02:00
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|