1. ホーム
  2. Qt

redis 共通のデータ型と操作

2022-02-22 05:07:58
<パス

記事目次


I. 文字列データ型

Stringはredisの最も基本的なタイプで、最大512MBのデータを保存することができます。String型はバイナリセーフなので、数字、画像、シリアライズされたオブジェクトなど、どんなデータでも保存できます。

1. set/get/append/strlen

redis 127.0.0.1:6379> exists mykey
# Determines if the key exists, returns 1 if it does, 0 otherwise.
(integer) 0		
redis 127.0.0.1:6379> append mykey "hello"
# The key does not exist, so the append command returns the length of the current Value.
(integer) 5		
redis 127.0.0.1:6379> append mykey " world"
# The key already exists, so the length of the appended Value is returned.
(integer) 11
redis 127.0.0.1:6379> get mykey
#Get the key with the get command to determine the result of append.
"hello world"
redis 127.0.0.1:6379> set mykey "this is a test"
# Set the new value for the key with the set command and overwrite the old value.
OK
redis 127.0.0.1:6379> get mykey
"this is a test"
redis 127.0.0.1:6379> strlen mykey
# Get the character length of the specified Key.
(integer) 14

redis 127.0.0.1:6379> set mykey 20
# set the value of Key to 20
OK
redis 127.0.0.1:6379> incr mykey
# increment the value of the Key by 1
(integer) 21
redis 127.0.0.1:6379> decr mykey
# The value of the Key is decremented by 1
(integer) 20
redis 127.0.0.1:6379> del mykey
# Delete an existing key.
(integer) 1
redis 127.0.0.1:6379> decr mykey
# Decrement the null value, which is set to 0 and decremented to -1
(integer) -1
redis 127.0.0.1:6379> del mykey   
(integer) 1
redis 127.0.0.1:6379> incr mykey
# increment the null value, the original value is set to 0 and the incremented value is 1
(integer) 1
redis 127.0.0.1:6379> set mykey hello
# Set the Value of the key to a normal string that cannot be converted to an integer.
OK
redis 127.0.0.1:6379> incr mykey
(error) ERR value is not an integer or out of range
redis 127.0.0.1:6379> set mykey 10
set mykey 10
redis 127.0.0.1:6379> decrby mykey 5
#Decrease the specified integer
(integer) 5
redis 127.0.0.1:6379> incrby mykey 10
# Increment the specified integer
(integer) 15

redis 127.0.0.1:6379> incr mycounter
# increment the counter value atomically by 1
(integer) 1
redis 127.0.0.1:6379> getset mycounter 0
# Get the original value of the counter and set it to the new value at the same time, these two operations are done atomically at the same time.
"1"
redis 127.0.0.1:6379> get mycounter
# View the result after setting.
"0"

redis 127.0.0.1:6379> setex mykey 15 "hello"
# Set the expiration time of the specified Key to 10 seconds.
OK    
redis 127.0.0.1:6379> ttl mykey
# Check the remaining time (in seconds) of the specified Key with the ttl command, -2 means it has expired, -1 means it will never expire.
(integer) 4
redis 127.0.0.1:6379> get mykey
# We can still get the Value of the key during its lifetime.
"hello"
redis 127.0.0.1:6379> ttl mykey
# The return value of this ttl command shows that the Key has expired.
(integer) -2
redis 127.0.0.1:6379> get mykey
#Get expired Key will return nil.
(nil)

redis 127.0.0.1:6379> del mykey
# delete the key for the following test to verify.
(integer) 1
redis 127.0.0.1:6379> setnx mykey "hello"
# The key does not exist, so the setnx command executed successfully.
(integer) 1
redis 127.0.0.1:6379> setnx mykey "world"
# The key already exists, so this setup has no effect.
(integer) 0
redis 127.0.0.1:6379> get mykey
# From the result, you can see that the value returned is still the value set the first time.
"hello"

redis 127.0.0.1:6379> mset key1 "hello" key2 "world"
# batch set key1 and key2.
OK
redis 127.0.0.1:6379> mget key1 key2
#Batch get the values of key1 and key2.
1) "hello"
2) "world"
redis 127.0.0.1:6379> msetnx key3 "zhang" key4 "san"
# Batch set the keys key3 and key4, because they did not exist before, so the msetnx command executes successfully and returns 1.
(integer) 1
redis 127.0.0.1:6379> mget key3 key4
1) "zhang"
2) "san"
redis 127.0.0.1:6379> msetnx key3 "hello" key5 "world"
# Batch set both key3 and key5, but key3 already exists, so msetnx command execution fails and returns 0.
(integer) 0
redis 127.0.0.1:6379> mget key3 key5
# Bulk get key3 and key5, return nil because key5 is not set successfully.
1) "zhang"
2) (nil)



2. INCR/DECR/INCRBY/DECRBY

redis 127.0.0.1:6379> del mykey
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d
#mykey key does not exist, this command will create the key and its associated list, and then insert the values in the parameters from left to right.
(integer) 4
redis 127.0.0.1:6379> lrange mykey 0 2
# Take the 3 elements starting at position 0 and ending at position 2.
1) "d"
2) "c"
3) "b"
redis 127.0.0.1:6379> lrange mykey 0 -1
#Fetch all elements of the chain table, where 0 means the first element and -1 means the last element.
1) "d"
2) "c"
3) "b"
4) "a"
redis 127.0.0.1:6379> lpushx mykey2 e
The #mykey2 key does not exist at this time, so the lpushx command will not perform any operation and will return 0.
(integer) 0
redis 127.0.0.1:6379> lrange mykey2 0 -1
# You can see that mykey2 does not have any List Value associated with it.
(empty list or set)
redis 127.0.0.1:6379> lpushx mykey e
#mykey key already exists at this point, so the lpushx command inserts successfully and returns the current number of elements in the chain.
(integer) 5
redis 127.0.0.1:6379> lrange mykey 0 0
# Get the head element of the List Value for this key.
1) "e"

redis 127.0.0.1:6379> del mykey
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d
(integer) 4
redis 127.0.0.1:6379> lpop mykey
#Remove and return the first element of the mykey key, taken from the left
"d"
redis 127.0.0.1:6379> lpop mykey
"c"
redis 127.0.0.1:6379> llen mykey
# After executing the lpop command twice, the two elements at the head of the chain have been popped, and the number of elements in the chain at this point is 2
(integer) 2

redis 127.0.0.1:6379> del mykey
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d a c
#Prepare the test data for the examples that follow.
(integer) 6
redis 127.0.0.1:6379> lrem mykey 2 a
# chain of variables from head (left) to tail (right), delete 2 elements with value equal to a, return value is the actual number deleted.
(integer) 2
redis 127.0.0.1:6379> lrange mykey 0 -1
# See all the elements in the chain table after deletion.
1) "c"
2) "d"
3) "c"
4) "b"
redis 127.0.0.1:6379> lindex mykey 1
# Get the value of the element with index value 1 (second element of the header).
"d"
redis 127.0.0.1:6379> lset mykey 1 e
# Set the value of the element with index 1 (the second element in the header) to the new value e.
OK
redis 127.0.0.1:6379> lindex mykey 1
# Check if the setting was successful.
"e"
redis 127.0.0.1:6379> lindex mykey 6
#The index value 6 exceeds the number of elements in the chain table, the command returns nil.
(nil)
redis 127.0.0.1:6379> lset mykey 6 hh
# Set index 6 exceeds the number of elements in the chain table, the setting fails and the command returns an error message.
(error) ERR index out of range
redis 127.0.0.1:6379> ltrim mykey 0 2
# Only the 3 elements between index 0 and 2 are reserved, note that both the 0th and 2nd elements are reserved.
OK
redis 127.0.0.1:6379> lrange mykey 0 -1
# Check the result after trim.
1) "c"
2) "e"
3) "c" 

redis 127.0.0.1:6379> del mykey
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d e
# Prepare test data for later examples.
(integer) 5
redis 127.0.0.1:6379> linsert mykey before a a1
# Insert new element a1 before a.
(integer) 6.
redis 127.0.0.1:6379> lrange mykey 0 -1
# check if the insertion is successful, from the result it looks like it has been inserted
1) "e"
2) "d"
3) "c"
4) "b"
5) "a1"
6) "a"
redis 127.0.0.1:6379> linsert mykey after e e2
# insert new element e2 after e. From the returned result, the insertion has been successful.
(integer) 7
redis 127.0.0.1:6379> lindex mykey 1
# Check again to see if the insertion was successful.
"e2"
redis 127.0.0.1:6379> linsert mykey after k a
# Insert a new element before or after a non-existent element. linsert command operation fails and returns -1.
(integer) -1
redis 127.0.0.1:6379> linsert mykey1 after a a2
# Insert a new element for a non-existent Key. linsert command operation fails and returns 0.
(integer) 0

redis 127.0.0.1:6379> del mykey
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> rpush mykey a b c d
# Insert the values given in the parameters from the end of the chain table, in order from right to left.
(integer) 4
redis 127.0.0.1:6379> lrange mykey 0 -1
# The lrange command allows you to learn the insertion order of rpush when inserting multiple values.
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpushx mykey e
# The key already exists and contains 4 elements, the rpushx command will execute successfully and insert the element e to the end of the chain.
(integer) 5
redis 127.0.0.1:6379> lindex mykey 4
# The lindex command shows that the previous rpushx command did execute successfully, because the element with index value 4 is already new.
"e"
redis 127.0.0.1:6379> rpushx mykey2 e
# Since the mykey2 key does not exist, the rpushx command will not insert data and its return value is 0.
(integer) 0
redis 127.0.0.1:6379> lrange mykey 0 -1
# Before executing the rpoplpush command, look at what elements of the chain table are in mykey and note their positional relationship.
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> RPOP mykey
#Remove and return the first element of the mykey key, taken from the right
"e"
127.0.0.1:6379> LRANGE mykey 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey2
#pop the trailing element e of mykey and insert it into the head of mykey2 at the same time (atomically completing these two operations).
"d"
redis 127.0.0.1:6379> lrange mykey 0 -1
# View the result of mykey after popping the trailing element with the lrange command.
1) "a"
2) "b"
3) "c"
redis 127.0.0.1:6379> lrange mykey2 0 -1
# View the result of mykey2 after inserting elements by lrange command.
1) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey
# Set source and destination to the same key, and move the trailing element in mykey to its head.
"c"
redis 127.0.0.1:6379> lrange mykey 0 -1
# View the result of the move.
1) "c"
2) "a"
3) "b"



3. ゲッツセット

redis 127.0.0.1:6379> hset myhash field1 "zhang"
# Set the field to field1 for the key with the key value myhash and the value zhang.
(integer) 1
redis 127.0.0.1:6379> hget myhash field1
# Get the value with key myhash and field field1.
"zhang"
redis 127.0.0.1:6379> hget myhash field2
#myhash key does not have field2 field in it, so it returns nil.
(nil)
redis 127.0.0.1:6379> hset myhash field2 "san"
# Add a new field field2 to myhash with the value san.
(integer) 1
redis 127.0.0.1:6379> hlen myhash
#hlen command to get the number of fields for the myhash key.
(integer) 2
redis 127.0.0.1:6379> hexists myhash field1
# Determine if a field with the name field1 exists in the myhash key, since it does, the return value is 1.
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1
# Delete the field with the field name field1 in the myhash key, returns 1 for successful deletion.
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1
# Delete the field with the field name field1 in the myhash key again, as the previous command already deleted it, as it was not deleted, returns 0.
(integer) 0
redis 127.0.0.1:6379> hexists myhash field1
# Determine if the field1 field exists in the myhash key, since it was deleted in the previous command, as 0 is returned.
(integer) 0
redis 127.0.0.1:6379> hsetnx myhash field1 zhang
# Add a new field field1 to myhash with the value zhang via the hsetnx command, which is successful and returns 1 because the field has been deleted.
(integer) 1
redis 127.0.0.1:6379> hsetnx myhash field1 zhang
# Since the field1 field of myhash has been successfully added by the previous command, this command returns 0 after not doing anything.
(integer) 0

redis 127.0.0.1:6379> del myhash
#delete the key to facilitate testing of the example later.
(integer) 1
redis 127.0.0.1:6379> hset myhash field 5
#Prepare the test data with the field of myhash set to 5.
(integer) 1
redis 127.0.0.1:6379> hincrby myhash field 1
The #hincrby command adds 1 to the value of the field field of myhash and returns the result of the addition.
(integer) 6
redis 127.0.0.1:6379> hincrby myhash field -1
The #hincrby command adds -1 to the field of myhash, and returns the result of the addition.
(integer) 5
redis 127.0.0.1:6379> hincrby myhash field -10
The #hincrby command adds -10 to the field of myhash and returns the result of the addition.
(integer) -5

redis 127.0.0.1:6379> del myhash
# Delete the key for testing later in the example.
(integer) 1
redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
#hmset command for the key myhash, set multiple fields at once, field1="hello", field2="world".
OK
redis 127.0.0.1:6379> hmget myhash field1 field2 field3
The #hmget command gets multiple fields of the myhash key, of which field3 does not exist because the value corresponding to that field is nil in the return result.
1) "hello"
2) "world"
3) (nil)
redis 127.0.0.1:6379> hgetall myhash
The #hgetall command returns all the fields of the myhash key and their values, and as you can see from the results, they are listed pair by pair.
1) "field1"
2) "hello"
3) "field2"
4) "world"
redis 127.0.0.1:6379> hkeys myhash
The #hkeys command only gets the names of all fields in the myhash key.
1) "field1"
2) "field2"
redis 127.0.0.1:6379> hvals myhash
The #hvals command only gets the values of all fields in the myhash key.
1) "hello"
2) "world" 


4. SETEX

Application scope.

5. SETNX

redis 127.0.0.1:6379> sadd myset a b c
#insert test data, and since the key myset did not exist before, all three members of the argument are inserted normally.
(integer) 3
redis 127.0.0.1:6379> sadd myset a d e
# Since the a in the argument already exists in myset, this operation only inserts the two new members d and e.
(integer) 2
redis 127.0.0.1:6379> sismember myset a
# Determine if a already exists, a return value of 1 means it exists.
(integer) 1
redis 127.0.0.1:6379> sismember myset f
# Determine if f already exists, a return value of 0 means it does not exist.
(integer) 0
redis 127.0.0.1:6379> smembers myset
# The results of the insertion are viewed by the smembers command. From the results, we can see that the order of the output has nothing to do with the order of insertion.
1) "c"
2) "d"
3) "a"
4) "b"
5) "e"
redis 127.0.0.1:6379> scard myset
# Get the number of elements in a Set collection.
(integer) 5

redis 127.0.0.1:6379> del myset
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> sadd myset a b c d
#Prepare test data for later examples.
(integer) 4
redis 127.0.0.1:6379> smembers myset
#View the location of the members in Set.
1) "c"
2) "d"
3) "a"
4) "b"
redis 127.0.0.1:6379> srandmember myset
# As you can see from the results, the command does return a random member.
"c"
redis 127.0.0.1:6379> spop myset
# Randomly remove and return a member of Set.
"b"
redis 127.0.0.1:6379> smembers myset
# View information about the members of Set after removal.
1) "c"
2) "d"
3) "a"
redis 127.0.0.1:6379> srem myset a d f
# removes the members a, d and f from Set, where f does not exist, so only the members a and d are removed, returning 2.
(integer) 2
redis 127.0.0.1:6379> smembers myset
# View the output after the move out.
1) "c"
redis 127.0.0.1:6379> sadd myset a b
#Prepare data for the smove command later.
(integer) 2
redis 127.0.0.1:6379> sadd myset2 c d
(integer) 2
redis 127.0.0.1:6379> smove myset myset2 a
# Move a from myset to myset2, as you can see from the result the move was successful.
(integer) 1
redis 127.0.0.1:6379> smove myset myset2 a
# Move a from myset to myset2 again. Since a is no longer a member of myset at this point, the move fails and returns 0.
(integer) 0
redis 127.0.0.1:6379> smembers myset
# Check the members of myset and myset2 separately to confirm if the move actually succeeded.
1) "b"
redis 127.0.0.1:6379> smembers myset2
1) "c"
2) "d"
3) "a"



6. mset/mget/msetnx

Application scope.

II. リストデータ型

リストの要素は文字列型であり、挿入順にソートされ、リストの先頭または末尾に要素を追加する。

1. lpush/lpushx/lrange

redis 127.0.0.1:6379> zadd myzset 1 "one"
# Add a member with a score of 1.
(integer) 1
redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"
#Add two members with scores 2 and 3 respectively.
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
#0 means the first member, -1 means the last member. the WITHSCORES option means that each member and its score are included in the returned result, otherwise only the member is returned.
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis 127.0.0.1:6379> zrank myzset one
# Get the index value of member one's position in Sorted-Set. 0 means the first position.
(integer) 0
redis 127.0.0.1:6379> zrank myzset four
# Member four does not exist, so nil is returned.
(nil)
redis 127.0.0.1:6379> zcard myzset
# Get the number of members in the myzset key.
(integer) 3
redis 127.0.0.1:6379> zcount myzset 1 2
#zcount key min max, the number of members whose scores satisfy the expression 1 <= score <= 2.
(integer) 2
redis 127.0.0.1:6379> zrem myzset one two
# Delete members one and two, returning the number of members actually deleted.
(integer) 2
redis 127.0.0.1:6379> zcard myzset
# Check if the deletion was successful.
(integer) 1
redis 127.0.0.1:6379> zscore myzset three
#Get the score of member three. The return value is in string form.
"3"
redis 127.0.0.1:6379> zscore myzset two
# Since the member two has been deleted, this command returns nil.
(nil)
redis 127.0.0.1:6379> zincrby myzset 2 one
# member one does not exist, the zincrby command will add the member and assume its initial score is 0, increase the score of member one by 2, and return the updated score of the member.
"2"
redis 127.0.0.1:6379> zincrby myzset -1 one
# Increase the score of member one by -1 and return the updated score of that member.
"1"
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
# Check if the score is correct after updating the members.
1) "one"
2) "1"
3) "three"
4) "3"

redis 127.0.0.1:6379> del myzset
(integer) 1
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrangebyscore myzset 1 2
#zrangebyscore key min max, get the members whose scores satisfy the expression 1 <= score <= 2.
1) "one"
2) "two"
redis 127.0.0.1:6379> zrangebyscore myzset (1 2
# Get the members whose scores satisfy the expression 1 < score <= 2.
1) "two"
redis 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3
#-inf means the first member (the one with the lowest location index value, i.e. 0), +inf means the last member (the one with the highest location index value), the parameter after limit is used to limit the value of the returned member, 2 means starting from the member with location index equal to 2, and taking the next 3 members.
1) "three"
2) "four"
redis 127.0.0.1:6379> zrangebyscore myzset 0 4 limit 2 3
redis 127.0.0.1:6379> zremrangebyscore myzset 1 2
# Delete members whose scores satisfy the expression 1 <= score <= 2, and return the actual number deleted.
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1
# See if the above deletion was successful.
1) "three"
2) "four"
redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
# Delete members whose location indexes satisfy the expression 0 <= rank <= 1.
(integer) 2
redis 127.0.0.1:6379> zcard myzset
# Check if the previous command deleted successfully.
(integer) 0

redis 127.0.0.1:6379> del myzset
#Prepare the test data for the examples that follow.
(integer) 0
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrevrange myzset 0 -1 WITHSCORES
# Get and return members in this range in position index from highest to lowest.
1) "FOUR"
2) "4"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"
redis 127.0.0.1:6379> zrevrange myzset 1 3
# Since it is sorted from highest to lowest, position equal to 0 is FOUR, 1 is THREE, and so on.
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrank myzset one
# Since it is sorted from highest to lowest, the position of one is 3.
(integer) 3
redis 127.0.0.1:6379> zrevrank myzset four
# Since it is sorted from highest to lowest, the position of four is 0.
(integer) 0
redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0
#zrevrangebyscore key max min, Get the members whose scores satisfy the expression 3 >= score >= 0, and output them in order from highest to lowest.
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2
The #zrevrangebyscore command supports the limit option, which has the same meaning as this option in zrangebyscore, except that the positions are calculated and fetched in reverse order when calculating them.
1) "three"
2) "two"
redis 127.0.0.1:6379> zrevrangebyscore myzset +inf -inf limit 1 3



2. LPOP/LLEN

redis 127.0.0.1:6379> del mykey
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d
(integer) 4
redis 127.0.0.1:6379> lpop mykey
#Remove and return the first element of the mykey key, taken from the left
"d"
redis 127.0.0.1:6379> lpop mykey
"c"
redis 127.0.0.1:6379> llen mykey
# After executing the lpop command twice, the two elements at the head of the chain have been popped, and the number of elements in the chain at this point is 2
(integer) 2


3. lrem/lset/lindex/ltrim

redis 127.0.0.1:6379> del mykey
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d a c
#Prepare the test data for the examples that follow.
(integer) 6
redis 127.0.0.1:6379> lrem mykey 2 a
# chain of variables from head (left) to tail (right), delete 2 elements with value equal to a, return value is the actual number deleted.
(integer) 2
redis 127.0.0.1:6379> lrange mykey 0 -1
# See all the elements in the chain table after deletion.
1) "c"
2) "d"
3) "c"
4) "b"
redis 127.0.0.1:6379> lindex mykey 1
# Get the value of the element with index value 1 (second element of the header).
"d"
redis 127.0.0.1:6379> lset mykey 1 e
# Set the value of the element with index 1 (the second element in the header) to the new value e.
OK
redis 127.0.0.1:6379> lindex mykey 1
# Check if the setting was successful.
"e"
redis 127.0.0.1:6379> lindex mykey 6
#The index value 6 exceeds the number of elements in the chain table, the command returns nil.
(nil)
redis 127.0.0.1:6379> lset mykey 6 hh
# Set index 6 exceeds the number of elements in the chain table, the setting fails and the command returns an error message.
(error) ERR index out of range
redis 127.0.0.1:6379> ltrim mykey 0 2
# Only the 3 elements between index 0 and 2 are reserved, note that both the 0th and 2nd elements are reserved.
OK
redis 127.0.0.1:6379> lrange mykey 0 -1
# Check the result after trim.
1) "c"
2) "e"
3) "c" 


4. LINSERT

redis 127.0.0.1:6379> del mykey
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> lpush mykey a b c d e
# Prepare test data for later examples.
(integer) 5
redis 127.0.0.1:6379> linsert mykey before a a1
# Insert new element a1 before a.
(integer) 6.
redis 127.0.0.1:6379> lrange mykey 0 -1
# check if the insertion is successful, from the result it looks like it has been inserted
1) "e"
2) "d"
3) "c"
4) "b"
5) "a1"
6) "a"
redis 127.0.0.1:6379> linsert mykey after e e2
# insert new element e2 after e. From the returned result, the insertion has been successful.
(integer) 7
redis 127.0.0.1:6379> lindex mykey 1
# Check again to see if the insertion was successful.
"e2"
redis 127.0.0.1:6379> linsert mykey after k a
# Insert a new element before or after a non-existent element. linsert command operation fails and returns -1.
(integer) -1
redis 127.0.0.1:6379> linsert mykey1 after a a2
# Insert a new element for a non-existent Key. linsert command operation fails and returns 0.
(integer) 0


5. rpush/rpushx/rpop/rpoplpush

redis 127.0.0.1:6379> del mykey
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> rpush mykey a b c d
# Insert the values given in the parameters from the end of the chain table, in order from right to left.
(integer) 4
redis 127.0.0.1:6379> lrange mykey 0 -1
# The lrange command allows you to learn the insertion order of rpush when inserting multiple values.
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpushx mykey e
# The key already exists and contains 4 elements, the rpushx command will execute successfully and insert the element e to the end of the chain.
(integer) 5
redis 127.0.0.1:6379> lindex mykey 4
# The lindex command shows that the previous rpushx command did execute successfully, because the element with index value 4 is already new.
"e"
redis 127.0.0.1:6379> rpushx mykey2 e
# Since the mykey2 key does not exist, the rpushx command will not insert data and its return value is 0.
(integer) 0
redis 127.0.0.1:6379> lrange mykey 0 -1
# Before executing the rpoplpush command, look at what elements of the chain table are in mykey and note their positional relationship.
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> RPOP mykey
#Remove and return the first element of the mykey key, taken from the right
"e"
127.0.0.1:6379> LRANGE mykey 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey2
#pop the trailing element e of mykey and insert it into the head of mykey2 at the same time (atomically completing these two operations).
"d"
redis 127.0.0.1:6379> lrange mykey 0 -1
# View the result of mykey after popping the trailing element with the lrange command.
1) "a"
2) "b"
3) "c"
redis 127.0.0.1:6379> lrange mykey2 0 -1
# View the result of mykey2 after inserting elements by lrange command.
1) "d"
redis 127.0.0.1:6379> rpoplpush mykey mykey
# Set source and destination to the same key, and move the trailing element in mykey to its head.
"c"
redis 127.0.0.1:6379> lrange mykey 0 -1
# View the result of the move.
1) "c"
2) "a"
3) "b"



III. ハッシュデータ型

  • ハッシュはオブジェクトを格納するために使用されます。命名は、オブジェクトのクラスとIDがキー名を形成し、オブジェクトの属性はフィールドを使って表現され、フィールド値には属性値が格納されるようにすることができる。例えば、IDが2の車のオブジェクトを格納する。

  • Hashに含まれるフィールドが少ない場合、そのタイプのデータもわずかなディスクスペースしか占めない。各ハッシュは4294967295のキーと値のペアを格納することができます。

1. hset/hget/hdel/hexists/hlen/hsetnx

redis 127.0.0.1:6379> hset myhash field1 "zhang"
# Set the field to field1 for the key with the key value myhash and the value zhang.
(integer) 1
redis 127.0.0.1:6379> hget myhash field1
# Get the value with key myhash and field field1.
"zhang"
redis 127.0.0.1:6379> hget myhash field2
#myhash key does not have field2 field in it, so it returns nil.
(nil)
redis 127.0.0.1:6379> hset myhash field2 "san"
# Add a new field field2 to myhash with the value san.
(integer) 1
redis 127.0.0.1:6379> hlen myhash
#hlen command to get the number of fields for the myhash key.
(integer) 2
redis 127.0.0.1:6379> hexists myhash field1
# Determine if a field with the name field1 exists in the myhash key, since it does, the return value is 1.
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1
# Delete the field with the field name field1 in the myhash key, returns 1 for successful deletion.
(integer) 1
redis 127.0.0.1:6379> hdel myhash field1
# Delete the field with the field name field1 in the myhash key again, as the previous command already deleted it, as it was not deleted, returns 0.
(integer) 0
redis 127.0.0.1:6379> hexists myhash field1
# Determine if the field1 field exists in the myhash key, since it was deleted in the previous command, as 0 is returned.
(integer) 0
redis 127.0.0.1:6379> hsetnx myhash field1 zhang
# Add a new field field1 to myhash with the value zhang via the hsetnx command, which is successful and returns 1 because the field has been deleted.
(integer) 1
redis 127.0.0.1:6379> hsetnx myhash field1 zhang
# Since the field1 field of myhash has been successfully added by the previous command, this command returns 0 after not doing anything.
(integer) 0


2. HINCRBY

redis 127.0.0.1:6379> del myhash
#delete the key to facilitate testing of the example later.
(integer) 1
redis 127.0.0.1:6379> hset myhash field 5
#Prepare the test data with the field of myhash set to 5.
(integer) 1
redis 127.0.0.1:6379> hincrby myhash field 1
The #hincrby command adds 1 to the value of the field field of myhash and returns the result of the addition.
(integer) 6
redis 127.0.0.1:6379> hincrby myhash field -1
The #hincrby command adds -1 to the field of myhash, and returns the result of the addition.
(integer) 5
redis 127.0.0.1:6379> hincrby myhash field -10
The #hincrby command adds -10 to the field of myhash and returns the result of the addition.
(integer) -5


3. hgetall/hkeys/hvals/hmget/hmset

redis 127.0.0.1:6379> del myhash
# Delete the key for testing later in the example.
(integer) 1
redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
#hmset command for the key myhash, set multiple fields at once, field1="hello", field2="world".
OK
redis 127.0.0.1:6379> hmget myhash field1 field2 field3
The #hmget command gets multiple fields of the myhash key, of which field3 does not exist because the value corresponding to that field is nil in the return result.
1) "hello"
2) "world"
3) (nil)
redis 127.0.0.1:6379> hgetall myhash
The #hgetall command returns all the fields of the myhash key and their values, and as you can see from the results, they are listed pair by pair.
1) "field1"
2) "hello"
3) "field2"
4) "world"
redis 127.0.0.1:6379> hkeys myhash
The #hkeys command only gets the names of all fields in the myhash key.
1) "field1"
2) "field2"
redis 127.0.0.1:6379> hvals myhash
The #hvals command only gets the values of all fields in the myhash key.
1) "hello"
2) "world" 


IV. データ型の設定(非順序型コレクション)

String型の要素を持つ非順序型セットで、要素は一意であり、メンバーの重複は許されない。複数のセットタイプ間でマージ、交差、差分操作を行うことができる。

Application scope.


  • RedisのSetデータ型を使って、ブログ訪問時のユニークなIPアドレス情報など、ユニークなデータを記録することができます。このシナリオでは、ブログにアクセスがあるたびに訪問者のIPをRedisに格納するだけで、Setデータ型が自動的にIPアドレスの一意性を保証してくれます。

  • Set型のサーバーサイドでの集約操作の容易さと効率性を活用し、データオブジェクト間の連想関係を維持するために使用することができます。たとえば、ある電子機器を購入したすべての顧客の ID が指定された Set に格納され、別の電子機器を購入した顧客の ID が別の Set に格納されている場合、両方のアイテムを購入した顧客を調べるには、Set の intersections コマンドを使用すると便利で効率的です。

1. sadd/smebers/scard/sistmember

redis 127.0.0.1:6379> sadd myset a b c
#insert test data, and since the key myset did not exist before, all three members of the argument are inserted normally.
(integer) 3
redis 127.0.0.1:6379> sadd myset a d e
# Since the a in the argument already exists in myset, this operation only inserts the two new members d and e.
(integer) 2
redis 127.0.0.1:6379> sismember myset a
# Determine if a already exists, a return value of 1 means it exists.
(integer) 1
redis 127.0.0.1:6379> sismember myset f
# Determine if f already exists, a return value of 0 means it does not exist.
(integer) 0
redis 127.0.0.1:6379> smembers myset
# The results of the insertion are viewed by the smembers command. From the results, we can see that the order of the output has nothing to do with the order of insertion.
1) "c"
2) "d"
3) "a"
4) "b"
5) "e"
redis 127.0.0.1:6379> scard myset
# Get the number of elements in a Set collection.
(integer) 5


2. spop/srem/srandmember/smove

redis 127.0.0.1:6379> del myset
# Delete the key for later testing.
(integer) 1
redis 127.0.0.1:6379> sadd myset a b c d
#Prepare test data for later examples.
(integer) 4
redis 127.0.0.1:6379> smembers myset
#View the location of the members in Set.
1) "c"
2) "d"
3) "a"
4) "b"
redis 127.0.0.1:6379> srandmember myset
# As you can see from the results, the command does return a random member.
"c"
redis 127.0.0.1:6379> spop myset
# Randomly remove and return a member of Set.
"b"
redis 127.0.0.1:6379> smembers myset
# View information about the members of Set after removal.
1) "c"
2) "d"
3) "a"
redis 127.0.0.1:6379> srem myset a d f
# removes the members a, d and f from Set, where f does not exist, so only the members a and d are removed, returning 2.
(integer) 2
redis 127.0.0.1:6379> smembers myset
# View the output after the move out.
1) "c"
redis 127.0.0.1:6379> sadd myset a b
#Prepare data for the smove command later.
(integer) 2
redis 127.0.0.1:6379> sadd myset2 c d
(integer) 2
redis 127.0.0.1:6379> smove myset myset2 a
# Move a from myset to myset2, as you can see from the result the move was successful.
(integer) 1
redis 127.0.0.1:6379> smove myset myset2 a
# Move a from myset to myset2 again. Since a is no longer a member of myset at this point, the move fails and returns 0.
(integer) 0
redis 127.0.0.1:6379> smembers myset
# Check the members of myset and myset2 separately to confirm if the move actually succeeded.
1) "b"
redis 127.0.0.1:6379> smembers myset2
1) "c"
2) "d"
3) "a"



V. ソート済みセットデータ型(zset、順序付きセット)

Sting型の要素を持つ順序付きセットで、要素は一意であり、繰り返されることはない。各要素には double 型のスコア score (重みを示す) が関連付けられ,重みの大きさでソートされ,要素が同じスコアを持つことができる.

Application scope.


  • 大規模なオンラインゲームのポイントリーダーボードに使用することができます。プレイヤーのスコアが変わるたびにZADDコマンドを実行してプレイヤーのスコアを更新し、以後はZRANGEコマンドでTOP10のポイントを持つユーザーの情報を取得することができます。もちろん、ZRANKコマンドでユーザー名によるプレイヤーのランキング情報を取得することも可能です。最後に、ZRANGEコマンドとZRANKコマンドを組み合わせて、あるプレーヤーと同じようなスコアを持つ他のユーザーの情報を素早く取得することができます。

  • Sorted-Set型は、インデックス付きのデータを構築するためにも使用できる。

1. zadd/zcard/zcoount/zrem/zincrby/zscore/zrange/zrank

redis 127.0.0.1:6379> zadd myzset 1 "one"
# Add a member with a score of 1.
(integer) 1
redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"
#Add two members with scores 2 and 3 respectively.
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
#0 means the first member, -1 means the last member. the WITHSCORES option means that each member and its score are included in the returned result, otherwise only the member is returned.
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis 127.0.0.1:6379> zrank myzset one
# Get the index value of member one's position in Sorted-Set. 0 means the first position.
(integer) 0
redis 127.0.0.1:6379> zrank myzset four
# Member four does not exist, so nil is returned.
(nil)
redis 127.0.0.1:6379> zcard myzset
# Get the number of members in the myzset key.
(integer) 3
redis 127.0.0.1:6379> zcount myzset 1 2
#zcount key min max, the number of members whose scores satisfy the expression 1 <= score <= 2.
(integer) 2
redis 127.0.0.1:6379> zrem myzset one two
# Delete members one and two, returning the number of members actually deleted.
(integer) 2
redis 127.0.0.1:6379> zcard myzset
# Check if the deletion was successful.
(integer) 1
redis 127.0.0.1:6379> zscore myzset three
#Get the score of member three. The return value is in string form.
"3"
redis 127.0.0.1:6379> zscore myzset two
# Since the member two has been deleted, this command returns nil.
(nil)
redis 127.0.0.1:6379> zincrby myzset 2 one
# member one does not exist, the zincrby command will add the member and assume its initial score is 0, increase the score of member one by 2, and return the updated score of the member.
"2"
redis 127.0.0.1:6379> zincrby myzset -1 one
# Increase the score of member one by -1 and return the updated score of that member.
"1"
redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
# Check if the score is correct after updating the members.
1) "one"
2) "1"
3) "three"
4) "3"


2. zrrangebyscore/zrrangebyrank/zrrangebyscore

redis 127.0.0.1:6379> del myzset
(integer) 1
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrangebyscore myzset 1 2
#zrangebyscore key min max, get the members whose scores satisfy the expression 1 <= score <= 2.
1) "one"
2) "two"
redis 127.0.0.1:6379> zrangebyscore myzset (1 2
# Get the members whose scores satisfy the expression 1 < score <= 2.
1) "two"
redis 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3
#-inf means the first member (the one with the lowest location index value, i.e. 0), +inf means the last member (the one with the highest location index value), the parameter after limit is used to limit the value of the returned member, 2 means starting from the member with location index equal to 2, and taking the next 3 members.
1) "three"
2) "four"
redis 127.0.0.1:6379> zrangebyscore myzset 0 4 limit 2 3
redis 127.0.0.1:6379> zremrangebyscore myzset 1 2
# Delete members whose scores satisfy the expression 1 <= score <= 2, and return the actual number deleted.
(integer) 2
redis 127.0.0.1:6379> zrange myzset 0 -1
# See if the above deletion was successful.
1) "three"
2) "four"
redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
# Delete members whose location indexes satisfy the expression 0 <= rank <= 1.
(integer) 2
redis 127.0.0.1:6379> zcard myzset
# Check if the previous command deleted successfully.
(integer) 0


3.ズレブレンジ/ズレブレンジビスコア/ズレブランク

redis 127.0.0.1:6379> del myzset
#Prepare the test data for the examples that follow.
(integer) 0
redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
(integer) 4
redis 127.0.0.1:6379> zrevrange myzset 0 -1 WITHSCORES
# Get and return members in this range in position index from highest to lowest.
1) "FOUR"
2) "4"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"
redis 127.0.0.1:6379> zrevrange myzset 1 3
# Since it is sorted from highest to lowest, position equal to 0 is FOUR, 1 is THREE, and so on.
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrank myzset one
# Since it is sorted from highest to lowest, the position of one is 3.
(integer) 3
redis 127.0.0.1:6379> zrevrank myzset four
# Since it is sorted from highest to lowest, the position of four is 0.
(integer) 0
redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0
#zrevrangebyscore key max min, Get the members whose scores satisfy the expression 3 >= score >= 0, and output them in order from highest to lowest.
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2
The #zrevrangebyscore command supports the limit option, which has the same meaning as this option in zrangebyscore, except that the positions are calculated and fetched in reverse order when calculating them.
1) "three"
2) "two"
redis 127.0.0.1:6379> zrevrangebyscore myzset +inf -inf limit 1 3