Skip to content

Commit

Permalink
fix: add logging to cache invalidator
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD authored and jvandaal committed Sep 27, 2024
1 parent b0fe7e3 commit 1af3e49
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

public interface IRedisCacheInvalidateService
{
Task Invalidate(IEnumerable<BuildingPersistentLocalId> buildingPersistentLocalIds);
Task Invalidate(ICollection<BuildingPersistentLocalId> buildingPersistentLocalIds);
}

internal sealed class RedisCacheInvalidateService: IRedisCacheInvalidateService
{
private readonly string[] _cacheKeyFormats;
private readonly string _connectionString;
private readonly ILogger<RedisCacheInvalidateService> _logger;

public RedisCacheInvalidateService(IConfiguration configuration)
public RedisCacheInvalidateService(IConfiguration configuration, ILoggerFactory loggerFactory)
{
_cacheKeyFormats = configuration.GetSection("RedisCacheKeyFormats")
.GetChildren()
Expand All @@ -34,10 +36,13 @@ public RedisCacheInvalidateService(IConfiguration configuration)

_connectionString = configuration.GetConnectionString("LastChangedList")
?? throw new ArgumentException("No connectionstring 'LastChangedList' found");
_logger = loggerFactory.CreateLogger<RedisCacheInvalidateService>();
}

public async Task Invalidate(IEnumerable<BuildingPersistentLocalId> buildingPersistentLocalIds)
public async Task Invalidate(ICollection<BuildingPersistentLocalId> buildingPersistentLocalIds)
{
_logger.LogInformation("Invalidating cache for {BuildingPersistentLocalIds}", buildingPersistentLocalIds.Count());

await using var connection = new SqlConnection(_connectionString);
connection.Open();

Expand All @@ -54,7 +59,10 @@ UPDATE [Redis].[LastChangedList]
SET [LastPopulatedPosition] = -1
WHERE [CacheKey] IN ('{string.Join("','", batchedCacheKeys)}')
""";
connection.Execute(query);

_logger.LogInformation("Executing Query: {Query}", query);
var result = connection.Execute(query);
_logger.LogInformation("Query executed, affected rows: {Result}", result);
}
}
}
Expand Down

0 comments on commit 1af3e49

Please sign in to comment.