Weird django caching problem

210 views
Skip to first unread message

philippw

unread,
Jun 21, 2011, 4:20:44 AM6/21/11
to Django users
hi.

i have weird caching problems with the 1.3 version of django. I
probably have something configured wrong, but am not sure what.

A good example is django-avatar, which uses caching and many people
use it. Even if I dont have a cache backend defined the avatar seems
to be cached, which by itself would be ok, but it keeps switching back
and forth between the last values cached. Example: I upload a new
avatar, now on approximately 50% of the requests it will show me the
new one, 50% the old one. If I delete the old one I still get it on
the site 50% of the time. The only way to fix it is to disable the
caching of the avatar by setting it to one second.

First I thought it was because i used
django.core.cache.backends.locmem.LocMemCache, which I never used
before, but it even happens when I dont configure a cache backend at
all.

I found one similar bug:
http://stackoverflow.com/questions/4888470/django-caching-bug-even-if-caching-is-disabled

but my pages render just fine, its the templatetags (for now) that
cause the problems in my setup.

I use django 1.3, postgres, nginx, gunicorn 0.12.0, greenlet==0.3.1,
eventlet==0.9.16

I just did some more testing and realized that it only happens when I
start gunicorn using the config file. If I start it with ./manage.py
run_gunicorn everything is fine. Running "gunicorn_django -c deploy/
gunicorn.conf.py" causes the problems.

The only explanation I can think of is that each worker gets his own
cache (I wonder why, since I did not define a cache).

Update: running ./manage.py run_gunicorn -w 4 also causes the same
problems. Therefore I am almost certain that the multiple workers are
causing the problems and each worker caches the values seperately.

My configuration:

import os
import socket
import sys

PORT = 8000
PROC_NAME = 'myapp_gunicorn'
LOGFILE_NAME = 'gunicorn.log'
TIMEOUT = 3600
IP = '127.0.0.1'
DEPLOYMENT_ROOT = os.path.dirname(os.path.abspath(__file__))
SITE_ROOT = os.path.abspath(os.path.sep.join([DEPLOYMENT_ROOT, '..']))
CPU_CORES = os.sysconf("SC_NPROCESSORS_ONLN")
sys.path.insert(0, os.path.join(SITE_ROOT, "apps"))
bind = '%s:%s' % (IP, PORT)
logfile = os.path.sep.join([DEPLOYMENT_ROOT, 'logs', LOGFILE_NAME])
proc_name = PROC_NAME
timeout = TIMEOUT
worker_class = 'eventlet'
workers = 2 * CPU_CORES + 1

-----------------------------------
I also tried it without using 'eventlet', but got the same errors.


Thanks for any help.
Philipp

Tom Evans

unread,
Jun 21, 2011, 6:45:23 AM6/21/11
to django...@googlegroups.com
On Tue, Jun 21, 2011 at 9:20 AM, philippw <philipp.w...@gmail.com> wrote:
> hi.
>
> i have weird caching problems with the 1.3 version of django. I
> probably have something configured wrong, but am not sure what.
>

You don't mention how you have configured caching, which is relevant.
By default, if you do not configure a a cache Django will use the
'locmem' cache, which uses per-process local memory to act as a cache,
which means you get one cache per process, and consequently the
behaviour you have described.

If you don't want a cache, but want to use code that uses the cache
framework, you must configure Django to use DummyCache.

All of this is documented in the caching documentation:
https://docs.djangoproject.com/en/1.3/topics/cache/

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages