项目之前使用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的更多相关文章

  1. 在django中使用redis

    方式一 utils文件夹下,简历redis_pool.py import redis POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,p ...

  2. CentOS7部署Django,nginx,uwsgi,redis

    前期准备 把所有的软件都传到这个tools文件夹 cd - mkdir tools cd tools/ mkdir /application 安装nginx yum install pcre pcre ...

  3. 用Python来操作redis 以及在Django中使用redis

    什么是Redis? Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure server). Redis的键值可以包 ...

  4. python框架之Django(16)-接入Redis

    准备 安装Redis 参考 Ubuntu 中 Redis 的安装与使用. 在python中使用Redis 参考 python 中使用 Redis . 安装依赖包 在 Django 中接入 Redis ...

  5. django中使用redis

    第一种 安装redis模块 1.1在app中定义一个redis的连接池的py文件 import redis POOL=redis.ConnectionPool(host='127.0.0.1',por ...

  6. Django 缓存 使用 Redis Memcached 为网站提速

    RedisRedis是一种键值对类型的内存数据库,读写内存比读写硬盘快,我们在Django里面使用Redis非常方便,下面给出详细步骤 基于Ubuntu 1. 安装Redis和django-redis ...

  7. Django之使用redis缓存session,历史浏览记录,首页数据实现性能优化

    Redis缓存session 配置Django缓存数据到redis中 # diango的缓存配置 CACHES = { "default": { "BACKEND&quo ...

  8. Django 中使用redis

    Django使用redis   方式一,使用Django-redis模块 #安装: pip3 install django-redis CACHES = { "default": ...

  9. python连接redis、redis字符串操作、hash操作、列表操作、其他通用操作、管道、django中使用redis

    今日内容概要 python连接redis redis字符串操作 redis之hash操作 redis之列表操作 redis其他 通用操作,管道 django中使用redis 内容详细 1.python ...

  10. Redis使用和部分源码剖析以及Django缓存和redis的关系

    0.特点: a.持久化 b.单进程.单线程 c.5大数据类型    d.用于操作内存的软件.    e.虽然是缓存数据库但是可以做持久化的工作 MySQL是一个软件,帮助开发者对一台机器的硬盘进行操作 ...

随机推荐

  1. 开源:ASP.NET MVC+EF6+Bootstrap开发框架

    前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿10个G的精神粮食从一个博客园大神那里换来一套开发框架,正式走上开发之路 ...

  2. JQuery fullCalendar 时间差 排序获取距当前最近的时间。

    let time = (wo: WoDto) => wo.ScheduleTime || wo.ScheduleStartTime; let wo = technician.wos .filte ...

  3. [HDOJ5445]Food Problem(优先队列优化多重背包)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5445 题意:多重背包 分析:f[i][j]=max(f[i-1][j-k*w[i]]+k*v[i]) 将j ...

  4. 52-which 显示系统命令所在目录

    显示系统命令所在目录 which command-list 参数 command-list 是which搜索的一条或多条命令(实用程序) 示例 which 单条命令 $ which ls /bin/l ...

  5. 各组对final发布产品的排名

    结果 排名 组名 项目简称 组长 平均 方差 1 新蜂 俄罗斯 武志远 2 0.80 2 天天向上 连连看 王森 2.50 1.90 3 奋斗吧兄弟 食物链 黄兴 2.83 0.97 4 金洲勇士 考 ...

  6. cxf和spring结合,发布restFull风格的服务

    rest(Representational State Transfer):表现层状态转化,它是一种风格,用于资源定位,例如:http://ip:port/user/student/001 和资源操作 ...

  7. SpringMVC学习--拦截器

    简介 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 拦截器定义 定义拦截器,实现HandlerInterceptor接 ...

  8. android中通话录音

    file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMil ...

  9. Linux14.04安装Mysql Linux公社

    今天在Ubuntu 14.04下安装MySQL,本来是去官网下载安装包来安装的,可是安装之后却不能用,估计是要配置吧,在网上搜了很多的资料,结果还是失败.所以只好在软件源中安装,这样就省去很多不必要的 ...

  10. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...