Initial implementation of voice works scanning.
This commit is contained in:
55
JSMR.Infrastructure/Caching/ICacheObject.cs
Normal file
55
JSMR.Infrastructure/Caching/ICacheObject.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using JSMR.Application.Common.Caching;
|
||||
using JSMR.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace JSMR.Infrastructure.Caching;
|
||||
|
||||
public interface ICacheObject<T>
|
||||
{
|
||||
Task<T> GetAsync(CancellationToken cancellationToken = default);
|
||||
Task<T> RefreshAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public abstract class CacheObject<T>(ICache cache) : ICacheObject<T>
|
||||
{
|
||||
protected abstract string Key { get; }
|
||||
protected abstract Task<T> FetchAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
protected virtual CacheEntryOptions Options => new() { SlidingExpiration = TimeSpan.FromHours(1) };
|
||||
|
||||
public async virtual Task<T> GetAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await cache.GetAsync<T>(Key, cancellationToken) ?? await RefreshAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async virtual Task<T> RefreshAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
T cacheObject = await FetchAsync(cancellationToken);
|
||||
|
||||
await cache.SetAsync(Key, cacheObject, Options, cancellationToken);
|
||||
|
||||
return cacheObject;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISpamCircleCache : ICacheObject<string[]>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class SpamCircleCache(IDbContextFactory<AppDbContext> contextFactory, ICache cache) : CacheObject<string[]>(cache), ISpamCircleCache
|
||||
{
|
||||
protected override string Key => "SpamCircles";
|
||||
|
||||
protected override async Task<string[]> FetchAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var context = contextFactory.CreateDbContext();
|
||||
|
||||
return await context.Circles
|
||||
.AsNoTracking()
|
||||
.Where(circle => circle.Spam)
|
||||
.Select(circle => circle.MakerId)
|
||||
.Distinct()
|
||||
.ToArrayAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user