redis 五大数据类型使用
字符串str
单个值
127.0.0.1:6379> set name pp # 设置键值[O(1)] OK 127.0.0.1:6379> setex name 300 pp # 设置键值及过期时间 OK 127.0.0.1:6379> get name # 获取单个值 "pp"
多个值
127.0.0.1:6379> mset class1 python class2 java # 设置多个键值[O(1)] OK 127.0.0.1:6379> append class2 python # 追加值 class2:javapython (integer) 10 127.0.0.1:6379> mget class1 class2 # 获取多个值 1) "python" 2) "javapython" 127.0.0.1:6379> del class1 class2 # 删除值 (integer) 2 127.0.0.1:6379> mget class1 class2 1) (nil) 2) (nil)
hash
单个属性
127.0.0.1:6379> hset info name pp (integer) 1 127.0.0.1:6379> hkeys info 1) "name" 127.0.0.1:6379> hget info name "pp" 127.0.0.1:6379> del info (integer) 1
多个属性
127.0.0.1:6379> hmset info name pp age 18 OK 127.0.0.1:6379> hmget info name age 1) "pp" 2) "18" 127.0.0.1:6379> hvals info 1) "pp" 2) "18" 127.0.0.1:6379> del info name age (integer) 1
list 列表
127.0.0.1:6379> lpush name ming uzi mlxg (integer) 3 127.0.0.1:6379> rpush name ming uzi mlxg (integer) 6 127.0.0.1:6379> linsert name before uzi xiaohu (integer) 7 127.0.0.1:6379> lset name 2 kid OK 127.0.0.1:6379> lrange name 0 -1 1) "mlxg" 2) "xiaohu" 3) "kid" 4) "ming" 5) "ming" 6) "uzi" 7) "mlxg" 127.0.0.1:6379> lindex name 1 "xiaohu"
set集合