前言
------先安装redis库 ------pip install redis
直接连接,用redis库中的Redis或者StrictRedis连接池连接,跟直接连接操作步骤相似,只是要传入pool参数
代码
第一种
from redis
import Redis
'''
一般连接,使用Redis连接
'''
db
= Redis
(host
="localhost", port
=6379, db
=0, decode_responses
=True)
resu
= db
.smembers
('set1')
print(resu
)
from redis
import StrictRedis
'''
一般连接,使用StrictRedis连接
'''
db
= StrictRedis
(host
="localhost", port
=6379, db
=0, decode_responses
=True)
第二种
from redis
import ConnectionPool
from redis
import Redis
'''
使用连接池 连接redis库的 Redis
'''
pool
= ConnectionPool
(host
='localhost', port
=6379, db
=0, decode_responses
=True)
db
= Redis
(connection_pool
=pool
)
resu
= db
.hget
('mark', "name")
resu2
= db
.hmget
('mark', 'name', "age", "h")
resu3
= db
.hgetall
("mark")
print(resu
, type(resu
), sep
=" 类型是: ")
print(resu2
, type(resu2
), sep
=" 类型是: ")
print(resu3
, type(resu3
), sep
=" 类型是: ")
from redis
import ConnectionPool
from redis
import StrictRedis
'''
使用连接池连接 redis库的 StrictRedis
'''
pool
= ConnectionPool
(host
="localhost", port
=6379, db
=0, decode_responses
=True)
db
= StrictRedis
(connection_pool
=pool
)