Skip to content

Commit 289d384

Browse files
committed
Publish EFCoreSecondLevelCacheInterceptor.HybridCache, Close #292
1 parent 0ec384a commit 289d384

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/EFCoreSecondLevelCacheInterceptor.HybridCache/EFHybridCacheDependenciesStore.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ public void AddCacheDependencies(IEnumerable<string>? tags)
2727
{
2828
tags ??= [];
2929

30-
// NOTE: hybridCache doesn't have a synchronous API!! So ... we will wait!
31-
32-
var currentTags = await hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey,
33-
factory => ValueTask.FromResult<List<string>>([]));
30+
var currentTags = hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey,
31+
factory => ValueTask.FromResult<List<string>>([]))
32+
.Preserve()
33+
.GetAwaiter()
34+
.GetResult();
3435

3536
foreach (var tag in tags)
3637
{
@@ -40,7 +41,7 @@ public void AddCacheDependencies(IEnumerable<string>? tags)
4041
}
4142
}
4243

43-
hybridCache.SetAsync(TagsCacheKey, currentTags, Options);
44+
hybridCache.SetAsync(TagsCacheKey, currentTags, Options).Preserve().GetAwaiter().GetResult();
4445
}
4546

4647
/// <summary>
@@ -51,22 +52,27 @@ public void RemoveCacheDependencies(IEnumerable<string>? tags)
5152
{
5253
tags ??= [];
5354

54-
var currentTags = await hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey,
55-
factory => ValueTask.FromResult<List<string>>([]));
55+
var currentTags = hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey,
56+
factory => ValueTask.FromResult<List<string>>([]))
57+
.Preserve()
58+
.GetAwaiter()
59+
.GetResult();
5660

5761
foreach (var tag in tags)
5862
{
5963
currentTags.Remove(tag);
6064
}
6165

62-
hybridCache.SetAsync(TagsCacheKey, currentTags, Options);
66+
hybridCache.SetAsync(TagsCacheKey, currentTags, Options).Preserve().GetAwaiter().GetResult();
6367
}
6468

6569
/// <summary>
6670
/// Returns the cached entries added by this library.
6771
/// </summary>
6872
public ISet<string> GetAllCacheDependencies()
6973
=> new HashSet<string>(
70-
await hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey,
71-
factory => ValueTask.FromResult<List<string>>([])), StringComparer.Ordinal);
74+
hybridCache.GetOrCreateAsync<List<string>>(TagsCacheKey, factory => ValueTask.FromResult<List<string>>([]))
75+
.Preserve()
76+
.GetAwaiter()
77+
.GetResult(), StringComparer.Ordinal);
7278
}

src/EFCoreSecondLevelCacheInterceptor.HybridCache/EFHybridCacheProvider.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ public void InsertValue(EFCacheKey cacheKey, EFCachedData? value, EFCachePolicy
2424
ArgumentNullException.ThrowIfNull(cacheKey);
2525
ArgumentNullException.ThrowIfNull(cachePolicy);
2626

27-
// NOTE: hybridCache doesn't have a synchronous API!! So ... we will wait!
28-
2927
value ??= new EFCachedData
3028
{
3129
IsNull = true
3230
};
3331

3432
hybridCache.SetAsync(cacheKey.KeyHash, value, new HybridCacheEntryOptions
35-
{
36-
Expiration = cachePolicy.CacheTimeout,
37-
LocalCacheExpiration = cachePolicy.CacheTimeout
38-
}, cacheKey.CacheDependencies);
33+
{
34+
Expiration = cachePolicy.CacheTimeout,
35+
LocalCacheExpiration = cachePolicy.CacheTimeout
36+
}, cacheKey.CacheDependencies)
37+
.Preserve()
38+
.GetAwaiter()
39+
.GetResult();
3940

4041
cacheDependenciesStore.AddCacheDependencies(cacheKey.CacheDependencies);
4142
}
@@ -62,10 +63,13 @@ public void ClearAllCachedEntries()
6263
ArgumentNullException.ThrowIfNull(cacheKey);
6364

6465
return hybridCache.GetOrCreateAsync<EFCachedData?>(cacheKey.KeyHash, factory
65-
=> ValueTask.FromResult<EFCachedData?>(new EFCachedData
66-
{
67-
IsNull = true
68-
}));
66+
=> ValueTask.FromResult<EFCachedData?>(new EFCachedData
67+
{
68+
IsNull = true
69+
}))
70+
.Preserve()
71+
.GetAwaiter()
72+
.GetResult();
6973
}
7074

7175
/// <summary>
@@ -81,11 +85,7 @@ public void InvalidateCacheDependencies(EFCacheKey cacheKey)
8185

8286
private void InvalidateTaggedEntries(ISet<string> cacheDependencies)
8387
{
84-
foreach (var rootCacheKey in cacheDependencies)
85-
{
86-
hybridCache.RemoveByTagAsync(rootCacheKey);
87-
}
88-
88+
hybridCache.RemoveByTagAsync(cacheDependencies).Preserve().GetAwaiter().GetResult();
8989
cacheDependenciesStore.RemoveCacheDependencies(cacheDependencies);
9090
}
9191
}

0 commit comments

Comments
 (0)