Redis basic command

  redis

Connect redis

redis-cli -h 192.168.2.215 -p 6379

View key

keys *

Access variable

set num 1
get num

String operation

set str "hello"
append str "world"
strlen str

View type

type num
type amap
type alist

Check to see if key exists

exists num
exists alist
hexists mmap email

Switching databases (Default 0 database)

select 1

Get multiple keys

mget num str
mset num 3 str "world"

Bit operation

set fg "bar"
getbit fg 0
getbit fg 6
setbit fg 0 1
bitcount fg

Atomic operation

incr num
incrby num 3
decr num
decrby num 2
incrbyfloat num 2.1
#没有decrbyfloat
incrbyfloat num -0.1 

List operation

lpush alist a
lpush alist b
lpush alist c
rpush alist d
lpop alist
rpop alist
llen alist
  • Gets a fragment that contains the rightmost element and supports negative index

lrange alist 0 4
  • Get all elements

lrange alist 0 -1
  • Delete the first element with value p from the left

lrem alist 1 p
  • Delete all elements with value p

lrem alist 0 p
  • Delete the first element with value p from the right

lrem alist -1 p
  • Find element with index 0

lindex alist 0
  • Find the first element on the right

lindex alist -1
  • Assignment by Index

lset alist 2 y
  • Deletes elements outside the specified index

ltrim alist 0 3
  • Insert after specifying value w

linsert alist after w k
  • Insert before specifying value w

linsert alist before w x
  • Pop an element from the right to the target list

rpoplpush alist blist

Press other keys to sort

lpush slist 2 1 3
set itemscore:1 50
set itemscore:2 100
set itemscore:3 -10
sort slist by itemscore:* desc

Set operation

sadd myset a
sadd myset b c
  • View all

smembers myset
  • View the number of collection elements

scard myset
  • Delete element

srem myset c
  • Determines whether the element is in the collection

sismember myset c
  • set operation

sadd seta 1 2 3
sadd setb 2 3 4
  • Calculate difference set

sdiff seta setb
  • The difference set is calculated and stored in setj

sdiffstore setj seta setb
sadd setc 2 3
sdiff seta setb setc
  • Calculate intersection

sinter seta setb
  • Calculates the intersection and stores it in setx

sinterstore setx seta setb
  • Compute union

sunion seta setb
  • Compute union and store to setu

sunionstore setu seta setb
  • Random acquisition of elements

srandmember seta 2
  • Pop up an element randomly from the collection

spop seta
  • Collection sorting, by default, converts elements to double-precision floating point, sorting from small to large

sort seta
  • In dictionary order

sort setn alpha

Ordered set

  • List is realized by double linked list. It is fast to obtain elements near both ends, and slow to access intermediate elements when the number of elements increases.

  • It is more suitable for applications that rarely query intermediate elements such as news and logs.

  • Ordered sets are implemented using hash tables/jump tables, and intermediate elements are read quickly.

  • Lists cannot simply adjust the position of an element; ordered sets can

  • Ordered sets calculate scores for each element, consume more memory, and are the most advanced of all sets.

zadd sortset 89 tom 90 jim 100 david
  • Update jim Score

zadd sortset 78 jim
  • View elements from small to large by score

zrange sortset 0 -1
  • View elements from top to bottom by score

zrevrange sortset 0 -1
  • View elements and scores

zrange sortset 0 -1 withscores
  • View score

zscore sortset david
  • According to the score range, including 90

zrangebyscore sortset 90 100
  • Does not include 90

zrangebyscore sortset (90 100
  • Infinite, take 3 people from the second

zrangebyscore sortset (80 +inf limit 1 3
  • Increase the score of an element

zincrby sortset -3 tom
  • Gets the number of elements

zcard sortset
  • Gets the number of elements in the specified range

zcount sortset 90 100
zcount sortset (89 +inf
  • Delete one or more elements

zrem sortset jim
  • Delete by Rank

zremrangebyrank sortset 0 2
  • Delete scores according to score range

zremrangebyscore sortset (60 80
  • Get ranking, from small to large

zrank sortset jim
  • Get ranking, from big to small

zrevrank sortset jim
  • Calculating Intersection of Ordered Sets

zadd t1 1 a 2 b
zadd t2 10 a 20 b
  • 2 is the number of elements

zinterstore rs 2 t1 t2
  • Aggregate is the calculation method for the element scores of the new set.

zinterstore rs1 2 t1 t2 aggregate min
zinterstore rs2 2 t1 t2 aggregate max
zinterstore rs3 2 t1 t2 aggregate sum
  • Increase weight

zinterstore rs4 2 t1 t2 weights 1 0.1
zrange rs2 0 -1 withscores

Map operation

hset amap name "psersia"
//update or insert a key
hset amap email "a@g.com"
hget amap name

hgetall amap

hmset mmap username persia password pw age 3
hmget mmap username age
hgetall mmap
//当字段不存在时赋值,atomic
hsetnx mmap email p@gmail.com
hincrby mmap age 2
//如果不存在,则创建一个key
hincrby mmap score 60
hdel mmap score
hkeys mmap
hvals mmap
hlen mmap

affair

multi
sadd "user:1:following" 2
sadd "user:2:followers" 1
exec
  • The watch variable is modified after watch

#于是事务不执行,count值为2
set count 1
watch count
set count 2
multi
set count 3
exec
get count

Set cache time

set mycache 100
  • Failure after 15 minutes, -1 is permanent survival

expire mycache 900
  • Reset the survival time

expire mycache 90
#以毫秒为单位
pexpire mycache 1000

See the remaining time to live

ttl mycache
  • Cancel the cache and change it to live forever.

persist mycache
  • Set can also clear the time to live.

set mycache hello
ttl mycache

Lpush,rpop implement message queue

lpush q hello world
lpush q test msg
lrange q 0 -1
rpop q
//带10秒的超时时间
brpop q 10
//超时时间设为0,当没有元素时一直阻塞
brpop q 0

Publish subscription mode

publish mychannel hi
subscribe mychannel
//匹配mychannel.1,mychannel.10
psubscribe mychannel.?*
unsbuscribe mychannel