Saturday, January 29, 2011

Re: Django cache created in one function not available to another?

On Sat, Jan 29, 2011 at 9:30 PM, Rob Hudson <> wrote:
> This sounds similar to this bug :

If the OP is using Django 1.2.3, this probably isn't #15179 -- Jeff
reports that if he rolls back to before the recent key prefixing
changes, his problem doesn't exist. Those changes don't exist on the
1.2.X branch.

> On Jan 28, 8:02 am, Lance Vick <> wrote:
>> So I have a function that sets cache (and successfully outputs it):
>> def push_cache:
>>     cache.set('foo_cache', 'FUBAR!!!')
>>     foodata = cache.get('foo_cache')
>>     print(foodata) # this works
>> However when I try to access that cache from within a view and
>> directly output it it returns None:
>> def fetch_cache(request):
>>     cache_data = cache.get('foo_cache')
>>     return HttpResponse(cache_data) #this does not work.
>> Am i misunderstanding something fundamental in how cache works?
>> I am using the memcached backend in django 1.2.3

The fundamental calls to caching are correct usage, but your usage is
missing something important -- caches aren't guaranteed to return a
value. Cache values expire, so your code *must* allow for the case
where the cache returns nothing.

That said, if you're pushing a value into the cache, and you're not
getting the value coming out on the fetch, there could be a couple of

* If memcached isn't running (or nor running correctly, or not
running on the right port, or not running on a port that is allowing
connections, etc), caching will fail, returning empty results.

* If the default timeout for the cache is short, the cache may be
expiring before the fetch operation is performed.

We'd need to see your cache settings to confirm the second issue; the
first issue is something you'll have to investigate yourself.

The easiest way to test this is to fire up a python shell prompt and
try using the cache programatically.

You might also want to try using a different cache backend -- that
will allow you to work out if the problem is your usage of caching, or
your memcache configuration.

Russ Magee %-)

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment