redis命令备忘单部分2-哈希和列表
#database #redis #cheatsheet #rediscommand

Redis Icon


以下是一些常用的redis命令和数据结构,将此列表用作快速备忘单或准备好的参考,以根据需要进行日常使用restis使用ð

这是本系列的第2部分,包含与哈希有关的命令列表 redis中的命令。


哈希

# create a hash
> HSET key field value [field value ...]
> HSET player:1000 name Artexius race Elf level 5
(integer) 3

# returns the number of fields saved in the hash, so 3 in the above case

# if field does not exist, HSET will create the field and set it's value as specified

# to set a field value only if it does not yet exist
> HSETNX key field value

# get all fields and values of the hash
> HGETALL key
> HGETALL player:1000

# get the value of a specific field in the hash
> HGET key field
> HGET player:1000 level

# get the value of a multiple specific fields in the hash
> HMGET key field [field ...]
> HMGET player:1000 level name race

# get all field names for a key
> HKEYS key

# get all field values for a key
> HVALS key

# update a field in the hash
# HSET overwrites the values of specified fields that exist in the hash. If key doesn't exist, a new key holding a hash is created
> HSET field
> HSET player:1000 level 15

# set a field in the hash only if field does not yet exist
> HSETNX key field value

# delete a field(s) in the hash
> HDEL key field [field ...] 
> HDEL player:1000 level

# increment the value of a hash field
> HINCRBY key field increment
> HINCRBYFLOAT key field increment

# determine if a hash field exists
> HEXISTS key field

# returns 1 if exists, 0 if does not

# iterate over fields that match a pattern
> HSCAN key cursor [MATCH pattern] [COUNT count]

# the first HSCAN called with cursor 0
# this returns a new cursor that can be used in subsequent iterations
# HSCAN Is more efficient than HGETALL

列表

# push elements (to right side) of list
> RPUSH key element [element ...]

# push elements (to left side) of list
> LPUSH key element [element ...]

# pop elements (from left side) of list
# default, the command pops a single element from the beginning of the list
# but when provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length
> LPOP key [count]

# pop elements (from right side) of list
> RPOP key [count]

# get a range of elements from the list 
> LRANGE key start stop

# if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements, that is, the rightmost item is included
# -1 is rightmost value

# check length of the list
> LLEN key

# if key does not exist, it is interpreted as an empty list and 0 is returned
# error is returned when the value stored at key is not a list

# return element at a specified index
> LINDEX key index

# add an element before/after an element
> LINSERT key BEFORE|AFTER pivot-element new-element

# set value at a specified index
> LSET key index value

# remove the first 'count' occurrences of elements equal to 'element' from the list stored at key
# the 'count' argument influences the operation in the following ways:
#   - 'count' > 0: Remove elements equal to 'element' moving from head to tail
#   - 'count' < 0: Remove elements equal to 'element' moving from tail to head
#   - 'count' = 0: Remove all elements equal to 'element'
> LREM key count element

# trim list to a specified range
> LTRIM key start stop

# example: 
> LTRIM foobar 0 2 # only the first three elements of the list will remain in the list

希望您发现这有用!保存帖子供参考,这是该系列的Part 1。第3部分将很快出门!

确实关注espelar.dev,我们正在研究一些非常酷的东西,并为您提供令人兴奋的内容。 欢呼!