python与redis交互(4)
python可以使用redis模块来跟redis交互
redis模块的使用
安装模块: pip3 install redis
导入模块:import redis
连接方式
- 严格连接模式:r=redis.StrictRedis(host="",port=)
- 更Python化的连接模式:r=redis.Redis(host="",port=)
- StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令
- Redis与StrictRedis的区别是:Redis是StrictRedis的子类,用于向前兼容旧版本的redis-py,并且这个连接方式是更加"python化"的
连接池
为了节省资源,减少多次连接损耗,连接池的作用相当于总揽多个客户端与服务端的连接,当新客户端需要连接时,只需要到连接池获取一个连接即可,实际上只是一个连接共享给多个客户端。
复制代码import redis pool= redis.ConnectionPool(host='localhost',port=6379,decode_responses=True) r=redis.Redis(connection_pool=pool)
r2=redis.Redis(connection_pool=pool)
r.set('apple','a')
print(r.get('apple'))
r2.set('banana','b')
print(r.get('banana')) print(r.client_list())
print(r2.client_list())#可以看出两个连接的id是一致的,说明是一个客户端连接
操作
值的设置和获取,可以参考redis的命令,redis模块中的对应功能的函数名基本与redis中的一致
【注意默认情况下,设置的值或取得的值都为bytes类型,如果想改为str类型,需要在连接时添加上decode_responses=True】
设置值
redis中set() ===> r.set()
redis中setnx() ===> r.set()
redis中setex() ===> r.setex()
redis中setbit() ===> r.setbit()
redis中mset() ===> r.mset()
redis中hset() ===> r.hset()
redis中sadd() ===> r.sadd()
其他。。。基本redis的命令名与redis模块中的函数名一致
获取值
redis中get() ===> r.get()
redis中mget() ===> r.mget()
redis中getset() ===> r.getset()
redis中getrange() ===> r.getrange()
其他。。。基本redis的命令名与redis模块中的函数名一致
import redis
r=redis.Redis(host='localhost',port=6379,decode_responses=True)
# r=redis.StrictRedis(host='localhost',port=6379) r.set('key','value')
value=r.get('key')
# print(type(value))
print(value)
r.hset('info','name','lilei')
r.hset('info','age','18')
print(r.hgetall('info'))
r.sadd('course','math','english','chinese')
print(r.smembers('course'))
管道
一般情况下,执行一条命令后必须等待结果才能输入下一次命令,管道用于在一次请求中执行多个命令。
参数介绍:
- transaction:指示是否所有的命令应该以原子方式执行。
import redis,time r=redis.Redis(host="localhost",port=6379,decode_responses=True) pipe=r.pipeline(transaction=True) pipe.set('p1','v2')
pipe.set('p2','v3')
pipe.set('p3','v4')
time.sleep(5)
pipe.execute()
事务
python中可以使用管道来代替事务:
- 补充:监视watch:pipe.watch()
import redis,time
import redis.exceptions
r=redis.Redis(host='localhost',port=6379,decode_responses=True)
pipe=r.pipeline()
print(r.get('a')) try:
# pipe.watch('a')
pipe.multi()
pipe.set('here', 'there')
pipe.set('here1', 'there1')
pipe.set('here2', 'there2')
time.sleep(5)
pipe.execute() except redis.exceptions.WatchError as e:
print("Error")
本文参考:https://www.cnblogs.com/progor/p/8567640.html
python与redis交互(4)的更多相关文章
- python与redis交互及redis基本使用
Redis简介 Redis是一使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日个开源的志型.Key-Value数据库,并提供多种语言的API. 从2010年3月15日起,Redis的开发工 ...
- python与redis交互
爬虫抓来的数据根据实际情况需要存入不同数据库,今天分享一下自己把数据存入redis数据库的经验,有需要的童鞋拿走不谢. 1.环境: Mac osx + python2. 2.需要安装的python包 ...
- 11 python与redis交互
安装:pip install redis 导入模块:from redis import * 创建StrictRedis 通过init创建对象,指定参数host.port与指定的服务器和端口连接. ho ...
- python和redis简单交互
python和redis简单交互 1.安装redis模块 pip3 install redis 2.redis模块简单使用: # /usr/bin/env python3 import redis c ...
- python之redis和memcache操作
Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...
- python——操作Redis
在使用django的websocket的时候,发现web请求和其他当前的django进程的内存是不共享的,猜测django的机制可能是每来一个web请求,就开启一个进程去与web进行交互,一次来达到利 ...
- python之 Redis
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 数据库之redis篇(3)—— Python操作redis
虽然前面两篇已经说了redis的一些配置安装什么的,篇幅有点长,可能看完了也不知道怎么操作,这里再浓缩一下: 什么是redis redis完全开源免费的,遵守BSD协议,是一个高性能的非关系型key- ...
- edis 以及 Python操作Redis
Redis 以及 Python操作Redis Redis Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis有以下特点: -- Redis支持数据的持 ...
随机推荐
- 【Java基础】JAVA中优先队列详解
总体介绍 优先队列的作用是能保证每次取出的元素都是队列中权值最小的(Java的优先队列每次取最小元素,C++的优先队列每次取最大元素).这里牵涉到了大小关系,元素大小的评判可以通过元素本身的自然顺序( ...
- 【Java】【学习】【监听器】Listener的学习的案例(窗体程序)
JavaWeb 监听器listener 学习与简单应用 Java窗体程序使用监听器 效果:点击按钮,控制台出现文字 代码如下 import javax.swing.*; import java.awt ...
- 「Python实用秘技02」给Python函数定“闹钟”
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第2期 ...
- 【WP】【web】中学生CTF | web部分wp
$_GET 源码: <?php show_source(__FILE__); include 'config.php'; if(!isset($_GET['args'])){ die(); } ...
- js Uncaught TypeError: undefined is not a function
如下代码: var columns={}; var column={}: column.name='张三'; columns.push(column); 会出现Uncaught TypeError: ...
- ubuntu 16.04 server安装Bittorrent Transmission
访问web服务 使用http://192.168.1.8:9091 这样的方式管理下载. http://192.168.1.8:9091/transmission/web/ 操作服务 sudo ser ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【LeetCode】1029. Two City Scheduling 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【九度OJ】题目1028:继续畅通工程 解题报告
[九度OJ]题目1028:继续畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1028 题目描述: 省政府" ...