2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// 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-06-12 03:47:20 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-13 02:23:55 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-06-12 03:47:20 +02:00
|
|
|
using NodaTime;
|
2025-03-04 17:03:39 +01:00
|
|
|
using StackExchange.Redis;
|
2024-06-12 03:47:20 +02:00
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Services;
|
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public class KeyCacheService(Config config)
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2025-03-04 17:03:39 +01:00
|
|
|
public ConnectionMultiplexer Multiplexer { get; } =
|
2025-03-04 17:25:07 +01:00
|
|
|
ConnectionMultiplexer.Connect(config.Database.Redis);
|
2024-09-04 14:25:44 +02:00
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public async Task SetKeyAsync(string key, string value, Duration expireAfter) =>
|
|
|
|
await Multiplexer
|
|
|
|
.GetDatabase()
|
|
|
|
.StringSetAsync(key, value, expiry: expireAfter.ToTimeSpan());
|
2024-06-12 03:47:20 +02:00
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public async Task<string?> GetKeyAsync(string key, bool delete = false) =>
|
|
|
|
delete
|
|
|
|
? await Multiplexer.GetDatabase().StringGetDeleteAsync(key)
|
|
|
|
: await Multiplexer.GetDatabase().StringGetAsync(key);
|
2024-06-12 03:47:20 +02:00
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public async Task DeleteKeyAsync(string key) =>
|
|
|
|
await Multiplexer.GetDatabase().KeyDeleteAsync(key);
|
2024-06-12 03:47:20 +02:00
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public async Task SetKeyAsync<T>(string key, T obj, Duration expiresAt)
|
2024-10-02 00:28:07 +02:00
|
|
|
where T : class
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string value = JsonConvert.SerializeObject(obj);
|
2025-03-04 17:03:39 +01:00
|
|
|
await SetKeyAsync(key, value, expiresAt);
|
2024-06-12 03:47:20 +02:00
|
|
|
}
|
2024-06-12 16:19:49 +02:00
|
|
|
|
2025-03-04 17:03:39 +01:00
|
|
|
public async Task<T?> GetKeyAsync<T>(string key, bool delete = false)
|
2024-09-10 02:39:07 +02:00
|
|
|
where T : class
|
2024-06-12 16:19:49 +02:00
|
|
|
{
|
2025-03-04 17:03:39 +01:00
|
|
|
string? value = await GetKeyAsync(key, delete);
|
2024-06-13 02:23:55 +02:00
|
|
|
return value == null ? default : JsonConvert.DeserializeObject<T>(value);
|
2024-06-12 16:19:49 +02:00
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|