Skip to content

Commit c7956bc

Browse files
committed
Fixing the minimum cache timeout validation #159 (it is too early...)
1 parent 31e9e7b commit c7956bc

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

src/CacheManager.StackExchange.Redis/RedisCacheHandle.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,8 @@ public override UpdateItemResult<TCacheValue> Update(string key, string region,
296296
{
297297
return UpdateItemResult.ForItemDidNotExist<TCacheValue>();
298298
}
299-
if (item.ExpirationTimeout < MinimumExpirationTimeout)
300-
{
301-
throw new ArgumentException("Timeout lower than one millisecond is not supported.", nameof(item.ExpirationTimeout));
302-
}
299+
300+
ValidateExpirationTimeout(item);
303301

304302
// run update
305303
var newValue = updateValue(item.Value);
@@ -357,10 +355,8 @@ protected UpdateItemResult<TCacheValue> UpdateNoScript(string key, string region
357355
{
358356
return UpdateItemResult.ForItemDidNotExist<TCacheValue>();
359357
}
360-
if (item.ExpirationTimeout < MinimumExpirationTimeout)
361-
{
362-
throw new ArgumentException("Timeout lower than one millisecond is not supported.", nameof(item.ExpirationTimeout));
363-
}
358+
359+
ValidateExpirationTimeout(item);
364360

365361
var oldValue = ToRedisValue(item.Value);
366362

@@ -754,6 +750,14 @@ private static Tuple<string, string> ParseKey(string value)
754750
return Tuple.Create(key, region);
755751
}
756752

753+
private static void ValidateExpirationTimeout(CacheItem<TCacheValue> item)
754+
{
755+
if ((item.ExpirationMode == ExpirationMode.Absolute || item.ExpirationMode == ExpirationMode.Sliding) && item.ExpirationTimeout < MinimumExpirationTimeout)
756+
{
757+
throw new ArgumentException("Timeout lower than one millisecond is not supported.", nameof(item.ExpirationTimeout));
758+
}
759+
}
760+
757761
private string GetKey(string key, string region = null)
758762
{
759763
if (string.IsNullOrWhiteSpace(key))
@@ -833,10 +837,7 @@ private bool Set(CacheItem<TCacheValue> item, When when, bool sync = false)
833837

834838
var flags = sync ? CommandFlags.None : CommandFlags.FireAndForget;
835839

836-
if (item.ExpirationTimeout < MinimumExpirationTimeout)
837-
{
838-
throw new ArgumentException("Timeout lower than one millisecond is not supported.", nameof(item.ExpirationTimeout));
839-
}
840+
ValidateExpirationTimeout(item);
840841

841842
// ARGV [1]: value, [2]: type, [3]: expirationMode, [4]: expirationTimeout(millis), [5]: created(ticks)
842843
var parameters = new RedisValue[]
@@ -916,10 +917,7 @@ private bool SetNoScript(CacheItem<TCacheValue> item, When when, bool sync = fal
916917
var fullKey = GetKey(item.Key, item.Region);
917918
var value = ToRedisValue(item.Value);
918919

919-
if (item.ExpirationTimeout < MinimumExpirationTimeout)
920-
{
921-
throw new ArgumentException("Timeout lower than one millisecond is not supported.", nameof(item.ExpirationTimeout));
922-
}
920+
ValidateExpirationTimeout(item);
923921

924922
var metaValues = new[]
925923
{

0 commit comments

Comments
 (0)