dolma33
dolma33

Reputation: 4303

Low-level caching with Django

Instead of caching the whole site, or whole views, I've decided to cache only a few heavy queries, using the low level caching API.

I'm doing something like this

key = ...
value = cache.get(key)
if value is None:
    value = ... 
    cache.set(key, value, CACHE_TIMEOUT)

It works almost as expected (question 01: is there a better way to do it?),
but if I set CACHE_TIMEOUT to a big value (86400: the db is updated once a day),
it looks that CACHE_TIMEOUT is being overrided by something else, and the value is cached just for a few minutes...

(question 02:) What am I doing wrong?
Is my timeout too long? Or maybe I'm caching too much information? (value contains ~ 500-1000 objects, and it's evaluated in 50-60 different pages/keys)

Upvotes: 2

Views: 1311

Answers (2)

Bernhard Vallant
Bernhard Vallant

Reputation: 50776

I do not know which cache backend you are using nor how you exactly set the timeout, but maybe you're doing wrong (from the django documentation on caching):

Each cache backend may take arguments. They're given in query-string style on the CACHE_BACKEND setting. Valid arguments are as follows:

  • timeout: The default timeout, in seconds, to use for the cache. This argument defaults to 300 seconds (5
  • ...

CACHE_BACKEND = "memcached://127.0.0.1:11211/?timeout=60

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Some cache servers (e.g. memcached) are lossy, and newer items will force older items out of the cache. Monitor your cache statistics and modify your configuration and/or behavior appropriately.

Upvotes: 1

Related Questions