Add tags to cache entries

This commit is contained in:
Pat Hartl 2025-02-09 01:37:31 -06:00
parent 361b5a5cee
commit 2909f151df
10 changed files with 21 additions and 27 deletions

View file

@ -8,13 +8,7 @@ public static class CacheExtensions
{
if (gameId.HasValue)
{
await cache.ExpireAsync($"/Depot/Results");
await cache.ExpireAsync($"Depot/Games/{gameId}");
await cache.ExpireAsync($"Games");
await cache.ExpireAsync($"Games/{gameId}");
await cache.ExpireAsync($"Games/{gameId}/Manifest");
await cache.ExpireAsync($"Games/{gameId}/Actions");
await cache.ExpireAsync($"Games/{gameId}/Addons");
await cache.RemoveByTagAsync([ $"Games/{gameId}", "Games", "Depot"]);
}
}
}

View file

@ -2,6 +2,7 @@
using LANCommander.Server.Data.Models;
using Microsoft.Extensions.Logging;
using AutoMapper;
using LANCommander.Server.Services.Extensions;
using Microsoft.EntityFrameworkCore;
using ZiggyCreatures.Caching.Fusion;
@ -50,7 +51,7 @@ namespace LANCommander.Server.Services
library.Games.Add(game);
await UpdateAsync(library);
await cache.ExpireAsync($"LibraryGames:{userId}");
await cache.RemoveByTagAsync([$"Library/{userId}"]);
if (game.BaseGame != null && !library.Games.Any(g => g.Id == game.BaseGame.Id) && game.BaseGame.Id != game.Id)
await AddToLibraryAsync(userId, game.BaseGame.Id);
@ -75,7 +76,7 @@ namespace LANCommander.Server.Services
await UpdateAsync(library);
await cache.ExpireAsync($"LibraryGames:{userId}");
await cache.RemoveByTagAsync([$"Library/{userId}"]);
}
}
}

View file

@ -56,8 +56,6 @@ namespace LANCommander.Server.Services
entity.Slug = $"{entity.Slug}-{i}";
entity.Route = RenderRoute(entity);
await cache.ExpireAsync("MappedGames");
return await base.AddAsync(entity);
}
@ -73,7 +71,7 @@ namespace LANCommander.Server.Services
entity.Route = RenderRoute(entity);
await cache.ExpireAsync($"Page|{entity.Route}");
await cache.ExpireAsync($"Page/{entity.Route}");
return await base.UpdateAsync(entity, async context =>
{

View file

@ -63,7 +63,7 @@ namespace LANCommander.Server.Services
_logger.LogError(ex, "Could not get roles for user {Username}", user.UserName);
return new List<Role>();
}
});
}, tags: ["User/Security", "User/Roles", $"User/{user.Id}"]);
return roles;
}

View file

@ -243,7 +243,7 @@ namespace LANCommander.Server.Services
if (existing == null)
{
await cache.ExpireAsync($"{typeof(T).FullName}:Get");
await cache.ExpireAsync($"{typeof(T).FullName}");
entity = await AddAsync(entity);
@ -290,7 +290,7 @@ namespace LANCommander.Server.Services
{
try
{
await cache.ExpireAsync($"{typeof(T).FullName}:Get");
await cache.ExpireAsync($"{typeof(T).FullName}");
using var context = await dbContextFactory.CreateDbContextAsync();

View file

@ -90,7 +90,7 @@ namespace LANCommander.Server.Controllers.Api
return NotFound();
}
var entries = await Cache.GetOrSetAsync<IEnumerable<ArchiveEntry>>($"ArchiveContents:{archive.Id}", async _ =>
var entries = await Cache.GetOrSetAsync<IEnumerable<ArchiveEntry>>($"Archive/{archive.Id}/Contents", async _ =>
{
var filename = await ArchiveService.GetArchiveFileLocationAsync(archive);
@ -117,7 +117,7 @@ namespace LANCommander.Server.Controllers.Api
}
return entries;
}, TimeSpan.MaxValue);
}, TimeSpan.MaxValue, tags: "Archives", $"Archives/{id}");
if (entries.Count() == 0)
return NotFound();

View file

@ -81,7 +81,7 @@ namespace LANCommander.Server.Controllers.Api
};
return results;
}, TimeSpan.MaxValue);
}, TimeSpan.MaxValue, tags: ["Depot"]);
var collections = await UserService.GetCollectionsAsync(user);
@ -129,7 +129,7 @@ namespace LANCommander.Server.Controllers.Api
.Include(g => g.Scripts)
.Include(g => g.Tags)
.GetAsync(id);
});
}, tags: ["Depot", "Depot/Games", "Games", $"Games/{id}"]);
var library = await LibraryService
.AsNoTracking()

View file

@ -60,7 +60,7 @@ namespace LANCommander.Server.Controllers.Api
}).GetAsync<SDK.Models.Game>();
return games;
}, TimeSpan.MaxValue);
}, TimeSpan.MaxValue, tags: ["Games"]);
foreach (var mappedGame in mappedGames)
{
@ -113,7 +113,7 @@ namespace LANCommander.Server.Controllers.Api
.Include(g => g.Redistributables)
.Include(g => g.Tags)
.GetAsync<SDK.Models.Game>(id);
}, TimeSpan.MaxValue);
}, TimeSpan.MaxValue, tags: ["Games", $"Games/{id}"]);
return game;
}
@ -124,7 +124,7 @@ namespace LANCommander.Server.Controllers.Api
var manifest = await Cache.GetOrSetAsync($"Games/{id}/Manifest", async _ =>
{
return await GameService.GetManifestAsync(id);
});
}, tags: ["Games", $"Games/{id}"]);
return manifest;
}
@ -153,7 +153,7 @@ namespace LANCommander.Server.Controllers.Api
actions.AddRange(game.DependentGames.Where(dg => dg.Type == GameType.Expansion || dg.Type == GameType.Mod).SelectMany(dg => dg.Actions));
return Mapper.Map<IEnumerable<SDK.Models.Action>>(actions);
});
}, tags: ["Games", $"Games/{id}"]);
return actions;
}
@ -166,7 +166,7 @@ namespace LANCommander.Server.Controllers.Api
return await GameService
.Include(g => g.Archives)
.GetAsync<SDK.Models.Game>(g => g.BaseGameId == id && (g.Type == GameType.Expansion || g.Type == GameType.Mod));
});
}, tags: ["Games", $"Games/{id}"]);
return addons;
}

View file

@ -57,7 +57,7 @@ namespace LANCommander.Server.Controllers.Api
.ToListAsync();
return Mapper.Map<IEnumerable<SDK.Models.EntityReference>>(games.OrderByTitle(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle));
}, TimeSpan.MaxValue);
}, TimeSpan.MaxValue, tags: ["Library"]);
}
catch (Exception ex)
{

View file

@ -111,8 +111,9 @@
async Task ClearCache()
{
await FusionCache.ExpireAsync("Games");
await FusionCache.ExpireAsync("Depot/Results");
await FusionCache.RemoveByTagAsync("Games");
await FusionCache.RemoveByTagAsync("Depot");
await FusionCache.RemoveByTagAsync("Library");
MessageService.Success("Cache cleared!");
}