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 ;
73using System ;
8- using Microsoft . Extensions . Options ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
96
107namespace Enyim . Caching
118{
@@ -40,10 +37,17 @@ async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken toke
4037 void IDistributedCache . Set ( string key , byte [ ] value , DistributedCacheEntryOptions options )
4138 {
4239 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+
4347 var expiration = GetExpiration ( options ) ;
4448 PerformStore ( StoreMode . Set , key , value , expiration , ref tmp , out var status ) ;
4549
46- if ( options . SlidingExpiration . HasValue )
50+ if ( options != null && options . SlidingExpiration . HasValue )
4751 {
4852 var sldExp = options . SlidingExpiration . Value ;
4953 Add ( GetSlidingExpirationKey ( key ) , sldExp . ToString ( ) , sldExp ) ;
@@ -52,6 +56,12 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
5256
5357 async Task IDistributedCache . SetAsync ( string key , byte [ ] value , DistributedCacheEntryOptions options , CancellationToken token = default ( CancellationToken ) )
5458 {
59+ if ( ! HasSlidingExpiration ( options ) )
60+ {
61+ await PerformStoreAsync ( StoreMode . Set , key , value , 0 ) ;
62+ return ;
63+ }
64+
5565 var expiration = GetExpiration ( options ) ;
5666 await PerformStoreAsync ( StoreMode . Set , key , value , expiration ) ;
5767
@@ -62,6 +72,23 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
6272 }
6373 }
6474
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+
6592 public void Refresh ( string key )
6693 {
6794 var sldExpKey = GetSlidingExpirationKey ( key ) ;
0 commit comments