Pick up a Redis Ruby client learning (ii)
For the five data types of Redis: string, hash (Map), list, collection (sets) , and ordered collection (sorted sets), the previous article describes the string.
1, hash (MAP)
- Hset. Sets the value of the specified field in the specified hash set of key. If the hash set specified by key does not exist, a new hash set is created and associated with the key. If the field exists in the hash set, it will be overridden.
" Redis " ='my_hash'field1 ' 001'
- Hget. Returns the value associated with the field in the hash set specified by key
' My_hash ' ' field1 '
- Hdel. Removes the specified domain from the hash set specified by key. Fields that do not exist in the hash set are ignored. If the hash set specified by key does not exist, it will be considered an empty hash set, and the command will return 0.
' My_hash ' ' field1 '
- Hexists. Returns whether the field is a field that exists in the hash set specified by key.
' My_hash ' ' field1 '
- Hgetall. Returns all fields and values in the hash set specified by key.
' My_hash '
- Hincrby. Increments the value of the specified field in the specified hash set of the key.
' My_hash ' ' field1 ', 5
- Hkeys. Returns the name of all fields in the hash set specified by key.
' My_hash '
- Hvals. Returns the value of all fields in the hash set specified by key.
' My_hash '
- Hlen. Returns the number of fields that the hash set specified by key contains.
' My_hash '
- Hmget. Returns a nil value for each field that does not exist in the hash set. Because the nonexistent keys is considered an empty hash set, executing hmget on a nonexistent key will return a list with only nil values.
' My_hash ','field2'field3 '
- Hmset. Sets the value of the specified field in the specified hash set of key. The command overrides all fields that exist in the hash set. If the hash set specified by key does not exist, a new hash set is created and associated with the key.
' My_hash ','field4'004'field5 "'005'
2, listing (list)
- Lpush. Inserts all the specified values into the head ( left ) of the list that is stored in key. If key does not exist, an empty list is created before the push operation. If the value of key is not a list, then an error is returned.
" my_list " ' value1 '
- Lindex The index of the elements returned in the list is
index
stored key
inside. The subscript is indexed starting at 0, so 0
it represents the first element, 1
represents the second element, and so on. A negative index is used to specify the element that starts the index at the end of the list. In this way, it -1
represents the last element, which -2
represents the penultimate element, and pushes it forward.
" my_list ", 1
value
inserts the inserted key
list in front of or behind the base value pivot
.
When key
it does not exist, the list is considered an empty list, and no action is taken.
Error is returned when it is key
present but not a list is saved.
R.linsert'my_list','before','value1','Value3'R.linsert'my_list',' After','value1','Value4'
- Llen. Returns the length of the list stored in
key
. If it key
does not exist, it is considered an empty list, and the return length is 0
. When key
the value stored in is not a list, it returns an error.
" my_list "
- Lpop. Removes and returns the
key
first element of the corresponding list ( left ).
" my_list "
Returns the elements in the key
specified range that are stored in the list. start
and end
offsets are based on the 0 subscript, that is, the first element of the list is subscript 0
(the table header of the list), the second element is subscript 1
, and so on.
The offset can also be a negative number, indicating that the offset is counted from the end of the list. For example, the -1
last element of the list -2
is the penultimate, and so on.
" my_list ", 0,-1
key
removes the element of the previous occurrence from the list in which it was stored count
value
. This count
parameter affects this operation in several ways:
-
count > 0
: Removes value
the element with the value from head to tail.
count < 0
: Removes value
the element with the value from the tail to the head.
count = 0
: Removes all value
elements of the value.
For example, the LREM list -2 "hello"
list
last two occurrences are removed from the list that is stored "hello"
.
Note that if there is no key in the list, it will be treated as an empty list, so key
the command will return when it does not exist 0
.
" my_list " # " my_list" "value1 " " My_list", 0,-1#
- LSet Sets the value of the
index
list element for the position value
. An error is returned when index is out of range.
" my_list " # " my_list" "value5 " " My_list", 0,-1#
- Rpop. Removes and returns the
key
last element of the list that is stored in.
" my_list "
- Rpush. Inserts all specified values at the end of the
key
list that is stored. If it key
does not exist, an empty list is created before the push operation. When key
the save is not a list, an error is returned.
" my_list " ' Value6 '
Summary:
This article mainly introduces the common methods of hashing and listing in the five data types of the Redis Ruby client.
Redis Ruby Client Learning (ii)