我想将内存缓存对象的到期时间设置为特定日期。
cache.set(string, 1, 86400)
上面的语句允许我将其设置为一天,但如果日期更改,它不会过期。我处理这个问题的一种方法是计算一天中剩余的秒数并将其作为变量提供。
我想知道是否有更简单/有效的方法来做到这一点。
最佳答案
查看documentation ,我们看到expiration参数解释为:
Optional expiration time, either relative number of seconds from current time (up to 1 month), or an absolute Unix epoch time. By default, items never expire, though items may be evicted due to memory pressure. Float values will be rounded up to the nearest whole second.
所以基本上,如果您输入的数字小于 2592000,它会被解释为相对时间。因此,数字 86400 将被解释为距离现在(设置时间)86400 秒(一天)。
看来您需要使用比该数字更大的数字来表示绝对时间。有variety获取 Unix 时间戳的方法。但很简单,您可以这样做:
time_tuple = (2013, 2, 15, 0, 0, 0,0,0,0)
timestamp = time.mktime(time_tuple)
cache.set(string, 1, timestamp);
你最初的想法是正确的。您现在可以找出时间戳,以及您想要的日期的时间戳,只需提供差异即可,这也是等效的。
关于python - Memcache 对象在特定日期到期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14678270/