[backend/core] Allow manual resetting of the home timeline heuristic

This commit is contained in:
Laura Hausmann 2024-10-24 16:48:06 +02:00
parent f19a414b27
commit e4074dddbc
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 10 additions and 2 deletions

View file

@ -11,6 +11,8 @@ public static class QueryableTimelineExtensions
// Determined empirically in 2023. Ask zotan for the spreadsheet if you're curious.
private const int Cutoff = 250;
private const string Prefix = "following-query-heuristic";
public static IQueryable<Note> FilterByFollowingAndOwn(
this IQueryable<Note> query, User user, DatabaseContext db, int heuristic
)
@ -27,10 +29,14 @@ public static class QueryableTimelineExtensions
.Concat(new[] { user.Id })
.Contains(note.UserId));
public static async Task ResetHeuristic(User user, CacheService cache)
{
await cache.ClearAsync($"{Prefix}:{user.Id}");
}
public static async Task<int> GetHeuristic(User user, DatabaseContext db, CacheService cache)
{
return await cache.FetchValueAsync($"following-query-heuristic:{user.Id}",
TimeSpan.FromHours(24), FetchHeuristic);
return await cache.FetchValueAsync($"{Prefix}:{user.Id}", TimeSpan.FromHours(24), FetchHeuristic);
[SuppressMessage("ReSharper", "EntityFramework.UnsupportedServerSideFunctionCall")]
async Task<int> FetchHeuristic()

View file

@ -119,6 +119,8 @@ public class CacheService([FromKeyedServices("cache")] DatabaseContext db)
public async Task<T> FetchValueAsync<T>(string key, TimeSpan ttl, Func<T> fetcher, bool renew = false)
where T : struct => await FetchValueAsync(key, ttl, () => Task.FromResult(fetcher()), renew);
public async Task ClearAsync(string key) => await db.CacheStore.Where(p => p.Key == key).ExecuteDeleteAsync();
private async Task<string?> GetValueAsync(string key)
{
return await db.CacheStore