项目之前使用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. 在Windows上将ReactNative集成到现有的Android项目

    React Natvie的官方文档的 Integrating with Existing Apps 已经很详细地教我们如何将React Natvie集成到现在的Android项目.我根据官方文档的步骤 ...

  2. 通过UserAgent判断设备为Android、Ios、Pc访问

    public static bool CheckAgent() { bool flag = false; string agent = HttpContext.Current.Request.User ...

  3. [CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)

    题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...

  4. vijos-1003等价表达式

    明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的要求是判断选项中哪些代数表达式是和 ...

  5. Android四大组件之Activity详解——传值和获取结果

    废话不多说,先来看效果图 项目源码: http://download.csdn.net/detail/ginodung/8331535 程序说明: 在MainActivity中输入用户名和密码,然后提 ...

  6. SpringMVC学习--参数绑定

    spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...

  7. git 冲突解决

    冲突文件的组成 "<<< HEAD"和 "====="之间的为主干内容 "=====" 和 ">>& ...

  8. poj3294 出现次数大于n/2 的公共子串

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13063   Accepted: 3670 Descr ...

  9. WebApi服务Uri加密及验证的两种方式

    最近的一个项目要求服务端与UI层分离,业务层以WebApi方式向外提供所有业务服务,服务在数据保密性方面提出了要求,主要包括: 1:客户端认证: 2:服务请求超时(默认5分钟): 3:服务Get请求的 ...

  10. Java算法-插入排序

    插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排好序,本趟需要找到 i 对应的元素 x 的正确位置 k ,并且在寻找这个位置 k 的过程中逐个将比较过的 ...