值的类型分为五种:
点击中⽂官⽹查看命令⽂档http://redis.cn/commands.html
如果设置的键不存在则为添加,如果设置的键已经存在则修改
set key value
set name itcast
setex key seconds value
setex aa 3 aa
mset key1 value1 key2 value2 ...
mset a1 python a2 java a3 c
append key value
append a1 haha
get key
get name
mget key1 key2 ...
mget a1 a2 a3
keys pattern
keys *
keys a*
exists key1
exists a1
type key
type a1
del key1 key2 ...
del a2 a3
expire key seconds
expire a1 3
ttl key
ttl bb
增加、修改
hset key field value
hset user name itheima
hmset key field1 value1 field2 value2 ...
hmset u2 name itcast age 11
获取
hkeys key
hkeys u2
hget key field
hget u2 name
hmget key field1 field2 ...
hmget u2 name age
hvals key
hvals u2
删除
hdel key field1 field2 ...
hdel u2 age
可能出现的错误
原因:
解决方案:
增加
lpush key value1 value2 ...
lpush a1 a b c
rpush key value1 value2 ...
rpush a1 0 1
linsert key before或after 现有元素 新元素
linsert a1 before b 3
获取
start、stop为元素的下标索引
索引从左侧开始,第⼀个元素为0
索引可以是负数,表示从尾部开始计数,如-1表示最后⼀个元素
lrange key start stop
lrange a1 0 -1
设置指定索引位置的元素值
lset key index value
lset a 1 z
删除
将列表中前count次出现的值为value的元素移除
count > 0: 从头往尾移除
count < 0: 从尾往头移除
count = 0: 移除所有
lrem key count value
lpush a2 a b a b a b
lrem a2 -2 b
lrange a2 0 -1
增加
sadd key member1 member2 ...
sadd a3 zhangsan sili wangwu
获取
smembers key
smembers a3
删除
srem key
srem a3 wangwu
增加
zadd key score1 member1 score2 member2 ...
zadd a4 4 lisi 5 wangwu 6 zhaoliu 3 zhangsan
获取
zrange key start stop
zrange a4 0 -1
zrangebyscore key min max
zrangebyscore a4 5 6
zscore key member
zscore a4 zhangsan
删除
zrem key member1 member2 ...
zrem a4 zhangsan
zremrangebyscore key min max
zremrangebyscore a4 5 6