Skip to content

Commit 670a62e

Browse files
committed
simplify TTL
1 parent 41a3e8e commit 670a62e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 12 additions & 10 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 = GetExpirationInSeconds(creationTime, absoluteExpiration, options);
144+
var ttl = GetExpiration(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, TimeSpan.FromSeconds(ttl.GetValueOrDefault()));
157+
var setTtl = batch.KeyExpireAsync(prefixedKey, 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 = GetExpirationInSeconds(creationTime, absoluteExpiration, options);
217+
var ttl = GetExpiration(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, TimeSpan.FromSeconds(ttl.GetValueOrDefault()))
228+
cache.KeyExpireAsync(prefixedKey, ttl.GetValueOrDefault())
229229
).ConfigureAwait(false);
230230
}
231231
}
@@ -555,21 +555,23 @@ private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? abs
555555
}
556556
}
557557

558-
private static long? GetExpirationInSeconds(DateTimeOffset creationTime, DateTimeOffset? absoluteExpiration, DistributedCacheEntryOptions options)
558+
private static TimeSpan? GetExpiration(DateTimeOffset creationTime, DateTimeOffset? absoluteExpiration, DistributedCacheEntryOptions options)
559559
{
560560
if (absoluteExpiration.HasValue && options.SlidingExpiration.HasValue)
561561
{
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;
565567
}
566568
else if (absoluteExpiration.HasValue)
567569
{
568-
return (long)(absoluteExpiration.Value - creationTime).TotalSeconds;
570+
return absoluteExpiration.GetValueOrDefault() - creationTime;
569571
}
570572
else if (options.SlidingExpiration.HasValue)
571573
{
572-
return (long)options.SlidingExpiration.Value.TotalSeconds;
574+
return options.SlidingExpiration.GetValueOrDefault();
573575
}
574576
return null;
575577
}

0 commit comments

Comments
 (0)