Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/Core.Application/Pipelines/Caching/CacheRemovingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> keysInGroup = JsonSerializer.Deserialize<HashSet<string>>(
Encoding.Default.GetString(cachedGroup)
)!;
foreach (string key in keysInGroup)
var keysInGroup = JsonSerializer.Deserialize<HashSet<string>>(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;
Expand Down
14 changes: 7 additions & 7 deletions src/Core.Application/Pipelines/Caching/CachingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ CancellationToken cancellationToken
byte[]? cachedResponse = await _cache.GetAsync(request.CacheKey, cancellationToken);
if (cachedResponse != null)
{
response = JsonSerializer.Deserialize<TResponse>(Encoding.Default.GetString(cachedResponse))!;
_logger.LogInformation($"Fetched from Cache -> {request.CacheKey}");
response = JsonSerializer.Deserialize<TResponse>(Encoding.UTF8.GetString(cachedResponse))!;
_logger.LogInformation("Fetched from Cache -> {CacheKey}", request.CacheKey);
}
else
response = await getResponseAndAddToCache(request, next, cancellationToken);
Expand All @@ -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);
Expand All @@ -75,7 +75,7 @@ private async Task addCacheKeyToGroup(TRequest request, TimeSpan slidingExpirati
HashSet<string> cacheKeysInGroup;
if (cacheGroupCache != null)
{
cacheKeysInGroup = JsonSerializer.Deserialize<HashSet<string>>(Encoding.Default.GetString(cacheGroupCache))!;
cacheKeysInGroup = JsonSerializer.Deserialize<HashSet<string>>(Encoding.UTF8.GetString(cacheGroupCache))!;
if (!cacheKeysInGroup.Contains(request.CacheKey))
cacheKeysInGroup.Add(request.CacheKey);
}
Expand All @@ -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
Expand All @@ -105,14 +105,14 @@ 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",
serializeCachedGroupSlidingExpirationData,
cacheOptions,
cancellationToken
);
_logger.LogInformation($"Added to Cache -> {request.CacheGroupKey}SlidingExpiration");
_logger.LogInformation("Added to Cache -> {SlidingExpirationKey}", $"{request.CacheGroupKey}SlidingExpiration");
}
}