Redis与python交互(三)
Python与数据库交互
安装redis可以直接使用命令:pip install redis
调用模块redis,这个模块中提供了StrictRedis对象,用于连接redis服务器,并按照不同类型提供了不同的方法进行交互操作。
StrictRedis对象方法:
通过init创建对象,指定参数host、port与指定的服务器和端口号连接,host默认为localhost,port默认为6379,db默认为0
示例:sr = StrictRedis(host=‘localhost’,port=6379,db=0)
简写:sr = StrictRedis()
操作对象示例:
· 列举部分方法
String类型的方法:
set、setex、mset、append、get、mget、key
Keys的方法:
exists、type、delete、expire、getrange、ttl
Hash类型的方法:
hset、hmset、hkeys、hget、hmget、hvals、hdel
List类型的方法:
lpush、rpush、linsert、lrange、lset、lrem
Set类型的方法:
sadd、smembers、srem
Zset类型的方法:
zadd、zrange、zrangebyscore、zscore、zrem、zremrangebyscore