1
- using Microsoft . Extensions . Caching . Distributed ;
2
- using Microsoft . Extensions . Logging ;
3
- using System . Threading . Tasks ;
4
- using System . Threading ;
5
- using Enyim . Caching . Memcached ;
6
- using Microsoft . Extensions . Caching . Memory ;
1
+ using Enyim . Caching . Memcached ;
2
+ using Microsoft . Extensions . Caching . Distributed ;
7
3
using System ;
8
- using Microsoft . Extensions . Options ;
4
+ using System . Threading ;
5
+ using System . Threading . Tasks ;
9
6
10
7
namespace Enyim . Caching
11
8
{
@@ -40,10 +37,17 @@ async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken toke
40
37
void IDistributedCache . Set ( string key , byte [ ] value , DistributedCacheEntryOptions options )
41
38
{
42
39
ulong tmp = 0 ;
40
+
41
+ if ( ! HasSlidingExpiration ( options ) )
42
+ {
43
+ PerformStore ( StoreMode . Set , key , value , 0 , ref tmp , out var status0 ) ;
44
+ return ;
45
+ }
46
+
43
47
var expiration = GetExpiration ( options ) ;
44
48
PerformStore ( StoreMode . Set , key , value , expiration , ref tmp , out var status ) ;
45
49
46
- if ( options . SlidingExpiration . HasValue )
50
+ if ( options != null && options . SlidingExpiration . HasValue )
47
51
{
48
52
var sldExp = options . SlidingExpiration . Value ;
49
53
Add ( GetSlidingExpirationKey ( key ) , sldExp . ToString ( ) , sldExp ) ;
@@ -52,6 +56,12 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
52
56
53
57
async Task IDistributedCache . SetAsync ( string key , byte [ ] value , DistributedCacheEntryOptions options , CancellationToken token = default ( CancellationToken ) )
54
58
{
59
+ if ( ! HasSlidingExpiration ( options ) )
60
+ {
61
+ await PerformStoreAsync ( StoreMode . Set , key , value , 0 ) ;
62
+ return ;
63
+ }
64
+
55
65
var expiration = GetExpiration ( options ) ;
56
66
await PerformStoreAsync ( StoreMode . Set , key , value , expiration ) ;
57
67
@@ -62,6 +72,23 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
62
72
}
63
73
}
64
74
75
+ private static bool HasSlidingExpiration ( DistributedCacheEntryOptions options )
76
+ {
77
+ if ( options == null )
78
+ {
79
+ return false ;
80
+ }
81
+
82
+ if ( ( options . SlidingExpiration . HasValue == false || options . SlidingExpiration . Value == TimeSpan . Zero ) &&
83
+ options . AbsoluteExpiration . HasValue == false &&
84
+ options . AbsoluteExpirationRelativeToNow . HasValue == false )
85
+ {
86
+ return false ;
87
+ }
88
+
89
+ return true ;
90
+ }
91
+
65
92
public void Refresh ( string key )
66
93
{
67
94
var sldExpKey = GetSlidingExpirationKey ( key ) ;
0 commit comments