Added logging.

This commit is contained in:
2025-10-20 23:32:38 -04:00
parent e0e8945728
commit 3a115bc7b8
18 changed files with 381 additions and 64 deletions

View File

@@ -1,31 +1,32 @@
using JSMR.Application.Common.Caching;
using JSMR.Application.Common.Search;
using JSMR.Application.Common.Search;
using JSMR.Application.Creators.Queries.Search.Contracts;
using JSMR.Application.Creators.Queries.Search.Ports;
using JSMR.Application.Logging;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
namespace JSMR.Application.Creators.Queries.Search;
public sealed class SearchCreatorsHandler(ICreatorSearchProvider searchProvider, ICache cache)
public sealed class SearchCreatorsHandler(ICreatorSearchProvider searchProvider, ILogger<SearchCreatorsHandler> logger)
{
public async Task<SearchCreatorsResponse> HandleAsync(SearchCreatorsRequest request, CancellationToken cancellationToken)
{
SearchOptions<CreatorSearchCriteria, CreatorSortField> searchOptions = request.Options;
string cacheKey = $"creator:{searchOptions.GetHashCode()}";
CreatorSearchResults? cachedResults = await cache.GetAsync<CreatorSearchResults>(cacheKey, cancellationToken);
if (cachedResults != null)
return new SearchCreatorsResponse(cachedResults);
Stopwatch stopWatch = Stopwatch.StartNew();
SearchResult<CreatorSearchItem> results = await searchProvider.SearchAsync(searchOptions, cancellationToken);
long elapsedMilliseconds = stopWatch.ElapsedMilliseconds;
CacheEntryOptions cacheEntryOptions = new()
{
SlidingExpiration = TimeSpan.FromMinutes(10)
};
await cache.SetAsync(cacheKey, results, cacheEntryOptions, cancellationToken);
LogEvents.SearchCompleted(
logger,
Elapsed: elapsedMilliseconds,
Items: results.Items.Length,
Total: results.TotalItems,
Page: searchOptions.PageNumber,
Size: searchOptions.PageSize,
Sort: searchOptions.SortOptions.ToLogObject(),
Criteria: searchOptions.Criteria.ToLogObject()
);
return new SearchCreatorsResponse(results);
}