Skip to content

Commit 5a05b2d

Browse files
committed
Allow caching with no expiration, Close #289
1 parent 49a03b5 commit 5a05b2d

25 files changed

+140
-58
lines changed

src/EFCoreSecondLevelCacheInterceptor.CacheManager.Core/EFCacheManagerCoreProvider.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@ public void InsertValue(EFCacheKey cacheKey, EFCachedData? value, EFCachePolicy
7979
}
8080
else
8181
{
82-
_valuesCacheManager.Add(new CacheItem<EFCachedData>(keyHash, value,
83-
cachePolicy.CacheExpirationMode == CacheExpirationMode.Absolute
84-
? ExpirationMode.Absolute
85-
: ExpirationMode.Sliding, cachePolicy.CacheTimeout));
82+
if (cachePolicy.CacheExpirationMode == CacheExpirationMode.NeverRemove ||
83+
!cachePolicy.CacheTimeout.HasValue)
84+
{
85+
_valuesCacheManager.Add(new CacheItem<EFCachedData>(keyHash, value));
86+
}
87+
else if (cachePolicy.CacheTimeout.HasValue)
88+
{
89+
_valuesCacheManager.Add(new CacheItem<EFCachedData>(keyHash, value,
90+
cachePolicy.CacheExpirationMode == CacheExpirationMode.Absolute
91+
? ExpirationMode.Absolute
92+
: ExpirationMode.Sliding, cachePolicy.CacheTimeout.Value));
93+
}
8694
}
8795
}
8896

src/EFCoreSecondLevelCacheInterceptor.CacheManager.Core/EFCacheManagerCoreProviderOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static EFCoreSecondLevelCacheOptions UseCacheManagerCoreProvider(this EFC
3131
/// <param name="timeout">The expiration timeout.</param>
3232
public static EFCoreSecondLevelCacheOptions UseCacheManagerCoreProvider(this EFCoreSecondLevelCacheOptions options,
3333
CacheExpirationMode expirationMode,
34-
TimeSpan timeout)
34+
TimeSpan? timeout = null)
3535
{
3636
if (options == null)
3737
{

src/EFCoreSecondLevelCacheInterceptor.CacheManager.Core/EFCoreSecondLevelCacheInterceptor.CacheManager.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>5.0.0</VersionPrefix>
4+
<VersionPrefix>5.1.1</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor.EasyCaching.Core/EFCoreSecondLevelCacheInterceptor.EasyCaching.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>5.0.0</VersionPrefix>
4+
<VersionPrefix>5.1.1</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor.EasyCaching.Core/EFEasyCachingCoreProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void InsertValue(EFCacheKey cacheKey, EFCachedData? value, EFCachePolicy
6969
var easyCachingProvider = GetEasyCachingProvider(cacheKey);
7070

7171
var keyHash = cacheKey.KeyHash;
72+
var timeout = cachePolicy.CacheTimeout ?? TimeSpan.MaxValue;
7273

7374
foreach (var rootCacheKey in cacheKey.CacheDependencies)
7475
{
@@ -84,17 +85,17 @@ public void InsertValue(EFCacheKey cacheKey, EFCachedData? value, EFCachePolicy
8485
easyCachingProvider.Set(rootCacheKey, new HashSet<string>(StringComparer.OrdinalIgnoreCase)
8586
{
8687
keyHash
87-
}, cachePolicy.CacheTimeout);
88+
}, timeout);
8889
}
8990
else
9091
{
9192
items.Value.Add(keyHash);
92-
easyCachingProvider.Set(rootCacheKey, items.Value, cachePolicy.CacheTimeout);
93+
easyCachingProvider.Set(rootCacheKey, items.Value, timeout);
9394
}
9495
}
9596

9697
// We don't support Sliding Expiration at this time. -> https://github.com/dotnetcore/EasyCaching/issues/113
97-
easyCachingProvider.Set(keyHash, value, cachePolicy.CacheTimeout);
98+
easyCachingProvider.Set(keyHash, value, timeout);
9899
}
99100

100101
/// <summary>

src/EFCoreSecondLevelCacheInterceptor.EasyCaching.Core/EFEasyCachingCoreProviderOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static EFCoreSecondLevelCacheOptions UseEasyCachingCoreProvider(this EFCo
6767
public static EFCoreSecondLevelCacheOptions UseEasyCachingCoreProvider(this EFCoreSecondLevelCacheOptions options,
6868
string providerName,
6969
CacheExpirationMode expirationMode,
70-
TimeSpan timeout,
70+
TimeSpan? timeout = null,
7171
bool isHybridCache = false)
7272
{
7373
if (options == null)
@@ -105,7 +105,7 @@ public static EFCoreSecondLevelCacheOptions UseEasyCachingCoreProvider(this EFCo
105105
public static EFCoreSecondLevelCacheOptions UseEasyCachingCoreProvider(this EFCoreSecondLevelCacheOptions options,
106106
Func<IServiceProvider, EFCacheKey?, string> providerName,
107107
CacheExpirationMode expirationMode,
108-
TimeSpan timeout,
108+
TimeSpan? timeout = null,
109109
bool isHybridCache = false)
110110
{
111111
if (options == null)

src/EFCoreSecondLevelCacheInterceptor.FusionCache/EFCoreSecondLevelCacheInterceptor.FusionCache.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>5.1.0</VersionPrefix>
4+
<VersionPrefix>5.1.1</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor.FusionCache/EFFusionCacheProvider.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ public void InsertValue(EFCacheKey cacheKey, EFCachedData? value, EFCachePolicy
5050

5151
_fusionCache.Set(cacheKey.KeyHash, value, entryOptions =>
5252
{
53-
entryOptions.SetDuration(cachePolicy.CacheTimeout);
54-
55-
if (cachePolicy.CacheExpirationMode == CacheExpirationMode.Sliding)
53+
if (cachePolicy.CacheExpirationMode != CacheExpirationMode.NeverRemove && cachePolicy.CacheTimeout.HasValue)
5654
{
57-
entryOptions.SetFailSafe(isEnabled: true, cachePolicy.CacheTimeout.Add(cachePolicy.CacheTimeout));
55+
entryOptions.SetDuration(cachePolicy.CacheTimeout.Value);
56+
57+
if (cachePolicy.CacheExpirationMode == CacheExpirationMode.Sliding)
58+
{
59+
entryOptions.SetFailSafe(isEnabled: true,
60+
cachePolicy.CacheTimeout.Value.Add(cachePolicy.CacheTimeout.Value));
61+
}
5862
}
5963
}, cacheKey.CacheDependencies);
6064
}

src/EFCoreSecondLevelCacheInterceptor.FusionCache/EFFusionCacheProviderOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static EFCoreSecondLevelCacheOptions UseFusionCacheProvider(this EFCoreSe
4343
/// </param>
4444
public static EFCoreSecondLevelCacheOptions UseFusionCacheProvider(this EFCoreSecondLevelCacheOptions options,
4545
CacheExpirationMode expirationMode,
46-
TimeSpan timeout,
46+
TimeSpan? timeout = null,
4747
string? namedCache = null)
4848
{
4949
ArgumentNullException.ThrowIfNull(options);

src/EFCoreSecondLevelCacheInterceptor.HybridCache/EFCoreSecondLevelCacheInterceptor.HybridCache.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>5.0.0</VersionPrefix>
4+
<VersionPrefix>5.1.1</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

0 commit comments

Comments
 (0)