StrictRedis对象⽅法
- 通过init创建对象,指定参数host、port与指定的服务器和端⼝连接,host默认为localhost,port默认为6379,db默认为0
sr = StrictRedis(host='localhost', port=6379, db=0)
简写
sr=StrictRedis()
- 根据不同的类型,拥有不同的实例⽅法可以调⽤,与前⾯学的redis命令对应,⽅法需要的参数与命令的参数⼀致
准备
- 在桌面上创建redis目录
- 使用pycharm打开 redis目录
- 创建redis_string.py文件
from redis import StrictRedis
if __name__ == '__main__':
try:
# 创建一个StritRidis对象,建立与redis的连接
sr = StrictRedis()
# 创建name属性值为'itheima'
#res = sr.set('name','itheima')
# 获取name属性值
#res = sr.get('name')
#print(res
# 修改name属性值
# res = sr.set('name','haha')
# print(sr.get('name'))
# 删除name值
res = sr.delete('name')
print(sr.get('name'))
except Exception as e:
print(e)
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
zset
- zadd
- zrange
- zrangebyscore
- zscore
- zrem
- zremrangebyscore