Skip to content

Django 1.6 with Redis Cloud

Rodrigo Ancavil del Pino edited this page Mar 27, 2014 · 5 revisions

If you want or need Redis.IO in your Django application,m you can use Redis-Cloud.

First, you need create your first database in redis-cloud site. Here will be created the next URL:

 Endpoint: YOUR_REDISCLOUD_HOSTNAME:YOUR_REDISCLOUD_DB_PORT

For the use of Redis with Django, you need install additional packages:

  • django-redis-cache
  • hiredis

For install this packages edit the setup.py file and put the names in the list:

 packages = ['Django<=1.6',
              'static3',  # If you want serve the static files in the same server
              'django-redis-cache',
              'hiredis',
               #  'mysql-connector-python',
               #  'pymongo',
               #  'psycopg2',
  ]

You only need configure:

 rhc env set REDISCLOUD_URL=YOUR_REDISCLOUD_HOSTNAME --app djangopy3
 rhc env set REDISCLOUD_PORT=YOUR_REDISCLOUD_DB_PORT --app djangopy3
 rhc env set REDISCLOUD_PASSWORD=YOUR_REDISCLOUD_DB_PASS --app djangopy3

Then make:

 git push

For more details, see the settings.py file where redis-cloud is configurated:

Here, the settings.py file uses the environ variables previously configurated. You must not do anything, just see :).

 # If you want configure the REDISCLOUD
 if 'REDISCLOUD_URL' in os.environ and 'REDISCLOUD_PORT' in os.environ and 
     'REDISCLOUD_PASSWORD' in os.environ:
       redis_server = os.environ['REDISCLOUD_URL']
       redis_port = os.environ['REDISCLOUD_PORT']
       redis_password = os.environ['REDISCLOUD_PASSWORD']
       CACHES = {
          'default' : {
              'BACKEND' : 'redis_cache.RedisCache',
              'LOCATION' : '%s:%d'%(redis_server,int(redis_port)),
              'OPTIONS' : {
                  'DB':0,
                  'PARSER_CLASS' : 'redis.connection.HiredisParser',
                  'PASSWORD' : redis_password,
              }
           }
       }
       MIDDLEWARE_CLASSES = ('django.middleware.cache.UpdateCacheMiddleware',) + 
                            MIDDLEWARE_CLASSES + 
                           ('django.middleware.cache.FetchFromCacheMiddleware',)
Clone this wiki locally