Skip to content
Merged
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
16 changes: 8 additions & 8 deletions src/OLT.Extensions.Caching.Memory/Services/OltHybridCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class OltHybridCache : IOltHybridCache
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IServiceScopeFactory _scopeFactory;
private readonly IFusionCache _cache;

public OltHybridCache(IFusionCache cache, IServiceScopeFactory serviceScopeFactory)
public OltHybridCache(IFusionCache cache, IServiceScopeFactory scopeFactory)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_serviceScopeFactory = serviceScopeFactory;
_scopeFactory = scopeFactory;
}

/// <summary>
Expand All @@ -29,7 +29,7 @@
public async Task SetAsync<TValue>(string key, TValue value, TimeSpan? duration = null, CacheItemPriority? cacheItemPriority = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Cache key cannot be null or empty.", nameof(key));

Check warning on line 32 in src/OLT.Extensions.Caching.Memory/Services/OltHybridCache.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'Cache key cannot be null or empty.' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

var options = duration.HasValue || cacheItemPriority.HasValue ? _cache.CreateEntryOptions() : _cache.DefaultEntryOptions;

Expand Down Expand Up @@ -58,14 +58,14 @@

var options = duration.HasValue ? _cache.CreateEntryOptions(duration: duration) : _cache.DefaultEntryOptions;

await using var scope = _serviceScopeFactory.CreateAsyncScope();

//Use scoped IFusionCache here
var cache = scope.ServiceProvider.GetRequiredService<IFusionCache>();

return await _cache.GetOrSetAsync<TValue>(
key,
async (ctx, ct) => await factory(scope.ServiceProvider, new OltHybridCacheContext<TValue>(ctx, ct)),
async (ctx, ct) =>
{
await using var scope = _scopeFactory.CreateAsyncScope();
return await factory(scope.ServiceProvider, new OltHybridCacheContext<TValue>(ctx, ct));
},
options,
cancellationToken);

Expand Down
Loading