@@ -141,7 +141,7 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
141
141
try
142
142
{
143
143
var prefixedKey = _instancePrefix . Append ( key ) ;
144
- var ttl = GetExpirationInSeconds ( creationTime , absoluteExpiration , options ) ;
144
+ var ttl = GetExpiration ( creationTime , absoluteExpiration , options ) ;
145
145
var fields = GetHashFields ( value , absoluteExpiration , options . SlidingExpiration ) ;
146
146
147
147
if ( ttl is null )
@@ -154,7 +154,7 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
154
154
// SE.Redis reuses the async API shape for this scenario
155
155
var batch = cache . CreateBatch ( ) ;
156
156
var setFields = batch . HashSetAsync ( prefixedKey , fields ) ;
157
- var setTtl = batch . KeyExpireAsync ( prefixedKey , TimeSpan . FromSeconds ( ttl . GetValueOrDefault ( ) ) ) ;
157
+ var setTtl = batch . KeyExpireAsync ( prefixedKey , ttl . GetValueOrDefault ( ) ) ;
158
158
batch . Execute ( ) ; // synchronous wait-for-all
159
159
160
160
// we *expect* that they are both complete; if not, something is *already*
@@ -214,7 +214,7 @@ public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOption
214
214
try
215
215
{
216
216
var prefixedKey = _instancePrefix . Append ( key ) ;
217
- var ttl = GetExpirationInSeconds ( creationTime , absoluteExpiration , options ) ;
217
+ var ttl = GetExpiration ( creationTime , absoluteExpiration , options ) ;
218
218
var fields = GetHashFields ( value , absoluteExpiration , options . SlidingExpiration ) ;
219
219
220
220
if ( ttl is null )
@@ -225,7 +225,7 @@ public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOption
225
225
{
226
226
await Task . WhenAll (
227
227
cache . HashSetAsync ( prefixedKey , fields ) ,
228
- cache . KeyExpireAsync ( prefixedKey , TimeSpan . FromSeconds ( ttl . GetValueOrDefault ( ) ) )
228
+ cache . KeyExpireAsync ( prefixedKey , ttl . GetValueOrDefault ( ) )
229
229
) . ConfigureAwait ( false ) ;
230
230
}
231
231
}
@@ -555,21 +555,23 @@ private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? abs
555
555
}
556
556
}
557
557
558
- private static long ? GetExpirationInSeconds ( DateTimeOffset creationTime , DateTimeOffset ? absoluteExpiration , DistributedCacheEntryOptions options )
558
+ private static TimeSpan ? GetExpiration ( DateTimeOffset creationTime , DateTimeOffset ? absoluteExpiration , DistributedCacheEntryOptions options )
559
559
{
560
560
if ( absoluteExpiration . HasValue && options . SlidingExpiration . HasValue )
561
561
{
562
- return ( long ) Math . Min (
563
- ( absoluteExpiration . Value - creationTime ) . TotalSeconds ,
564
- options . SlidingExpiration . Value . TotalSeconds ) ;
562
+ var absolute = absoluteExpiration . GetValueOrDefault ( ) - creationTime ;
563
+ var sliding = options . SlidingExpiration . GetValueOrDefault ( ) ;
564
+
565
+ // take whichever is smaller
566
+ return absolute < sliding ? absolute : sliding ;
565
567
}
566
568
else if ( absoluteExpiration . HasValue )
567
569
{
568
- return ( long ) ( absoluteExpiration . Value - creationTime ) . TotalSeconds ;
570
+ return absoluteExpiration . GetValueOrDefault ( ) - creationTime ;
569
571
}
570
572
else if ( options . SlidingExpiration . HasValue )
571
573
{
572
- return ( long ) options . SlidingExpiration . Value . TotalSeconds ;
574
+ return options . SlidingExpiration . GetValueOrDefault ( ) ;
573
575
}
574
576
return null ;
575
577
}
0 commit comments