user1592380
user1592380

Reputation: 36205

Redis cache not working with django

I have a django project on an ubuntu EC2 node, and I want to set up a cache, I am following http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/ to use redis for this. In the article the author refers to https://docs.djangoproject.com/en/1.7/topics/cache/ and based of that I can do:

(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ python manage.py shell
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> import redis
>>> from django.core.cache import cache
>>> cache.set('my_key','hi world')
>>> cache.get('my_key')
'hi world'

My current django view contains;

def index(token):

    html = calculator(token)
    print('here1')

    import redis
    from django.core.cache import cache
    cache.set('my_key', 'hello, world!', 60*60*12)
    print('here2')

    return html

However when I trigger the index function, nothing is saved to the cache. I checked after from the command line.

How can I get the cache working?

edit:

>>> print(settings.CACHES)
{'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}

Upvotes: 1

Views: 6936

Answers (2)

Yauheni Leaniuk
Yauheni Leaniuk

Reputation: 456

Updated version of answer of Raphaël Braud:

 'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },

Upvotes: 1

Raphaël Braud
Raphaël Braud

Reputation: 1519

The key point is your CACHES configuration, it should be :

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },
}

(cf http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/)

Upvotes: 5

Related Questions