Redis操作命令

mac2022-06-30  74

1.String类型数据操作命令:

exists mykey

append a "hello" #可以不加引号

get a

set b "this is redis"

strlen b 

incr c

incrby c 3

decr c 

decrby c 3

del c

getset d "haha"

setex e 8 "expired after 8 s"

ttl e

setnx e "operation will success if e doesn't exist"

setrange f 20 ddd #替换或补0,以字节为单位,而非字符

getrange f 1 2 

set h "7"

setbit h 6 1  #设置该bit位置的值为1,并返回原来的值

getbit h 6 #返回1

get h #发现h变成了5

mset k "kkk" l "lll"

msetnx k "kkk" l "lll" #只要一个键已存在,则所有键设置失败

 

2.List集合操作命令:

lpush n a b c d

lrange n 0 2

lrange n 0 -1 

lpush n e #n已存在,返回n集合的长度

lpushx o e #o不存在,返回0

lpop n

llen n

lrem p 2 a

lindex p 0

lset p 0 h

lset p 100 h #报错: (error) ERR out of range 

ltrim p 0 2

linsert q before a a1

linsert q after e e1

rpush r a b c d

rpushx r e

rpop r 

rpoplpush r s 

rpoplpush r s 

rpoplpush r r

 

3.Hash操作命令:

hset t key1 "value1"

hsetnx t key1 "value111"

hsetnx t key9 "value9"

hget t key1 

hget t key2

hget t2 key1

hlen t 

hexists t key1

hdel t key1

hincrby t key5 2

hmset u k1 "v1" k2 "v2" 

hmget u k1 k2 k3 

hgetall u

hkeys u 

hvals u

 

4.Set集合操作命令:

sadd v a b c 

sadd v a d c #sadd 2次,为了说明set是自动去重的

sismember v a

sismember v f 

smembers v

scard v

srandmember v 

spop v 

srem v a e f 

smove v w "a" 

sadd w a b c d 

sadd x c 

sadd y a c e 

sdiff w x y 

sdiffstore w x y 

sinter w x y 

sinterstore w x y 

sunion w x y 

sunionstore w x y 

 

转载于:https://www.cnblogs.com/mediocreWorld/p/11397206.html

最新回复(0)