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;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Services;
|
|
|
|
|
|
|
|
public class KeyCacheService(DatabaseContext db, IClock clock, ILogger logger)
|
|
|
|
{
|
2024-09-04 14:25:44 +02:00
|
|
|
private readonly ILogger _logger = logger.ForContext<KeyCacheService>();
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public Task SetKeyAsync(
|
|
|
|
string key,
|
|
|
|
string value,
|
|
|
|
Duration expireAfter,
|
|
|
|
CancellationToken ct = default
|
|
|
|
) => SetKeyAsync(key, value, clock.GetCurrentInstant() + expireAfter, ct);
|
2024-06-12 03:47:20 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task SetKeyAsync(
|
|
|
|
string key,
|
|
|
|
string value,
|
|
|
|
Instant expires,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
db.TemporaryKeys.Add(
|
|
|
|
new TemporaryKey
|
|
|
|
{
|
|
|
|
Expires = expires,
|
|
|
|
Key = key,
|
|
|
|
Value = value,
|
|
|
|
}
|
|
|
|
);
|
2024-09-09 14:37:59 +02:00
|
|
|
await db.SaveChangesAsync(ct);
|
2024-06-12 03:47:20 +02:00
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task<string?> GetKeyAsync(
|
|
|
|
string key,
|
|
|
|
bool delete = false,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
TemporaryKey? value = await db.TemporaryKeys.FirstOrDefaultAsync(k => k.Key == key, ct);
|
2024-10-02 00:28:07 +02:00
|
|
|
if (value == null)
|
|
|
|
return null;
|
2024-06-12 03:47:20 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
if (delete)
|
|
|
|
await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(ct);
|
2024-06-12 03:47:20 +02:00
|
|
|
|
|
|
|
return value.Value;
|
|
|
|
}
|
|
|
|
|
2024-09-10 02:39:07 +02:00
|
|
|
public async Task DeleteKeyAsync(string key, CancellationToken ct = default) =>
|
|
|
|
await db.TemporaryKeys.Where(k => k.Key == key).ExecuteDeleteAsync(ct);
|
|
|
|
|
2024-09-04 01:46:39 +02:00
|
|
|
public async Task DeleteExpiredKeysAsync(CancellationToken ct)
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
int count = await db
|
2024-10-02 00:28:07 +02:00
|
|
|
.TemporaryKeys.Where(k => k.Expires < clock.GetCurrentInstant())
|
|
|
|
.ExecuteDeleteAsync(ct);
|
|
|
|
if (count != 0)
|
|
|
|
_logger.Information("Removed {Count} expired keys from the database", count);
|
2024-06-12 03:47:20 +02:00
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public Task SetKeyAsync<T>(
|
|
|
|
string key,
|
|
|
|
T obj,
|
|
|
|
Duration expiresAt,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
|
|
|
where T : class => SetKeyAsync(key, obj, clock.GetCurrentInstant() + expiresAt, ct);
|
2024-06-13 02:23:55 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task SetKeyAsync<T>(
|
|
|
|
string key,
|
|
|
|
T obj,
|
|
|
|
Instant expires,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
|
|
|
where T : class
|
2024-06-12 03:47:20 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string value = JsonConvert.SerializeObject(obj);
|
2024-09-09 14:37:59 +02:00
|
|
|
await SetKeyAsync(key, value, expires, ct);
|
2024-06-12 03:47:20 +02:00
|
|
|
}
|
2024-06-12 16:19:49 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task<T?> GetKeyAsync<T>(
|
|
|
|
string key,
|
|
|
|
bool delete = false,
|
|
|
|
CancellationToken ct = default
|
|
|
|
)
|
2024-09-10 02:39:07 +02:00
|
|
|
where T : class
|
2024-06-12 16:19:49 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string? value = await GetKeyAsync(key, delete, ct);
|
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
|
|
|
}
|