Skip to content

Commit 175d6fa

Browse files
authored
change to how the scoped factory is created (#10)
1 parent c3bc020 commit 175d6fa

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/OLT.Extensions.Caching.Memory/Services/OltHybridCache.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace OLT.Core;
66

77
public class OltHybridCache : IOltHybridCache
88
{
9-
private readonly IServiceScopeFactory _serviceScopeFactory;
9+
private readonly IServiceScopeFactory _scopeFactory;
1010
private readonly IFusionCache _cache;
1111

12-
public OltHybridCache(IFusionCache cache, IServiceScopeFactory serviceScopeFactory)
12+
public OltHybridCache(IFusionCache cache, IServiceScopeFactory scopeFactory)
1313
{
1414
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
15-
_serviceScopeFactory = serviceScopeFactory;
15+
_scopeFactory = scopeFactory;
1616
}
1717

1818
/// <summary>
@@ -58,14 +58,14 @@ public async Task<TValue> GetOrSetAsync<TValue>(string key, Func<IServiceProvide
5858

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

61-
await using var scope = _serviceScopeFactory.CreateAsyncScope();
62-
63-
//Use scoped IFusionCache here
64-
var cache = scope.ServiceProvider.GetRequiredService<IFusionCache>();
6561

6662
return await _cache.GetOrSetAsync<TValue>(
6763
key,
68-
async (ctx, ct) => await factory(scope.ServiceProvider, new OltHybridCacheContext<TValue>(ctx, ct)),
64+
async (ctx, ct) =>
65+
{
66+
await using var scope = _scopeFactory.CreateAsyncScope();
67+
return await factory(scope.ServiceProvider, new OltHybridCacheContext<TValue>(ctx, ct));
68+
},
6969
options,
7070
cancellationToken);
7171

0 commit comments

Comments
 (0)