9
cache.set(key, value, 9999999)

But this is not infinite time...

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

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

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.

miku
  • 181,842
  • 47
  • 306
  • 310
10

From the docs:

If the value of this settings is None, cache entries will not expire.

Notably, this is different from how the expiration time works in the Memcache standard protocol:

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 None if you're using Django's cache abstraction, or 0 if you're using Memcache more directly.

Chris Lawlor
  • 47,306
  • 11
  • 48
  • 68
  • When I set cache to 0 it doesn't appear to cache at all. – jfunk Jul 25 '18 at 20:47
  • Setting Timeout to 0 causes keys to immediately expire (effectively “don’t cache” at all). You can set TIMEOUT to None so that, by default, cache keys never expire. See https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-CACHES-TIMEOUT for details. – jibin mathew Dec 20 '19 at 10:41
8

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

Gady
  • 4,935
  • 8
  • 38
  • 48
  • `Django 1.6.2`, `Memcached 1.4.17`. Was using `timeout=0` and it was same as if i didn't cache at all. `timeout=None` is the way to go for non-expiring cache. – Neara Mar 19 '14 at 08:58
-6

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.

mlissner
  • 17,359
  • 18
  • 106
  • 169
  • -1 Question was about memcache, this way it will be much slower. – Viktor Stískala Mar 14 '12 at 16:49
  • 1
    Depends. Slower than what? If you are generating a very complicated page, this can be faster than regenerating it from scratch, and yet be fast enough. If you're generating a simple page, this is slower than serving it from memcache. – mlissner Nov 10 '12 at 20:33