@@ -21,12 +21,12 @@ public static Task<IDictionary<string, CacheValue<T>>> GetAllAsync<T>(this ICach
2121
2222 public static Task < long > IncrementAsync ( this ICacheClient client , string key , long amount , DateTime ? expiresAtUtc )
2323 {
24- return client . IncrementAsync ( key , amount , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
24+ return client . IncrementAsync ( key , amount , client . ToExpiresIn ( expiresAtUtc ) ) ;
2525 }
2626
2727 public static Task < double > IncrementAsync ( this ICacheClient client , string key , double amount , DateTime ? expiresAtUtc )
2828 {
29- return client . IncrementAsync ( key , amount , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
29+ return client . IncrementAsync ( key , amount , client . ToExpiresIn ( expiresAtUtc ) ) ;
3030 }
3131
3232 public static Task < long > IncrementAsync ( this ICacheClient client , string key , TimeSpan ? expiresIn = null )
@@ -46,42 +46,42 @@ public static Task<long> DecrementAsync(this ICacheClient client, string key, lo
4646
4747 public static Task < long > DecrementAsync ( this ICacheClient client , string key , long amount , DateTime ? expiresAtUtc )
4848 {
49- return client . IncrementAsync ( key , - amount , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
49+ return client . IncrementAsync ( key , - amount , client . ToExpiresIn ( expiresAtUtc ) ) ;
5050 }
5151
5252 public static Task < double > DecrementAsync ( this ICacheClient client , string key , double amount , DateTime ? expiresAtUtc )
5353 {
54- return client . IncrementAsync ( key , - amount , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
54+ return client . IncrementAsync ( key , - amount , client . ToExpiresIn ( expiresAtUtc ) ) ;
5555 }
5656
5757 public static Task < bool > AddAsync < T > ( this ICacheClient client , string key , T value , DateTime ? expiresAtUtc )
5858 {
59- return client . AddAsync ( key , value , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
59+ return client . AddAsync ( key , value , client . ToExpiresIn ( expiresAtUtc ) ) ;
6060 }
6161
6262 public static Task < bool > SetAsync < T > ( this ICacheClient client , string key , T value , DateTime ? expiresAtUtc )
6363 {
64- return client . SetAsync ( key , value , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
64+ return client . SetAsync ( key , value , client . ToExpiresIn ( expiresAtUtc ) ) ;
6565 }
6666
6767 public static Task < bool > ReplaceAsync < T > ( this ICacheClient client , string key , T value , DateTime ? expiresAtUtc )
6868 {
69- return client . ReplaceAsync ( key , value , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
69+ return client . ReplaceAsync ( key , value , client . ToExpiresIn ( expiresAtUtc ) ) ;
7070 }
7171
7272 public static Task < bool > ReplaceIfEqualAsync < T > ( this ICacheClient client , string key , T value , T expected , DateTime ? expiresAtUtc )
7373 {
74- return client . ReplaceIfEqualAsync ( key , value , expected , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
74+ return client . ReplaceIfEqualAsync ( key , value , expected , client . ToExpiresIn ( expiresAtUtc ) ) ;
7575 }
7676
7777 public static Task < int > SetAllAsync ( this ICacheClient client , IDictionary < string , object > values , DateTime ? expiresAtUtc )
7878 {
79- return client . SetAllAsync ( values , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
79+ return client . SetAllAsync ( values , client . ToExpiresIn ( expiresAtUtc ) ) ;
8080 }
8181
8282 public static Task SetExpirationAsync ( this ICacheClient client , string key , DateTime expiresAtUtc )
8383 {
84- return client . SetExpirationAsync ( key , expiresAtUtc . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
84+ return client . SetExpirationAsync ( key , client . ToExpiresIn ( expiresAtUtc ) ?? TimeSpan . MaxValue ) ;
8585 }
8686
8787 public static async Task < bool > ListAddAsync < T > ( this ICacheClient client , string key , T value , TimeSpan ? expiresIn = null )
@@ -152,7 +152,7 @@ public static Task<bool> SetUnixTimeMillisecondsAsync(this ICacheClient client,
152152
153153 public static Task < bool > SetUnixTimeMillisecondsAsync ( this ICacheClient client , string key , DateTime value , DateTime ? expiresAtUtc )
154154 {
155- return client . SetAsync ( key , value . ToUnixTimeMilliseconds ( ) , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
155+ return client . SetAsync ( key , value . ToUnixTimeMilliseconds ( ) , client . ToExpiresIn ( expiresAtUtc ) ) ;
156156 }
157157
158158 public static async Task < DateTimeOffset > GetUnixTimeSecondsAsync ( this ICacheClient client , string key , DateTime ? defaultValue = null )
@@ -171,6 +171,18 @@ public static Task<bool> SetUnixTimeSecondsAsync(this ICacheClient client, strin
171171
172172 public static Task < bool > SetUnixTimeSecondsAsync ( this ICacheClient client , string key , DateTime value , DateTime ? expiresAtUtc )
173173 {
174- return client . SetAsync ( key , value . ToUnixTimeSeconds ( ) , expiresAtUtc ? . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ) ;
174+ return client . SetAsync ( key , value . ToUnixTimeSeconds ( ) , client . ToExpiresIn ( expiresAtUtc ) ) ;
175+ }
176+
177+ /// <summary>
178+ /// Converts a DateTime expiration to a TimeSpan relative to now.
179+ /// DateTime.MaxValue is treated as null (no expiration).
180+ /// </summary>
181+ private static TimeSpan ? ToExpiresIn ( this ICacheClient client , DateTime ? expiresAtUtc )
182+ {
183+ if ( ! expiresAtUtc . HasValue || expiresAtUtc . Value == DateTime . MaxValue )
184+ return null ;
185+
186+ return expiresAtUtc . Value . Subtract ( client . GetTimeProvider ( ) . GetUtcNow ( ) . UtcDateTime ) ;
175187 }
176188}
0 commit comments