Skip to content

Commit 0b99297

Browse files
committed
Revert "simplify TTL"
This reverts commit 670a62e.
1 parent 670a62e commit 0b99297

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
141141
try
142142
{
143143
var prefixedKey = _instancePrefix.Append(key);
144-
var ttl = GetExpiration(creationTime, absoluteExpiration, options);
144+
var ttl = GetExpirationInSeconds(creationTime, absoluteExpiration, options);
145145
var fields = GetHashFields(value, absoluteExpiration, options.SlidingExpiration);
146146

147147
if (ttl is null)
@@ -154,7 +154,7 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
154154
// SE.Redis reuses the async API shape for this scenario
155155
var batch = cache.CreateBatch();
156156
var setFields = batch.HashSetAsync(prefixedKey, fields);
157-
var setTtl = batch.KeyExpireAsync(prefixedKey, ttl.GetValueOrDefault());
157+
var setTtl = batch.KeyExpireAsync(prefixedKey, TimeSpan.FromSeconds(ttl.GetValueOrDefault()));
158158
batch.Execute(); // synchronous wait-for-all
159159

160160
// 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
214214
try
215215
{
216216
var prefixedKey = _instancePrefix.Append(key);
217-
var ttl = GetExpiration(creationTime, absoluteExpiration, options);
217+
var ttl = GetExpirationInSeconds(creationTime, absoluteExpiration, options);
218218
var fields = GetHashFields(value, absoluteExpiration, options.SlidingExpiration);
219219

220220
if (ttl is null)
@@ -225,7 +225,7 @@ public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOption
225225
{
226226
await Task.WhenAll(
227227
cache.HashSetAsync(prefixedKey, fields),
228-
cache.KeyExpireAsync(prefixedKey, ttl.GetValueOrDefault())
228+
cache.KeyExpireAsync(prefixedKey, TimeSpan.FromSeconds(ttl.GetValueOrDefault()))
229229
).ConfigureAwait(false);
230230
}
231231
}
@@ -555,23 +555,21 @@ private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? abs
555555
}
556556
}
557557

558-
private static TimeSpan? GetExpiration(DateTimeOffset creationTime, DateTimeOffset? absoluteExpiration, DistributedCacheEntryOptions options)
558+
private static long? GetExpirationInSeconds(DateTimeOffset creationTime, DateTimeOffset? absoluteExpiration, DistributedCacheEntryOptions options)
559559
{
560560
if (absoluteExpiration.HasValue && options.SlidingExpiration.HasValue)
561561
{
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);
567565
}
568566
else if (absoluteExpiration.HasValue)
569567
{
570-
return absoluteExpiration.GetValueOrDefault() - creationTime;
568+
return (long)(absoluteExpiration.Value - creationTime).TotalSeconds;
571569
}
572570
else if (options.SlidingExpiration.HasValue)
573571
{
574-
return options.SlidingExpiration.GetValueOrDefault();
572+
return (long)options.SlidingExpiration.Value.TotalSeconds;
575573
}
576574
return null;
577575
}

0 commit comments

Comments
 (0)