from connection_pool import POOL
import time
import redis
conn = redis.Redis(connection_pool=POOL)# 设置3秒过去
conn.set('k1','v1',3)
res = conn.get('k1')print(res)# b'v1'# 延时4秒
time.sleep(4)
res = conn.get('k1')print(res)# None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
6.2 写入限制
set('key','value',nx)==>setnx('key','value')
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# set没有写入
conn.set('k1','v1')
res = conn.get('k1')print(res)# b'v1'# 写入限制, 键已经存在, set操作失效
conn.set('k1','v2', nx=True)
res = conn.get('k1')print(res)# b'v1'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
6.3 修改限制
set('key','value',xx)==>setxx('key','value')
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)
res = conn.get('k4')# Noneprint(res)# None# 限制修改, 当key存在才能修改
conn.set('k4','v4', xx=True)
res = conn.get('k4')print(res)# None
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# o 的二进制为 01101111
conn.mset({'k1':'o','k2':'o'})print(conn.mget('k1','k2'))# [b'o', b'o']
conn.bitop('AND','k3','k1','k2')# AND 一一得一 零零得零print(conn.get('k3'))# b'o'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
strlen(key)返回value的字节格式,一个汉字三个字节.
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# o 的二进制为 01101111
conn.set('k1','你好')print(conn.strlen('k1'))# 6
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hmget('n1','k1','k2'))# [b'v1', b'v2']print(conn.hmget('n1',['k1','k2']))# [b'v1', b'v2']
1
2
3
4
5
6
7
8
9
10
7.2 查询所有
hgetall(name)获取name对应的hash所有键值对.
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hgetall('n1'))# {b'k1': b'v1', b'k2': b'v2'}
1
2
3
4
5
6
7
8
9
10
11
7.3 获取个数
hlen(name)获取name对应hash的所有键值对个数.
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hlen('n1'))# 2
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hkeys('n1'))# [b'k1', b'k2']
1
2
3
4
5
6
7
8
9
10
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hvals('n1'))# [b'v1', b'v2']
1
2
3
4
5
6
7
8
9
10
7.5 判断键是否存在
hexists(name,key)判断hash中是否存在某个键
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hexists('n1','k1'))# True
1
2
3
4
5
6
7
8
9
10
7.6 删除键
hdel(name,*keys)将指定的key从hash中删除.
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hmset('n1',{'k1':'v1','k2':'v2'})print(conn.hdel('n1','k1'))# 1print(conn.hgetall('n1'))# {b'k2': b'v2'}
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)# DeprecationWarning: Redis.hmset() is deprecated. Use Redis.hset() instead. 弃用警告
conn.hset('n1','k1',0)print(conn.hincrby('n1','k1'))# 1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)
res = conn.lpush('list1',1,2,3,4,5)print(res)# 5 插入的数据量print(conn.lrange('list1',0,-1))# [b'5', b'4', b'3', b'2', b'1']
1
2
3
4
5
6
7
8
9
10
11
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)
res = conn.rpush('list2',1,2,3,4,5)print(res)# 5 list2的数据量print(conn.lrange('list2',0,-1))# [b'1', b'2', b'3', b'4', b'5']
1
2
3
4
5
6
7
8
9
10
11
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)
res1 = conn.rpushx('list3',1,)
res2 = conn.lpushx('list4',1,)print(res1)# 0 list3的数据量print(res2)# 0 list3的数据量
1
2
3
4
5
6
7
8
9
10
11
8.2 统计长度
llen(name)统计name对应的列表的长度.
1
from connection_pool import POOL
import redis
conn = redis.Redis(connection_pool=POOL)
conn.lpush('list5',1,2,3)
res = conn.llen('list5')print(res)# 3 list5长度