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是一个软件,帮助开发者对一台机器的硬盘进行操作 ...
随机推荐
- Tomcat 项目部署方式
方法一:在Tomcat中的Conf目录中,在Server.Xml中的,<Host/>节点中添加: <Context Path="/Hello"Docbase=&q ...
- 采访ServiceStack的项目领导Demis Bellot——第1部分(网摘)
ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第1部分中,我 ...
- [译]用AngularJS构建大型ASP.NET单页应用(三)
原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single A ...
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
Jquery Ajax方法传值到action <script type="text/javascript"> $(document).ready(function(){ ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- MyEclipse10连接数据库
连接oracle数据库 DB窗口>>右键:新建
- CSS选择器优先级排列
CSS选择器的效率从高到低做了一个排序: 1.id选择器(#myid) 2.类选择器(.myclassname) 3.标签选择器(div,h1,p) 4.相邻选择器(h1+p) 5.子选择器(ul & ...
- confluence的安装、备份和恢复(wiki)
还有一种比较不错的wiki工具MediaWiki 安装教程参考 http://pangge.blog.51cto.com/6013757/1560249 我是按照上面的教程搭建的 还有几篇不错的文章 ...
- vmware 在NAT模式下连接上外网
文章: http://www.2cto.com/os/201504/389011.html
- iOS开发小技巧--实现毛玻璃效果的方法
一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...