This article introduces the list data types of the redis Cache Server.
Come on, let’s enjoy FA.
Reference Link
LIST
Redis list is a Link List of string values, which can be used to implement stacks and queues, build queue management for background worker systems, etc.Also, since Redis lists are implemented by linked lists, operations that add new elements to the beginning or end of the list are performed in a fixed amount of time, even if there are millions of elements in the list.
Thereby, the speed of adding a new element to the beginning of a list with 10 elements with the LPUSH command is the same as the speed of adding an element to the beginning of a list with 10 million elements.
Conversely, it is very fast for lists implemented with arrays of access to elements by index, and not so fast for lists implemented with linked lists.
Redis lists are implemented using linked lists because it is extremely important for a database system to be able to add elements to very long lists very quickly.Also, adding elements, for example, is fast.
{‘Beckhoff’,’OMRON’,’Codesys’,’Beckhoff’} |
LPUSH
The LPUSH command will insert all the specified values at the beginning of the list stored in key; if the key does not exist, it will be created as an empty list before the push operation is performed.
In the example below, the LPUSH command adds a list called PLCs and the string “Beckhoff”.Also, Key does not exist, so a new list must be created.
Done!A value of 1 was returned, so a value of 1 was added in the List.
Next we see that a LIST called PLCs has been added in the Browser as well.
Use the LPUSH command again to add the “Codesys” value to the list PLCs.
Done!Since 2 is returned, we see that the list PLCs now has two values.
Checking the PLCs list from the Browser, I see that Index0=Codesys and Index1=Beckhoff.
That is a feature of the LPUSH command, LPUSH adds Value to the left (L) side of the list.
So it will insert the new element at the top of the list.
RPUSH
The RPUSH command will insert all the specified values at the end of the list stored in the key; if the key does not exist, it will be created as an empty list before the push operation is performed.In the example below, the “OMRON” string is added with the RPUSH command.
Checking the PLCs list from the Browser, I see that Index0=Codesys, Index1=Beckhoff, 2=OMRON.
それはRPUSHコマンドの特徴で、RPUSHはリストの左(R)側にValueを追加します。
なので、リストの一番最後に新要素を挿入することになります。
LPOP
The LPOP command would delete the first element of the list stored in key and return it.So first insert “WAGO” and “TURCK” at the beginning of the PLCs key with the LPUSH command.
Currently the PLCs key has six values.
In the example below, the LPOP command will delete the PLCs key to 1.
Done!The PLC key “TURCK” is deleted because “TURCK” is returned.
Now the PLCs key changes Index0 to “WAGO”.
RPOP
The RPOP command can be used to delete and return the last element of the list stored in key.In the example below, the RPOP command removes the last two elements from the PLCs key.
Done!OMRON” is returned twice and the last two “OMRON” elements in the PLCs key are deleted.
LLEN
The LLEN command can be used to return the length of the list stored in key; if key does not exist, 0 is returned.The example below uses the LLEN command to obtain the length of the PLCs key.
Done!3 was returned, indicating that the PLCs key has three elements.
LRANGE
The LRANGE command can be used to return the specified element of the list stored in key.In the example below, the LRANGE command is used to obtain the elements from 0 to 1 of the PLCs key.
Done!Index 0 “WAGO” and Index 1 “Codesys” were returned.
Next, the same LRANGE command is used to get from -2 to -1 in the PLCs key.
Done!Index 1 “Codesys” and Index 2 “Beckhoff” were returned.
LINDEX
The LINDEX command can return the index-th element of the list stored in key.The example below retrieves the second value in the PLCs list.
Done!Server responded with “Beckhoff” (Index=2).
LPOS
The LPOS command returns the index of the matching element in the Redis list. If the element is found, its index (zero-based position in the list) is returned; otherwise, nil is returned if no match is found.
In the example below, we get the Index of the element “Beckhoff” in the PLCs list.
Done!We found that “Beckhoff” has an Index of 2.