Skip to content

Inserting item with sub-millisecond expiration time causes problems on get (redis backend) #159

@ahockersten

Description

@ahockersten

I think I have found a corner case in the Redis code that causes problems where items are inserted with really low durations.

Consider the following code:

var item = new CacheItem<object>("foo", "bar", sliding ExpirationMode.Absolute, TimeSpan.FromTicks(1));
cache.Put(item);

This will enter CacheItem's constructor, where TimeSpan will be checked if it is > 0 at CacheItem.cs line 133

if (ExpirationMode != ExpirationMode.Default && ExpirationMode != ExpirationMode.None && ExpirationTimeout <= TimeSpan.Zero)
{
    throw new ArgumentOutOfRangeException(nameof(timeout), "Expiration timeout must be greater than zero if expiration mode is defined.");
}

When cache items are stored into Redis, their duration is converted into a millisecond value (RedisCacheHandle.cs line 833 and other places). If this value is lower than 1 ms, this will end up being stored as 0. Later, inside GetCacheItemAndVersion() in RedisCacheHandle.cs, at line 499+, CacheItem's constructor is called with this value, which is now 0, which causes the constructor to throw an exception.

I am unsure what the fix for this is, else I would submit a proper patch for it. I will gladly submit a patch if we can figure out what the fix should be.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions