Time has not been updated, this period of time has been busy with a project, today to record a now often used technology bar.
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 Install Redis-server
Then, to be able to use Redis in Django, you also need to install the Redis for Django plugin:
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.
fromDjango.confImportSettings fromDjango.core.cacheImportCache#read cache User IDdefRead_from_cache (Self, user_name): Key='user_id_of_'+user_name Value=cache.get (Key)ifValue = =None:data=NoneElse: Data=json.loads (value)returnData#Write cache User IDdefWrite_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 " : { ' : ' '
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.
Using the Redis cache server for Django analysis