8

Is there a way I can set a cache key indefinitely using the default django cache framework? I tried setting the timeout to 0, but that doesn't set the key at all unfortunately.

Hanpan
  • 10,013
  • 25
  • 77
  • 115

2 Answers2

4

Django 1.6 now accepts None for the timeout argument to specify forever.

https://docs.djangoproject.com/en/dev/topics/cache/#basic-usage

Matt
  • 3,483
  • 4
  • 36
  • 46
1

Someone recently mentioned Johnny Cache in an unrelated answer. It provides a infinite caching locmem and memcached backend when a timeout of 0 is provided.

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • Not true. Third-party systems on top of memcached don't have any control over the fact that that system is designed to throw data away eventually when it gets full. – Daniel Roseman Feb 12 '11 at 16:53
  • True that! But Johnny can still prevent invalidation due to expiration and I think that's what the OP was aiming at. – Filip Dupanović Feb 12 '11 at 17:10
  • Thanks, I'll take a look. Shocked you can't do this using the standard framework though. I guess having immortal cache kind of goes against what caching is used for – Hanpan Feb 12 '11 at 17:20
  • 4
    I'm guessing that kind of behavior on the default `LocMem` cache backend could inadvertently blow any long-running production processes for the unaware, but using Johnny during development definitely has it's place. Plus, you just have to love this bit from the docs: "This actually doesn’t cache “forever”, just for a very long time. On 32 bit systems, it will cache for 68 years, quite a bit longer than any computer will last. On a 64 bit machine, your cache will expire about 285 billion years after the Sun goes red-giant and destroys Earth." http://goo.gl/N3vAs – Filip Dupanović Feb 12 '11 at 17:28
  • last release was in [2012](https://pypi.org/project/johnny-cache/#history). – Boris Verkhovskiy Nov 12 '20 at 07:34