本文可作为redis的操作指南,介绍redis命令对应的python使用姿势
0. 准备篇
首先需要一个redis环境,安装过程,可以参考: redis-环境安装
其次python3 环境安装,可以参考博文:python 运行环境搭建
redis模块安装
1. redis连接
直接连接
| 12
 3
 
 | import redis
 conn = redis.Redis(host='127.0.0.1', port=6379, password='')
 
 | 
主要是三个参数,host + port + password
需要注意一点,当我们需要连接的redis不在本机时,请确保redis的配置是开启了外部访问的,否则会提示连接失败
连接池方式
连接池的方式最大的好处是每次需要获取连接时,从连接池中获得一个可用连接,用完之后不会立马释放,而是扔回到连接池,以实现连接的复用,避免频繁的redis连接的创建和释放
| 12
 3
 4
 
 | import redis
 pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='')
 conn = redis.Redis(connection_pool=pool)
 
 | 
2. String
首先介绍Redis基本数据结构中String的操作姿势
添加
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | 
 
 
 
 
 redis.client.Redis
 
 
 
 
 
 
 redis.client.Redis
 
 
 redis.client.Redis
 
 
 redis.client.Redis
 
 
 redis.client.Redis
 
 | 
获取
注意下面获取的结果类型为byte,使用的时候需要转换成目标对象
| 12
 3
 4
 5
 
 | redis.client.Redis
 
 
 redis.client.Redis
 
 | 
实例
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 
 | import time
 import redis
 
 conn = redis.Redis(host='127.0.0.1', port=6379, password='')
 
 
 def str_demo():
 
 ans = conn.set('test', 1234)
 print(ans)
 
 
 ans = conn.get('test')
 print(ans.decode('utf8'))
 
 
 ans = conn.setnx('test', 4567)
 print(f'{ans}==False')
 
 ans = conn.setex('test', 1, 'abc')
 ret = conn.get('test')
 time.sleep(1)
 ret2 = conn.get('test')
 print(f'{ans}|{ret}==abc|{ret2}==blank')
 
 conn.mset({'t1': 1, 't2': 2, 't3': 3, 't4': 4})
 ans = conn.mget('t1', 't3', 't5', 't4', 't2')
 print(f"mget: {ans}")
 
 
 str_demo()
 
 | 
输出结果
| 12
 3
 4
 5
 
 | True1234
 False==False
 True|b'abc'==abc|None==blank
 mget: [b'1', b'3', None, b'4', b'2']
 
 | 
请注意一下上面的mget返回结果,与传入的keys是通过数组小标进行关联的
3. List
数据结构为列表,支持左添加,右添加
添加
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | lpush(key, v1, v2...)
 
 rpush(key, v1, v2...)
 
 
 lpushex(key, value)
 
 rpushex(key, value)
 
 
 
 lset(key, index, newValue)
 
 
 linsert(key, where, originValue, newValue)
 
 | 
获取
| 12
 3
 4
 5
 
 | lindex(key, index)
 
 
 lrange(key, start, end)
 
 | 
删除
| 12
 3
 4
 5
 6
 
 | lpop(key)
 
 lrem(key, value, num)
 
 ltrim(key, start, end)
 
 | 
实例
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | def list_demo():
 key = "l_test"
 conn.delete(key)
 
 conn.lpush(key, 1, 2, 3)
 ans = conn.lpushx(key, 'head')
 print(ans)
 
 conn.rpush(key, 'a', 'b')
 conn.rpushx(key, 'tail')
 
 ans = conn.lrange(key, 0, -1)
 print(ans)
 
 
 conn.lset(key, 2, 'update')
 conn.linsert(key, 'after', 'update', 'insert')
 ans = conn.lrange(key, 0, -1)
 print(ans)
 
 
 ans = conn.lpop(key)
 print(f"{ans}==head")
 
 
 conn.ltrim(key, 1, 3)
 ans = conn.lrange(key, 0, -1)
 print(ans)
 
 list_demo()
 
 | 
输出结果
| 12
 3
 4
 5
 
 | 4[b'head', b'3', b'2', b'1', b'a', b'b', b'tail']
 [b'head', b'3', b'update', b'insert', b'1', b'a', b'b', b'tail']
 b'head'==head
 [b'update', b'insert', b'1']
 
 | 
II. 其他
一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
2. 声明
尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
3. 扫描关注
一灰灰blog
