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是一个软件,帮助开发者对一台机器的硬盘进行操作 ...
随机推荐
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [转]Android Studio创建Xposed模块项目时BridgeApi的正确添加方式
使用Android Studio创建的空项目作为Xposed Module App,对于Api Jar包的引用方式,一开始是按照傻瓜式Jar Lib的处理方式,复制XposedBridgeApi-54 ...
- MySQL5.6 实现主从复制,读写分离,分散单台服务器压力
闲来无事,在本地搭建几台虚拟机,准备配一个mysql读写分离的主从配置,版本选用最新版的,mysql.5.6.28 版本,本处使用源码安装(鄙人一向喜欢源码安装,因为centos中鄙人不知道yum安装 ...
- Castle 多继承选择
Castle 多继承选择 很多时候,我们定义了一个接口,但是这个接口会有多种不同的,这时IOC构造函数注入的时候,就需要自动选择对应的实现. public interface ITestService ...
- C++学习准则
C++学习准则 1.把C++当成一门新的语言学习(和C没啥关系!真的): 2.看<Thinking In C++>,不要看<C++变成死相>(C++编程思想,翻译的非常差): ...
- IE6下margin时,float浮动产生双倍边距
今天遇到了一个IE6下的兼容性问题,虽然IE6已经不被大众所期待了,用户也已基本上消失的所剩无几,但是,作为一个问题而存在,我们有必要尝试的去研究一下bug的改善方法 对元素float-left,然后 ...
- json注意:
import json #dct="{'1':111}"#json 不认单引号 #dct=str({"1":111})#报错,因为生成的数据还是单引号:{'on ...
- SpringMVC学习--功能完善
简介 在基本的项目中,无非就是基本的增删改查,前面我们已经实现了一个简单的查询功能,现在来实现增删改功能,来了解实际开发中的运用,以修改功能为例,因为修改功能基本覆盖了增加和删除的运用. 前面我们实现 ...
- Linux vsftp配置本地用户
主要讲的是配置本地用户, ftp现在用的也少了,一般都用ssh和svn 1. 安装ftp yum -y install vsftpd 2. 配置 /etc/vsftpd/vsftpd.conf # ...
- iOS开发小技巧-修改SliderBar指针的样式(牢记这个方法,只能通过代码来修改)
代码: // 修改进度条的指针图片 [self.progressSlider setThumbImage:[UIImage imageNamed:@"player_slider_playba ...