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

时间:2010-05-26 22:27:45

标签: python django memcached

cache.set(key, value, 9999999)

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

4 个答案:

答案 0 :(得分: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将其解释为日期,并在该日期之后使该项目到期。这是一个简单(但不起眼)的机制。

答案 1 :(得分:9)

来自the docs

  

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

因此,要将密钥设置为永不过期,请将超时设置为0。

答案 2 :(得分:6)

通过设置timeout=None

支持非过期缓存has been added in Django 1.6

答案 3 :(得分:-6)

另一种简单的技术是将生成的HTML写入磁盘上的文件,并将其用作缓存。它并不难实现,并且它可以很好地用作基于文件的缓存,它永远不会过期,非常透明等等。

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