Python 之操作redis
一、示例代码
import redis pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password="", max_connections=10) redis_obj = redis.Redis(connection_pool=pool, decode_responses=True) # 操作字符串 redis_obj.set(name="password", value="123456", ex=6) print(redis_obj.get("password")) redis_obj.setex("username", 5, "Yang") print(redis_obj.get("username")) # 操作列表 redis_obj.delete("database") redis_obj.lpush("database", "sql", "mysql", "mygodb") redis_obj.linsert("database", "before", "mysql", "redis") print(redis_obj.lrange("database", 0, -1)) # 操作hash散列值 redis_obj.hset("user", "username", "Yang") print(redis_obj.hget("user", "username")) # 一次设置多个 user = { "username": "Yang", "password": "123456", } redis_obj.hmset("user", user) # 获取所有,返回字典 print(redis_obj.hgetall("user")) #操作集合 redis_obj.sadd("color", "red", "blue", "yellow") print(redis_obj.smembers("color")) # 删除red redis_obj.srem("color", "red") print(redis_obj.smembers("color"))