python - Memcache 对象在特定日期到期

标签 python memcached

我想将内存缓存对象的到期时间设置为特定日期。

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/

相关文章:

python - 如何测量 Django 缓存性能?

ruby-on-rails - 如何在heroku上使用两个不同的memcache实例

Raspberry Pi 上的 Python 导入模块

python - 如何使用 response.css() 和 response.follow() 在 Scrapy 中对最后一页进行分页?

Python - Tkinter 文本大小未调整大小

php - 帮助 PHP call_user_func 并将函数集成到类中?

缓存数据库问题

c - Memcached ruby​​gem + Rlibmemcached 参数错误与 memcache_mget()

javascript 相当于 python 的编译和执行

python - 在Python中绘制离散值的热图