介绍:
Redis:非关系型缓存数据库
-
nosql:非关系型数据库
- 没有表,没有表与表之间的关系,更不存在外键
- 存储数据的形式为key:values的形式
- c语言写的服务(监听端口),用来存储数据的,数据是存储在内存中,取值,放值速度非常快, 10w qps
-
缓存数据库:
- 数据存储在内存中
- 大部分时间用于缓存,也可以长久存储数据
-
redis速度为什么这么快:
- 纯内存操作
- 网路模型使用的IO多路复用(可以处理的请求更多)
- 6.x版本之前,单进程,单线程,不存在进程线程之间切换,更少消耗资源
1、redis的安装与运行
版本选择:
- mac:源码编译安装
- linux:源码编译安装
- windows:
- redis没有windows的版本,只有微软基于源码改动,编译的安装包(用法一样)
- 3.x版本 Releases · microsoftarchive/redis · GitHub
- 最新5.x版本 Releases · tporadowski/redis · GitHub
安装:
redis需要了解的目录结构:
- redis-server :服务端启动文件
- redis-cli:客服端启动文件
- redis.windows-service.conf:默认使用的配置文件
- bind 127.0.0.1 :服务端跑在的地址
- port 6379 :占用的端口号
启动redis:
- 方式一:将服务端添加至系统服务(电脑运行自动启动)
* - 方式二:cmd终端使用命令启动
- redis-server 指定配置文件 # 如果不指定,会默认
客服端链接redis:
-
cmd终端使用命令
-
方式一:redis-cli 默认链接本地的6379端口
-
方式二:redis-cli -h 地址 -p 端口
-
2、RESP图形化操作文件
可以使用软件:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python">Redis Desktop Manager :开源的,原来免费,后来收费了 推荐用(mac<span style="color:#393a34">,</span>win<span style="color:#393a34">,</span>linux 都有)
<span style="color:#393a34">-</span>Qt5 qt是个平台,专门用来做图形化界面的
<span style="color:#393a34">-</span>可以使用c<span style="color:#393a34">+</span><span style="color:#393a34">+</span>写
<span style="color:#393a34">-</span>可以使用python写 pyqt5 使用python写图形化界面 <span style="color:#393a34">(</span>少量公司再用<span style="color:#393a34">)</span>
<span style="color:#393a34">-</span>resp<span style="color:#393a34">-</span><span style="color:#36acaa">2022.1</span><span style="color:#36acaa">.0</span><span style="color:#36acaa">.0</span><span style="color:#393a34">.</span>exe 一路下一步,安装完启动起来
<span style="color:#393a34">-</span>Redis Client 小众
</code></span></span>
RESP操作redis:
二、pycharm操作redis
安装模块:
使用pycharm操作redis,pycharm相当于客户端,输入redis命令即可对数据库进行操作
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 下载模块:</em></span>
pip install redis
<span style="color:#008000"><em># 补充:django 中操作mysql,没有连接池的,一个请求就是一个mysql连接</em></span>
<span style="color:#393a34">-</span>可能会有问题,并发数过高,导致mysql连接数过高,影响mysql性能
<span style="color:#393a34">-</span>使用django连接池:https<span style="color:#393a34">:</span><span style="color:#393a34">//</span>blog<span style="color:#393a34">.</span>51cto<span style="color:#393a34">.</span>com<span style="color:#393a34">/</span>liangdongchang<span style="color:#393a34">/</span><span style="color:#36acaa">5140039</span>
</code></span></span>
1、Redis普通连接和连接池
普通链接:
普通链接是指,直接操作redis,每一次操作都会建立一条链接
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 导入模块</em></span>
<span style="color:#0000ff">from</span> redis <span style="color:#0000ff">import</span> Redis
<span style="color:#008000"><em># 实例化对象(后方参数填写链接的地址和端口,还可以填写其他参数)</em></span>
conn <span style="color:#393a34">=</span> Redis<span style="color:#393a34">(</span>host<span style="color:#393a34">=</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span> port<span style="color:#393a34">=</span><span style="color:#36acaa">6379</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 向redis中存放一个数据</em></span>
conn<span style="color:#393a34">.</span>set<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'kangkang'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 从redis中取出存放的数据(二进制格式)</em></span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 操作完成,手动关闭,不然会一直占用一条链接</em></span>
conn<span style="color:#393a34">.</span>close<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
连接池连接:
拿到一个Redis实例的连接池,避免每次建立、释放连接的开销,节省了每次连接用的时间,文中设置了最大10个。
POOL.py
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 1、使用单例模式(保证每次使用的都是同一个实例对象),设置连接池最大为10个</em></span>
<span style="color:#0000ff">import</span> redis
POOL <span style="color:#393a34">=</span> redis<span style="color:#393a34">.</span>ConnectionPool<span style="color:#393a34">(</span>
<span style="color:#008000"><em># 最大链接</em></span>
max_connections<span style="color:#393a34">=</span><span style="color:#36acaa">10</span><span style="color:#393a34">,</span>
<span style="color:#008000"><em># 指定服务端地址</em></span>
host<span style="color:#393a34">=</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span>
prot<span style="color:#393a34">=</span><span style="color:#36acaa">6379</span>
<span style="color:#393a34">)</span>
</code></span></span>
my_redis.py
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 导入redis</em></span>
<span style="color:#0000ff">from</span> redis <span style="color:#0000ff">import</span> Redis
<span style="color:#008000"><em># 导入线程模块(模仿多线程并发)</em></span>
<span style="color:#0000ff">from</span> threading <span style="color:#0000ff">import</span> Thread
<span style="color:#008000"><em># 导入自己创建的单例</em></span>
<span style="color:#0000ff">from</span> script<span style="color:#393a34">.</span>pool <span style="color:#0000ff">import</span> POOL
<span style="color:#008000"><em># 写一个函数,在内部操作redis</em></span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">task</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
<span style="color:#008000"><em># 以后生成的对象都是POLL这一个实例化的对象</em></span>
conn <span style="color:#393a34">=</span> Redis<span style="color:#393a34">(</span>connection_pool<span style="color:#393a34">=</span>POOL<span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>set<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'kangkang'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
<span style="color:#0000ff">for</span> i <span style="color:#0000ff">in</span> range<span style="color:#393a34">(</span><span style="color:#36acaa">100</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
t <span style="color:#393a34">=</span> Thread<span style="color:#393a34">(</span>target<span style="color:#393a34">=</span>task<span style="color:#393a34">)</span>
t<span style="color:#393a34">.</span>start<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
2、Redis数据类型
介绍:
redis 是key-value形式存储
redis 数据放在内存中,如果断电,数据丢失---》需要有持久化的方案
类型 | 介绍 |
---|---|
字符串(string) | 用的最多,做缓存;做计数器 |
列表(list) | 常用于消息队列 |
字典(hash) | 缓存 |
集合(set) | 去重 |
有序集合(zset) | 排行榜 |
2、1.String类型
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 1、set(name, value, ex=None, px=None, nx=False, xx=False)</em></span>
ex:过期时间(秒)
px:过期时间(毫秒)
nx:如果设置为<span style="color:#36acaa">True</span>,则只有name不存在时,当前set操作才执行<span style="color:#393a34">,</span> 值存在,就修改不了,执行没效果
xx:如果设置为<span style="color:#36acaa">True</span>,则只有name存在时,当前set操作才执行,值存在才能修改,值不存在,不会设置新值
<span style="color:#008000"><em># 2、setnx(name, value) </em></span>
等同于set<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'kangkang'</span><span style="color:#393a34">,</span>nx<span style="color:#393a34">=</span><span style="color:#36acaa">True</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 3、setex(name,time,values)</em></span>
等同于set<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#36acaa">10</span><span style="color:#393a34">,</span><span style="color:#a31515">'kangkang'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 4、psetex(name,time_ms,values)</em></span>
conn<span style="color:#393a34">.</span>psetex<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#36acaa">3000</span><span style="color:#393a34">,</span><span style="color:#a31515">'kangkang'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 5、mset(*args,**kwargs)</em></span>
可以批量存入数据
conn<span style="color:#393a34">.</span>mset<span style="color:#393a34">(</span><span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span><span style="color:#a31515">'kangkang'</span><span style="color:#393a34">,</span><span style="color:#a31515">'age'</span><span style="color:#393a34">:</span><span style="color:#36acaa">18</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 6、get(name)</em></span>
取值<span style="color:#393a34">(</span>单个<span style="color:#393a34">)</span>
coon<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 7、mget(keys, *args)</em></span>
批量取值(可以是列表)
coon<span style="color:#393a34">.</span>mget<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'age'</span><span style="color:#393a34">)</span>
coon<span style="color:#393a34">.</span>mget<span style="color:#393a34">(</span><span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'age'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 8、getset(name, value)</em></span>
放置新的值进入(可以使用变量接收被替换的值)
res <span style="color:#393a34">=</span> coon<span style="color:#393a34">.</span>getset<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'jason'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 9、getrange(key, start, end)</em></span>
按照指定key对应值的索引位置取值(按字节取值)
res <span style="color:#393a34">=</span> coon<span style="color:#393a34">.</span>gettrange<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#36acaa">0</span> <span style="color:#393a34">,</span><span style="color:#36acaa">3</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 10、settrange(name, offset, value)</em></span>
按照索引位置替换指定key对应的值(按照字节)
coon<span style="color:#393a34">.</span>settrange<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#36acaa">3</span><span style="color:#393a34">,</span><span style="color:#a31515">'bbb'</span><span style="color:#393a34">)</span>
<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span> 比特位<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span>操作<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span>
<span style="color:#008000"><em># 11 setbit(name, offset, value)</em></span>
<span style="color:#008000"><em># 12 getbit(name, offset)</em></span>
<span style="color:#008000"><em># 13 bitcount(key, start=None, end=None)</em></span>
<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span> 比特位<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span>操作<span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span><span style="color:#393a34">-</span>
<span style="color:#008000"><em># 14、bitop(operation, dest, *keys)</em></span>
获取多个值,并将值做位运算,将最后的结果保存至新的name对应的值
<span style="color:#008000"><em># 15、strlen(name)</em></span>
获取指定key对应值的位数(按字节)
res <span style="color:#393a34">=</span> coon<span style="color:#393a34">.</span>strlen<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 16、incr(self, name, amount=1)</em></span>
自增(默认自增:<span style="color:#36acaa">1</span>,不支持含有浮点类型)
conn<span style="color:#393a34">.</span>incr<span style="color:#393a34">(</span><span style="color:#a31515">'age'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 17、incrbyfloat(self, name, amount=1.0)</em></span>
自增<span style="color:#393a34">(</span>支持小数<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 18、decr(self, name, amount=1)</em></span>
自减
<span style="color:#008000"><em># 19、append(key, value)</em></span>
在指定key对应值的后面添加指定字符
conn<span style="color:#393a34">.</span>append<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'NB'</span><span style="color:#393a34">)</span>
</code></span></span>
2、2.List类型
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">from</span> redis <span style="color:#0000ff">import</span> Redis
conn <span style="color:#393a34">=</span> Redis<span style="color:#393a34">(</span>host<span style="color:#393a34">=</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span> port<span style="color:#393a34">=</span><span style="color:#36acaa">6379</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 1、lpush(name, values)</em></span>
<span style="color:#008000"><em># 键为key,值为list(参数位置在前生成数据的索引位置就在后)</em></span>
conn<span style="color:#393a34">.</span>lpush<span style="color:#393a34">(</span><span style="color:#a31515">'gender'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'male'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'female'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 2、rpush(name, values)</em></span>
<span style="color:#008000"><em># 键为key,值为list(参数位置在前生成数据的索引位置就在前)</em></span>
conn<span style="color:#393a34">.</span>rpush<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'read'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'run'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 3、lpushx(name, value)</em></span>
<span style="color:#008000"><em># 向列表插入一个值(有对应的key就插入到列表的头部,没有就不操作)</em></span>
conn<span style="color:#393a34">.</span>lpushx<span style="color:#393a34">(</span><span style="color:#a31515">'gender'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'female'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 4、rpushx(name, value)</em></span>
<span style="color:#008000"><em># 向列表插入一个值(有对应的key就插入到列表的尾部部,没有就不操作)</em></span>
conn<span style="color:#393a34">.</span>rpushx<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'ball'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 5、llen(name)</em></span>
<span style="color:#008000"><em># 判断列表内有多少个数据</em></span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>llen<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 6、linsert(name, where, refvalue, value))</em></span>
<span style="color:#008000"><em># name:插入到哪个列表</em></span>
<span style="color:#008000"><em># where:插入到指定值的前或后before:前/after:后</em></span>
<span style="color:#008000"><em># refavalue:插入到哪个值</em></span>
<span style="color:#008000"><em># value:插入的值</em></span>
<span style="color:#008000"><em># 向列表内插入数据</em></span>
conn<span style="color:#393a34">.</span>linsert<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'after'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'run'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'go'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 7、lset</em></span>
<span style="color:#008000"><em># 向指定的索引位置插入一个值</em></span>
conn<span style="color:#393a34">.</span>lset<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">1</span><span style="color:#393a34">,</span> <span style="color:#a31515">'sleep'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 8、lrem</em></span>
<span style="color:#008000"><em># 删除指定的值,可以指定删除的数量,填 0 全部删除,负数从后面删除</em></span>
conn<span style="color:#393a34">.</span>lrem<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">0</span><span style="color:#393a34">,</span> <span style="color:#a31515">'aaaa'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 9、lpop(name)</em></span>
<span style="color:#008000"><em># 从头部弹出一个值,可以接收</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>lpop<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 10、rpop(name)</em></span>
<span style="color:#008000"><em># 从尾部弹出一个值,可以接收</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>rpop<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 11、lindex(name, index)</em></span>
<span style="color:#008000"><em># 按照索引位置取值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>lindex<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">1</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 12、lrange(name, start, end)</em></span>
<span style="color:#008000"><em># 按照索引位置,范围取值,第二个参数填负数取全部</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>lrange<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">0</span><span style="color:#393a34">,</span> <span style="color:#393a34">-</span><span style="color:#36acaa">1</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 13、ltrim(name, start, end)</em></span>
<span style="color:#008000"><em># 修剪,留下索引位置内的数据</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>ltrim<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">1</span><span style="color:#393a34">,</span> <span style="color:#36acaa">2</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 14、rpoplpush(src, dst)</em></span>
<span style="color:#008000"><em># 提供两个列表,将第一个列表的值被弹出,加入到第二个列表内</em></span>
conn<span style="color:#393a34">.</span>lpush<span style="color:#393a34">(</span><span style="color:#a31515">'gender'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'male'</span><span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>rpoplpush<span style="color:#393a34">(</span><span style="color:#a31515">'gender'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 15、blpop(keys, timeout)</em></span>
<span style="color:#008000"><em># 可做消息队列,弹出数据(从左向右)</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>blpop<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 16、brpop(keys, timeout)</em></span>
<span style="color:#008000"><em># 可做消息队列,弹出数据(从右向左)</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>brpop<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 17、brpoplpush(src, dst, timeout=0)</em></span>
<span style="color:#008000"><em># 将弹出的数据加入到两一个列表中</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>brpoplpush<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span><span style="color:#a31515">'gender'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>close<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
2、3.Hash类型
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">import</span> redis
conn <span style="color:#393a34">=</span> redis<span style="color:#393a34">.</span>Redis<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 1、hset(name, key, value)</em></span>
<span style="color:#008000"><em># 设置一个key,value值为字典</em></span>
conn<span style="color:#393a34">.</span>hset<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'lqz'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 这种方法可以一次写入多个键值对</em></span>
conn<span style="color:#393a34">.</span>hset<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span>mapping<span style="color:#393a34">=</span><span style="color:#393a34">{</span><span style="color:#a31515">'age'</span><span style="color:#393a34">:</span><span style="color:#36acaa">19</span><span style="color:#393a34">,</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">:</span><span style="color:#a31515">'篮球'</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 2、hmset(name, mapping)</em></span>
<span style="color:#008000"><em># 批量设置,被弃用了,以后都使用hset</em></span>
conn<span style="color:#393a34">.</span>hmset<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo2'</span><span style="color:#393a34">,</span><span style="color:#393a34">{</span><span style="color:#a31515">'age'</span><span style="color:#393a34">:</span><span style="color:#36acaa">19</span><span style="color:#393a34">,</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">:</span><span style="color:#a31515">'篮球'</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 3、hget(name,key)</em></span>
<span style="color:#008000"><em># 取值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hget<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'name'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 4、hmget(name, keys, *args)</em></span>
<span style="color:#008000"><em># 批量取值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hmget<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 第二种写法</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hmget<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 5、hgetall(name)</em></span>
<span style="color:#008000"><em># 一次性取出所有的值(慎用,数据量庞大的话会引起司机、宕机)</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>hgetall<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 6、hlen(name)</em></span>
<span style="color:#008000"><em># 获取键值对的数量</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>hlen<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 7、hkeys(name)</em></span>
<span style="color:#008000"><em># 一次性获取所有key值</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>hkeys<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 8、hvals(name)</em></span>
<span style="color:#008000"><em># 一次性获取所有value值</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>hvals<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 9、hexists(name, key)</em></span>
<span style="color:#008000"><em># 判断value值是否存在,返回1/0</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hexists<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'name'</span><span style="color:#393a34">)</span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hexists<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'name1'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 10、hdel(name,*keys)</em></span>
<span style="color:#008000"><em># 删除对应的value值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hdel<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 11、hincrby(name, key, amount=1)</em></span>
<span style="color:#008000"><em># 指定字段自增,默认自增1</em></span>
conn<span style="color:#393a34">.</span>hincrby<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">2</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 12、hincrbyfloat(name, key, amount=1.0)</em></span>
<span style="color:#008000"><em># 自增,支持小数</em></span>
<span style="color:#008000"><em># 13、hgetall(name)</em></span>
<span style="color:#008000"><em># 一次性取出所有value的键值对</em></span>
<span style="color:#008000"><em># 插入一批数据</em></span>
<span style="color:#0000ff">for</span> i <span style="color:#0000ff">in</span> range<span style="color:#393a34">(</span><span style="color:#36acaa">1000</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
conn<span style="color:#393a34">.</span>hset<span style="color:#393a34">(</span><span style="color:#a31515">'hash_test'</span><span style="color:#393a34">,</span><span style="color:#a31515">'id_%s'</span><span style="color:#393a34">%</span>i<span style="color:#393a34">,</span><span style="color:#a31515">'鸡蛋_%s号'</span><span style="color:#393a34">%</span>i<span style="color:#393a34">)</span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>hgetall<span style="color:#393a34">(</span><span style="color:#a31515">'hash_test'</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 可以,但是不好,一次性拿出,可能占很大内存</em></span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 14、hscan(name, cursor=0, match=None, count=None) </em></span>
<span style="color:#008000"><em># 按照游标位置取值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hscan<span style="color:#393a34">(</span><span style="color:#a31515">'hash_test'</span><span style="color:#393a34">,</span> cursor<span style="color:#393a34">=</span><span style="color:#36acaa">0</span><span style="color:#393a34">,</span> count<span style="color:#393a34">=</span><span style="color:#36acaa">5</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>len<span style="color:#393a34">(</span>res<span style="color:#393a34">[</span><span style="color:#36acaa">1</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span> <span style="color:#008000"><em>#(数字,拿出来的10条数据) 数字是下一个游标位置</em></span>
<span style="color:#008000"><em># 15、hscan_iter(name, match=None, count=None)</em></span>
<span style="color:#008000"><em># 类似生成器,一次只拿出指定数量的值</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>hscan_iter<span style="color:#393a34">(</span><span style="color:#a31515">'hash_test'</span><span style="color:#393a34">,</span> count<span style="color:#393a34">=</span><span style="color:#36acaa">10</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span> <span style="color:#008000"><em># generator 只要函数中有yield关键字,这个函数执行的结果就是生成器 ,生成器就是迭代器,可以被for循环</em></span>
<span style="color:#0000ff">for</span> i <span style="color:#0000ff">in</span> res<span style="color:#393a34">:</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>i<span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>close<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
4、通用操作
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">import</span> redis
conn <span style="color:#393a34">=</span> redis<span style="color:#393a34">.</span>Redis<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 1、delete(*names)</em></span>
<span style="color:#008000"><em># 删除指定的key,一次删除多个</em></span>
conn<span style="color:#393a34">.</span>delete<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'userinfo2'</span><span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>delete<span style="color:#393a34">(</span><span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'userinfo2'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 不能用它</em></span>
conn<span style="color:#393a34">.</span>delete<span style="color:#393a34">(</span><span style="color:#393a34">*</span><span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'userinfo2'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 可以用它,打散</em></span>
<span style="color:#008000"><em># 2、exists(name)</em></span>
<span style="color:#008000"><em># 判断redis中是否存在指定的key(返回 0/1)</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>exists<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 3、keys(pattern='*')</em></span>
<span style="color:#008000"><em># 查找value的key值中是否存在指定的key,返回对应的value(支持简单的正则)</em></span>
res<span style="color:#393a34">=</span>conn<span style="color:#393a34">.</span>keys<span style="color:#393a34">(</span><span style="color:#a31515">'w?e'</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># ?表示一个字符, * 表示多个字符</em></span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 4、expire(name ,time)</em></span>
<span style="color:#008000"><em># 设置过期时间</em></span>
conn<span style="color:#393a34">.</span>expire<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">,</span><span style="color:#36acaa">3</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 5、rename(src, dst)</em></span>
<span style="color:#008000"><em># 重命名</em></span>
conn<span style="color:#393a34">.</span>rename<span style="color:#393a34">(</span><span style="color:#a31515">'hobby'</span><span style="color:#393a34">,</span><span style="color:#a31515">'hobby111'</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 6、move(name, db)</em></span>
<span style="color:#008000"><em># 更改存储数据的库</em></span>
conn<span style="color:#393a34">.</span>move<span style="color:#393a34">(</span><span style="color:#a31515">'hobby111'</span><span style="color:#393a34">,</span><span style="color:#36acaa">8</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 7、randomkey()</em></span>
<span style="color:#008000"><em># 随机获得一个key</em></span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>randomkey<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>res<span style="color:#393a34">)</span>
<span style="color:#008000"><em># 8、type(name)</em></span>
<span style="color:#008000"><em># 判断数据的类型</em></span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>type<span style="color:#393a34">(</span><span style="color:#a31515">'userinfo'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>type<span style="color:#393a34">(</span><span style="color:#a31515">'age'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>close<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
3、Redis管道
什么是管道:
管道是指,将多条redis操作放在管道内同时执行,管道也是一种事务的操作,可以作用于敏感,重要的数据
ridis支持事务吗:
redis的事务基于管道,只有单实例才支持事务
ridis管道的使用:
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 引入redis</em></span>
<span style="color:#0000ff">import</span> redis
<span style="color:#008000"><em># 实例化redis对象</em></span>
conn <span style="color:#393a34">=</span> redis<span style="color:#393a34">.</span>Redis<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 实例化管道</em></span>
p <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>pipeline<span style="color:#393a34">(</span>transaction<span style="color:#393a34">=</span><span style="color:#36acaa">True</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 开启管道(类似于开启事务)</em></span>
p<span style="color:#393a34">.</span>multi<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 模拟张三给李四100元</em></span>
p<span style="color:#393a34">.</span>decr<span style="color:#393a34">(</span><span style="color:#a31515">'zhangsan'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">100</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 模拟李四账户增加100元</em></span>
p<span style="color:#393a34">.</span>incr<span style="color:#393a34">(</span><span style="color:#a31515">'lisi'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">100</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 启动管道(类似于提交事务)</em></span>
p<span style="color:#393a34">.</span>execute<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#008000"><em># 关闭</em></span>
p<span style="color:#393a34">.</span>close<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>
三、Django操作Redis
1、自定义包方案
这种方案所有的框架都可以使用
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#393a34">-</span> <span style="color:#36acaa">1</span>、第一步:创建一个pool<span style="color:#393a34">.</span>py文件(导入rides,并实例化对象,生成连接池)
<span style="color:#0000ff">import</span> Redis
<span style="color:#008000"><em># 实例化对象,设置连接池最大数量为100</em></span>
POOL <span style="color:#393a34">=</span> Redis<span style="color:#393a34">.</span>CoonectionPool<span style="color:#393a34">(</span>max_connections<span style="color:#393a34">=</span><span style="color:#36acaa">100</span><span style="color:#393a34">)</span>
<span style="color:#393a34">-</span> <span style="color:#36acaa">2</span>、第二步:在以后需要使用的地方直接导入使用即可
<span style="color:#0000ff">from</span> pool <span style="color:#0000ff">import</span> POOL
<span style="color:#0000ff">from</span> redis <span style="color:#0000ff">import</span> Redis
conn <span style="color:#393a34">=</span> Redis<span style="color:#393a34">(</span>connections<span style="color:#393a34">=</span>POOL<span style="color:#393a34">)</span>
conn<span style="color:#393a34">.</span>set<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">,</span><span style="color:#a31515">'kangkang'</span><span style="color:#393a34">)</span>
res <span style="color:#393a34">=</span> conn<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span>
</code></span></span>
2、将Redis设置为Django缓存[推荐使用]
需要安装模块,配置settings文件,以后直接作为Django内置缓存使用
<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#393a34">-</span> 安装模块:
pip install django<span style="color:#393a34">-</span>redis
<span style="color:#393a34">-</span> settings<span style="color:#393a34">.</span>py 中配置:
CACHES <span style="color:#393a34">=</span> <span style="color:#393a34">{</span>
<span style="color:#a31515">"default"</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span>
<span style="color:#a31515">"BACKEND"</span><span style="color:#393a34">:</span> <span style="color:#a31515">"django_redis.cache.RedisCache"</span><span style="color:#393a34">,</span>
<span style="color:#a31515">"LOCATION"</span><span style="color:#393a34">:</span> <span style="color:#a31515">"redis://127.0.0.1:6379"</span><span style="color:#393a34">,</span> <span style="color:#008000"><em># 地址和端口</em></span>
<span style="color:#a31515">"OPTIONS"</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span>
<span style="color:#a31515">"CLIENT_CLASS"</span><span style="color:#393a34">:</span> <span style="color:#a31515">"django_redis.client.DefaultClient"</span><span style="color:#393a34">,</span>
<span style="color:#a31515">"CONNECTION_POOL_KWARGS"</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">"max_connections"</span><span style="color:#393a34">:</span> <span style="color:#36acaa">100</span><span style="color:#393a34">}</span> <span style="color:#008000"><em># 连接池</em></span>
<span style="color:#008000"><em># "PASSWORD": "123", # 密码,没有的话不用设置</em></span>
<span style="color:#393a34">}</span>
<span style="color:#393a34">}</span>
<span style="color:#393a34">}</span>
<span style="color:#393a34">-</span> 在需要使用的地方直接使用即可(但是只能支持内存的操作)
<span style="color:#0000ff">from</span> django<span style="color:#393a34">.</span>core<span style="color:#393a34">.</span>cache <span style="color:#0000ff">import</span> cache
在使用redis的地方:cache<span style="color:#393a34">.</span>set<span style="color:#393a34">(</span><span style="color:#a31515">'count'</span><span style="color:#393a34">,</span> res<span style="color:#393a34">+</span><span style="color:#36acaa">1</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 可以存入任何类型的数据</em></span>
<span style="color:#008000"><em># 底层做了pickle序列化 </em></span>
</code></span></span>
3、使用第三方模块
直接使用django-redis模块
<span style="color:#393a34"><span style="background-color:white"><code class="language-python">
<span style="color:#0000ff">from</span> django_redis <span style="color:#0000ff">import</span> get_redis_connection
<span style="color:#0000ff">def</span> <span style="color:#393a34">test_redis</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
conn<span style="color:#393a34">=</span>get_redis_connection<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">print</span><span style="color:#393a34">(</span>conn<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'count'</span><span style="color:#393a34">)</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">return</span> JsonResponse<span style="color:#393a34">(</span><span style="color:#393a34">)</span></code></span></span>