0

I have problems using the django cache. It looks like the cached items are not readable between processes. Is that by design? I haven't found any information on it.

Testing on the production server using two ssh sessions in parallel, and setting the cache in one and reading in the other using the memcache backend (also tested with file based backend), and this was the result:

(session 1):

>>> from django.core.cache import cache
>>> cache.set('foo','bar')
>>> cache.get('foo')
'bar'

(session 2):

>>> from django.core.cache import cache
>>> cache.get('foo', 0) #Cache has not been set yet...
0
>>> cache.get('foo', 0) #Cache has been set in other session, I expect 'bar' here
0

I use the low level cache api to cache the processed results of an uploaded file. The user then complete some more steps that describe the uploaded data at which point it's entered in the DB. This is done asynchronously using apache2 with one thread per process, mod_wsgi and python 2.5. The problem I ran in to was that the "cache.get('<filekey>')" always returns None when I test and upload a file.

Thanks

mandrake
  • 1,213
  • 1
  • 14
  • 28
  • No, that's not by design and your examples just should work. Plz, check that you correctly specified cache backend and "foo" is stored in the memcached. – alex vasi Feb 24 '11 at 08:57
  • Well, that should be verified by the `cache.get` in the same session as `cache.set`, unless there's another caching layer hidden in between. – mandrake Feb 24 '11 at 09:53
  • Did you solve this issue ? – Azd325 May 16 '13 at 10:34

1 Answers1

2

Django's cache system is an abstraction layer for several different cache backends. Although it allows you to interact with them using the same API they will behave differently depending on which one you have configured. See the documentation for the full details.

You configure which backend to use using the CACHE_BACKEND setting in your settings.py file. If you don't set that setting then you'll get a simple, in-process, cache which would explain why you're not able to access cache values set in other processes. I would suggest that you look at memcached and use that your backend. It's very fast, very scalable and also very easy to configure.

Andrew Wilkinson
  • 10,682
  • 3
  • 35
  • 38
  • The django settings module is automatically loaded when `django.core.cache` is imported (it won't import if the `DJANGO_SETTINGS_MODULE` env.var is not set correctly), and if you read my question I say that I tested both memcached and filebased backends. – mandrake Feb 24 '11 at 15:38
  • Sounds like issue with cache configuration in that case. – chhantyal Dec 27 '17 at 20:21