django-redis和redis-py

项目之前使用memcache做缓存,现在转到redis,改写几个语句的事情,然后就这种我把django-redis和py-redis搞混了,记录一下。

django默认使用memcache做缓存,这里的操作一般是cache.get()  cache.set()这种,要想操作使用from django.core.cache import cache就可以了。

具体安装及操作见:http://blog.beginman.cn/blog/83/  (好像跑题了啊喂)

django现在可以使用redis做缓存,但是使用cache默认不能操作redis,这个时候就出现了django-redis了,一个开源的。

这个是要安装的,然后配置就可以使用了,但是据说性能不够高,官方文档见https://niwinz.github.io/django-redis/latest/

而py-redis是一个python的库,用来操作redis,效率已经不错了,操作也比较简单。

类似与import redis              r.set()   r.get()   r.lpush()  r.lpop() 这种操作。

文档见:https://github.com/andymccurdy/redis-py

r.rpop() r.rpush()

示例:

In [2]: import redis

In [3]: r = redis.StrictRedis(host='localhost', port=6379, db=0)

In [4]: r.set('a', 'abc')
Out[4]: True

In [5]: r.get('a')
Out[5]: 'abc'

In [6]: r.get('b')

In [7]: r.setex('b', 30, 'bcd')   # 设置过期时间,第一次读在30s内,有结果,第二次过了30s就没有内容了
Out[7]: True

In [8]: r.get('b')
Out[8]: 'bcd'

In [9]: r.get('b')

In [10]: r.lpush('l', 1)
Out[10]: 1L

In [11]: r.lpop('l')
Out[11]: '1'

In [12]: r.llen()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-e38b57a801ea> in <module>()
----> 1 r.llen()

TypeError: llen() takes exactly 2 arguments (1 given)

In [13]: r.llen('l')
Out[13]: 0

In [14]: 

  

In [20]: r.delete('a')
Out[20]: 1

In [21]: r.get('a')

  

In [22]: r.exists('a')
Out[22]: False

  

In [23]: pipline = r.pipeline()                                                                                                                                                                   

In [24]: pipline.se
pipline.sentinel                          pipline.sentinel_monitor                  pipline.sentinel_slaves                   pipline.setex
pipline.sentinel_get_master_addr_by_name  pipline.sentinel_remove                   pipline.set                               pipline.setnx
pipline.sentinel_master                   pipline.sentinel_sentinels                pipline.set_response_callback             pipline.setrange
pipline.sentinel_masters                  pipline.sentinel_set                      pipline.setbit                            

In [24]: pipline.set('a', 'aaaaaaaaaaaaaaaa')
Out[24]: StrictPipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>

In [25]: pipline.set('b', 'bbbbbbbbbbbbbbbb')
Out[25]: StrictPipeline<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>

In [26]: pipline.execu
pipline.execute          pipline.execute_command  

In [26]: pipline.execute()
Out[26]: [True, True]

  

Pipelines are a subclass of the base Redis class that provide support for buffering multiple commands to the server in a single request. They can be used to dramatically increase the performance of groups of commands by reducing the number of back-and-forth TCP packets between the client and server.

参考见:

 https://github.com/andymccurdy/redis-py

https://redis-py.readthedocs.io/en/latest/

原文地址:https://www.cnblogs.com/wswang/p/5460070.html