diff --git a/src/Core.Application/Pipelines/Caching/CacheRemovingBehavior.cs b/src/Core.Application/Pipelines/Caching/CacheRemovingBehavior.cs index 397f0b7..e81a9f7 100644 --- a/src/Core.Application/Pipelines/Caching/CacheRemovingBehavior.cs +++ b/src/Core.Application/Pipelines/Caching/CacheRemovingBehavior.cs @@ -29,32 +29,34 @@ CancellationToken cancellationToken TResponse response = await next(); - if (request.CacheGroupKey != null) - for (int i = 0; i < request.CacheGroupKey.Count(); i++) + if (request.CacheGroupKey?.Any() == true) + foreach (var groupKey in request.CacheGroupKey) { - byte[]? cachedGroup = await _cache.GetAsync(request.CacheGroupKey[i], cancellationToken); + byte[]? cachedGroup = await _cache.GetAsync(groupKey, cancellationToken); if (cachedGroup != null) { - HashSet keysInGroup = JsonSerializer.Deserialize>( - Encoding.Default.GetString(cachedGroup) - )!; - foreach (string key in keysInGroup) + var keysInGroup = JsonSerializer.Deserialize>(Encoding.UTF8.GetString(cachedGroup)); + if (keysInGroup != null) { - await _cache.RemoveAsync(key, cancellationToken); - _logger.LogInformation($"Removed Cache -> {key}"); + foreach (var key in keysInGroup) + { + await _cache.RemoveAsync(key, cancellationToken); + _logger.LogInformation("Removed Cache -> {Key}", key); + } } - await _cache.RemoveAsync(request.CacheGroupKey[i], cancellationToken); - _logger.LogInformation($"Removed Cache -> {request.CacheGroupKey}"); - await _cache.RemoveAsync(key: $"{request.CacheGroupKey}SlidingExpiration", cancellationToken); - _logger.LogInformation($"Removed Cache -> {request.CacheGroupKey}SlidingExpiration"); + await _cache.RemoveAsync(groupKey, cancellationToken); + _logger.LogInformation("Removed Cache Group -> {GroupKey}", groupKey); + + await _cache.RemoveAsync($"{groupKey}SlidingExpiration", cancellationToken); + _logger.LogInformation("Removed Cache -> {Key}", $"{groupKey}SlidingExpiration"); } } - if (request.CacheKey != null) + if (!string.IsNullOrWhiteSpace(request.CacheKey)) { await _cache.RemoveAsync(request.CacheKey, cancellationToken); - _logger.LogInformation($"Removed Cache -> {request.CacheKey}"); + _logger.LogInformation("Removed Cache -> {Key}", request.CacheKey); } return response; diff --git a/src/Core.Application/Pipelines/Caching/CachingBehavior.cs b/src/Core.Application/Pipelines/Caching/CachingBehavior.cs index ea8f97a..4dc5487 100644 --- a/src/Core.Application/Pipelines/Caching/CachingBehavior.cs +++ b/src/Core.Application/Pipelines/Caching/CachingBehavior.cs @@ -39,8 +39,8 @@ CancellationToken cancellationToken byte[]? cachedResponse = await _cache.GetAsync(request.CacheKey, cancellationToken); if (cachedResponse != null) { - response = JsonSerializer.Deserialize(Encoding.Default.GetString(cachedResponse))!; - _logger.LogInformation($"Fetched from Cache -> {request.CacheKey}"); + response = JsonSerializer.Deserialize(Encoding.UTF8.GetString(cachedResponse))!; + _logger.LogInformation("Fetched from Cache -> {CacheKey}", request.CacheKey); } else response = await getResponseAndAddToCache(request, next, cancellationToken); @@ -61,7 +61,7 @@ CancellationToken cancellationToken byte[] serializeData = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(response)); await _cache.SetAsync(request.CacheKey, serializeData, cacheOptions, cancellationToken); - _logger.LogInformation($"Added to Cache -> {request.CacheKey}"); + _logger.LogInformation("Added to Cache -> {CacheKey}", request.CacheKey); if (request.CacheGroupKey != null) await addCacheKeyToGroup(request, slidingExpiration, cancellationToken); @@ -75,7 +75,7 @@ private async Task addCacheKeyToGroup(TRequest request, TimeSpan slidingExpirati HashSet cacheKeysInGroup; if (cacheGroupCache != null) { - cacheKeysInGroup = JsonSerializer.Deserialize>(Encoding.Default.GetString(cacheGroupCache))!; + cacheKeysInGroup = JsonSerializer.Deserialize>(Encoding.UTF8.GetString(cacheGroupCache))!; if (!cacheKeysInGroup.Contains(request.CacheKey)) cacheKeysInGroup.Add(request.CacheKey); } @@ -90,7 +90,7 @@ private async Task addCacheKeyToGroup(TRequest request, TimeSpan slidingExpirati int? cacheGroupCacheSlidingExpirationValue = null; if (cacheGroupCacheSlidingExpirationCache != null) cacheGroupCacheSlidingExpirationValue = Convert.ToInt32( - Encoding.Default.GetString(cacheGroupCacheSlidingExpirationCache) + Encoding.UTF8.GetString(cacheGroupCacheSlidingExpirationCache) ); if ( cacheGroupCacheSlidingExpirationValue == null @@ -105,7 +105,7 @@ private async Task addCacheKeyToGroup(TRequest request, TimeSpan slidingExpirati new() { SlidingExpiration = TimeSpan.FromSeconds(Convert.ToDouble(cacheGroupCacheSlidingExpirationValue)) }; await _cache.SetAsync(key: request.CacheGroupKey!, newCacheGroupCache, cacheOptions, cancellationToken); - _logger.LogInformation($"Added to Cache -> {request.CacheGroupKey}"); + _logger.LogInformation("Added to Cache -> {CacheGroupKey}", request.CacheGroupKey); await _cache.SetAsync( key: $"{request.CacheGroupKey}SlidingExpiration", @@ -113,6 +113,6 @@ await _cache.SetAsync( cacheOptions, cancellationToken ); - _logger.LogInformation($"Added to Cache -> {request.CacheGroupKey}SlidingExpiration"); + _logger.LogInformation("Added to Cache -> {SlidingExpirationKey}", $"{request.CacheGroupKey}SlidingExpiration"); } }