@@ -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 = GetExpiration ( creationTime , absoluteExpiration , options ) ;
144
+ var ttl = GetExpirationInSeconds ( 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 , ttl . GetValueOrDefault ( ) ) ;
157
+ var setTtl = batch . KeyExpireAsync ( prefixedKey , TimeSpan . FromSeconds ( 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 = GetExpiration ( creationTime , absoluteExpiration , options ) ;
217
+ var ttl = GetExpirationInSeconds ( 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 , ttl . GetValueOrDefault ( ) )
228
+ cache . KeyExpireAsync ( prefixedKey , TimeSpan . FromSeconds ( ttl . GetValueOrDefault ( ) ) )
229
229
) . ConfigureAwait ( false ) ;
230
230
}
231
231
}
@@ -555,23 +555,21 @@ private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? abs
555
555
}
556
556
}
557
557
558
- private static TimeSpan ? GetExpiration ( DateTimeOffset creationTime , DateTimeOffset ? absoluteExpiration , DistributedCacheEntryOptions options )
558
+ private static long ? GetExpirationInSeconds ( DateTimeOffset creationTime , DateTimeOffset ? absoluteExpiration , DistributedCacheEntryOptions options )
559
559
{
560
560
if ( absoluteExpiration . HasValue && options . SlidingExpiration . HasValue )
561
561
{
562
- var absolute = absoluteExpiration . GetValueOrDefault ( ) - creationTime ;
563
- var sliding = options . SlidingExpiration . GetValueOrDefault ( ) ;
564
-
565
- // take whichever is smaller
566
- return absolute < sliding ? absolute : sliding ;
562
+ return ( long ) Math . Min (
563
+ ( absoluteExpiration . Value - creationTime ) . TotalSeconds ,
564
+ options . SlidingExpiration . Value . TotalSeconds ) ;
567
565
}
568
566
else if ( absoluteExpiration . HasValue )
569
567
{
570
- return absoluteExpiration . GetValueOrDefault ( ) - creationTime ;
568
+ return ( long ) ( absoluteExpiration . Value - creationTime ) . TotalSeconds ;
571
569
}
572
570
else if ( options . SlidingExpiration . HasValue )
573
571
{
574
- return options . SlidingExpiration . GetValueOrDefault ( ) ;
572
+ return ( long ) options . SlidingExpiration . Value . TotalSeconds ;
575
573
}
576
574
return null ;
577
575
}
0 commit comments