In Django/python, how do I set the memcache to infinite time?

纵然是瞬间 提交于 2019-12-10 02:48:54

问题


cache.set(key, value, 9999999)

But this is not infinite time...


回答1:


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

And from the FAQ:

What are the limits on setting expire time? (why is there a 30 day limit?)

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.




回答2:


From the docs:

Expiration times can be set from 0, meaning "never expire", to 30 days. Any time higher than 30 days is interpreted as a unix timestamp date

So, to set a key to never expire, set the timeout to 0.




回答3:


Support for non-expiring cache has been added in Django 1.6 by setting timeout=None




回答4:


Another simple technique is to write the generated HTML out to a file on the disk, and to use that as your cache. It's not hard to implement, and it works quite well as a file-based cache that NEVER expires, is quite transparent, etc.

It's not the django way, but it works well.



来源:https://stackoverflow.com/questions/2917290/in-django-python-how-do-i-set-the-memcache-to-infinite-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!