Update spam circle cache after updating circle status.

This commit is contained in:
2025-09-11 00:26:11 -04:00
parent 3c0a39b324
commit 39274165cb
10 changed files with 70 additions and 37 deletions

View File

@@ -4,12 +4,6 @@ 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; }
@@ -30,26 +24,4 @@ public abstract class CacheObject<T>(ICache cache) : ICacheObject<T>
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);
}
}

View File

@@ -0,0 +1,23 @@
using JSMR.Application.Common.Caching;
using JSMR.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace JSMR.Infrastructure.Caching;
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);
}
}