简体   繁体   English

StackExchange.Redis 密钥按 UTC 日期到期

[英]StackExchange.Redis key expiration by UTC date

I am working with StackExchange.Redis and building a Redis client interface RedisClientManager .我正在使用StackExchange.Redis并构建一个 Redis 客户端界面RedisClientManager In my interface I have 2 key setters (by timespan expiration and datetime expiration):在我的界面中,我有 2 个密钥设置器(按时间跨度到期和日期时间到期):

By timespan:按时间跨度:

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);
}

Usage:用法:

By timespan:按时间跨度:

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 new key by using Timespan (first method), the object is in Redis cache and expires after 5 minutes as well.如果我使用 Timespan(第一种方法)添加新密钥,则该对象在 Redis 缓存中并在 5 分钟后过期。 If I add new key by using Date (second method), the object is not added to Redis.如果我使用 Date(第二种方法)添加新键,则该对象不会添加到 Redis。

This issue happens only on server.此问题仅发生在服务器上。 On localhost all works fine.在本地主机上一切正常。

Maybe Redis uses local server time for keys?也许 Redis 使用本地服务器时间作为密钥?

How can I fix this issue?我该如何解决这个问题? What the proper way to set absolute expiration to key by using StackExchange.Redis ?使用StackExchange.Redis将绝对过期时间设置为 key 的正确方法是什么?

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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM