A detailed description of the usage of the Redis cache server in Django

Source: Internet
Author: User
Tags install django pip install django install redis
Redis believe that everyone is familiar with, and memcached is a high-performance Key-value database, as to what is the cache server, degrees Niang have a very clear introduction, I do not introduce here.

So what are we going to do with a cache server in general? It's not exactly what you need. Oh, in general, it is necessary to read a field frequently when you need to put this field on the cache server, and since the Key-value database is generally only easy to put data, so in the selection of the saved object should be careful to choose.

I'll show you how to configure a Redis database in Django, first by installing Redis and executing the following command in Ubuntu:

#安装Redis服务器端

sudo apt-get install Redis-server

Then, to be able to use Redis in Django, you also need to install the Redis for Django plugin:

Pip Install Django-redis

This is an open source project, the GitHub address is Https://github.com/niwibe/django-redis, thanks to the author.

So now it's configured in Django settings.

CACHES = {    ' default ': {        ' backend ': ' Redis_cache.cache.RedisCache ',        ' location ': ' 127.0.0.1:6379 ',        ' Options ": {            " Client_class ":" Redis_cache.client.DefaultClient ",        },    },}redis_timeout=7*24*60*60cubes_ Redis_timeout=60*60never_redis_timeout=365*24*60*60

In fact, just need to caches in the few can, the following three sentences can not need, but I need to use the example behind, I am here to configure.

OK, now that the connection and configuration are done, how do you use it in your project? Now let's look at the following example.

From django.conf import settingsfrom django.core.cache import cache#read Cache user Iddef Read_from_cache (self, user_name ):    key = ' user_id_of_ ' +user_name    value = Cache.get (key)    if value = = None:        data = none    else:        data = Json.loads (value)    return data#write cache user Iddef Write_to_cache (self, user_name):    key = ' user_id_of _ ' +user_name    cache.set (Key, Json.dumps (user_name), settings. Never_redis_timeout)

With these two methods above, you can implement the read operation of Redis, just need to pass the required fields as parameters into the method.

What about the memcached mentioned before? In fact, it is the same configuration:

CACHES = {    ' default ': {        ' backend ': ' Django.core.cache.backends.memcached.MemcachedCache ',        ' location ': ' 127.0.0.1:11211 ',    }}

Of course the usage is the same as the example above me. In fact, for a cache server such as Redis, configuration is very simple, and the specific use is not difficult, the official web site also has a lot of simple and clear examples for our reference, there is only one thing to note, that is, what kind of information to be saved to Redis is what we really need to care about.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.