Extract of StackExchange.Redis key by UTC date

I am working with StackExchange.Redis

and creating some client interface RedisClientManager

. In my interface, I have 2 key setters (on expiration and time and date expiration):

By time:

public void Set(string key, object value, TimeSpan timeout)
{
    _cache.StringSet(key, Serialize(value), timeout);
}

      

By date:

public void Set(string key, object value, DateTime expires)
{
    _cache.StringSet(key, Serialize(value));
    _cache.KeyExpire(key, expires);
}

      

Using:

By time:

RedisClientManager.Set(o.Key, o, new TimeSpan(0, 0, 5, 0));

      

By date:

RedisClientManager.Set(o.Key, o, DateTime.UtcNow.AddMinutes(5));

      

If I add a new key using Timespan (first method), the object is in the Redis cache and expires after 5 minutes. If you add a new key using Date (second method), the object will not be added to Redis. This problem only occurs on the server. Everything works fine on localhost. Maybe Redis uses server local time for keys? How can I fix this problem?

How do I correctly set the absolute key expiration using StackExchange.Redis

?

+5


source to share


1 answer


How about something like ...



public void Set(string key, object value, DateTime expires)
{
    var expiryTimeSpan = expires.Subtract(DateTime.UtcNow);

    _cache.StringSet(key, Serialize(value), expiryTimeSpan);

    //or Set(key, value, expiryTimeSpan);
} 

      

+7


source







All Articles