在Django / python中,如何将Memcache设置为无限时间?

问题描述 投票:9回答:4
cache.set(key, value, 9999999)

但这不是无限的时间...

python django memcached
4个回答
12
投票
def _get_memcache_timeout(self, timeout):
    """
    Memcached deals with long (> 30 days) timeouts in a special
    way. Call this function to obtain a safe value for your timeout.
    """
    timeout = timeout or self.default_timeout
    if timeout > 2592000: # 60*60*24*30, 30 days
        # See http://code.google.com/p/memcached/wiki/FAQ
        # "You can set expire times up to 30 days in the future. After that
        # memcached interprets it as a date, and will expire the item after
        # said date. This is a simple (but obscure) mechanic."
        #
        # This means that we have to switch to absolute timestamps.
        timeout += int(time.time())
    return timeout

并且来自FAQ

设置过期时间有什么限制? (为什么会有30天的限制?)

您可以设置最多30天的过期时间。之后,memcached会将其解释为日期,并将在该日期之后使该项目过期。这是一个简单(但晦涩)的机制。


9
投票

来自the docs

如果此设置的值为None,则缓存条目不会过期。

值得注意的是,这与Memcache standard protocol中的到期时间如何不同:

过期时间可以设置为0(表示“永不过期”)到30天。任何高于30天的时间都将被解释为unix时间戳日期

因此,要将密钥设置为永不过期,如果使用的是Django的缓存抽象,则将超时设置为None,如果直接使用Memcache,则将超时设置为0


8
投票

通过设置has been added in Django 1.6支持不过期的缓存timeout=None


-6
投票

另一种简单的技术是将生成的HTML写出到磁盘上的文件中,并将其用作缓存。这并不难实现,并且由于永不过期,相当透明的基于文件的缓存而非常有效,等等。

这不是django的方法,但是效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.