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/
django-redis和redis-py的更多相关文章
- 在django中使用redis
方式一 utils文件夹下,简历redis_pool.py import redis POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,p ...
- CentOS7部署Django,nginx,uwsgi,redis
前期准备 把所有的软件都传到这个tools文件夹 cd - mkdir tools cd tools/ mkdir /application 安装nginx yum install pcre pcre ...
- 用Python来操作redis 以及在Django中使用redis
什么是Redis? Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure server). Redis的键值可以包 ...
- python框架之Django(16)-接入Redis
准备 安装Redis 参考 Ubuntu 中 Redis 的安装与使用. 在python中使用Redis 参考 python 中使用 Redis . 安装依赖包 在 Django 中接入 Redis ...
- django中使用redis
第一种 安装redis模块 1.1在app中定义一个redis的连接池的py文件 import redis POOL=redis.ConnectionPool(host='127.0.0.1',por ...
- Django 缓存 使用 Redis Memcached 为网站提速
RedisRedis是一种键值对类型的内存数据库,读写内存比读写硬盘快,我们在Django里面使用Redis非常方便,下面给出详细步骤 基于Ubuntu 1. 安装Redis和django-redis ...
- Django之使用redis缓存session,历史浏览记录,首页数据实现性能优化
Redis缓存session 配置Django缓存数据到redis中 # diango的缓存配置 CACHES = { "default": { "BACKEND&quo ...
- Django 中使用redis
Django使用redis 方式一,使用Django-redis模块 #安装: pip3 install django-redis CACHES = { "default": ...
- python连接redis、redis字符串操作、hash操作、列表操作、其他通用操作、管道、django中使用redis
今日内容概要 python连接redis redis字符串操作 redis之hash操作 redis之列表操作 redis其他 通用操作,管道 django中使用redis 内容详细 1.python ...
- Redis使用和部分源码剖析以及Django缓存和redis的关系
0.特点: a.持久化 b.单进程.单线程 c.5大数据类型 d.用于操作内存的软件. e.虽然是缓存数据库但是可以做持久化的工作 MySQL是一个软件,帮助开发者对一台机器的硬盘进行操作 ...
随机推荐
- lecture10-模型的结合与全贝叶斯学习
这是Hinton的第10课 这节课有两篇论文可以作为背景或者课外读物<Adaptive mixtures of local experts>和<Improving neural ne ...
- DOM与CSS样式表
在前文 <DOM与元素节点内联样式>中我们了解了用 DOM 提供的接口操作元素节点内联样式的方法,今天我们来学习一下如何用 DOM 操作 CSS 样式表. CSS 样式表概况 通过使用 H ...
- SQL基础之数据库
1.基础概念 首先要强调一点,就是我们的数据库是由数据库系统来管理的,我们登入数据库并在其上进行操作时最终均要通过数据库系统来完成.可以理解成在数据库上进行操作的是客户端,数据库系统是服务端.一个数据 ...
- Chrome 监听 console 打开
这个算是 Chrome only 其他的我没测试,也不想测试.因为我的控制台脚本仅仅在 Chrome 下加载. 如果你需要全平台,那么这肯定不是你需要的结果. 需求 其实我很早就想折腾这个了,但是,, ...
- Windows Azure 云服务角色架构
当我们使用VS发布一个Cloud Service或者在Portal上上传发布包后,就能启动和运行一个云服务,可以保护WebRole,WorkerRole的一个或者多个实例. Windows Azure ...
- 北大OJ 1001题
题目:输入一序列的正实数和幂次(正整数)对,然后打印结果(具体的比这个精细) 这道题是关于大数计算的(大数求幂),从开始建立思路,到写代码.调式到最后被AC以及最终的优化,总共用了差不多一天的时间.开 ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- canvas边界与摩擦力
处理物体超出画布时的三种基本状态,复位,移除,反弹 (1)检测是否越界的核心算法 if( object.x - object.width / 2 > right || object.x + ob ...
- 你需要知道的MySQL开源存储引擎TokuDB
在四月份的Percona Live MySQL会议上, TokuDB庆祝自己成为开源存储引擎整一周年.我现在仍能记得一年前它刚创建时的官方声明与对它的期望.当时的情况非常有意思,因为它拥有帮助MySQ ...
- jQuery基础--样式篇(5)
jQuery的属性与样式 (1).attr()与.removeAttr():每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息. attr()有4个表达式 attr(传入 ...