The five data types in Redis are easy to manipulate

  • 2020-05-27 07:32:09
  • OfStack

The five data types in Redis are easy to manipulate

Ask questions

Redis5 data types of simple add, delete, change and check command??

To solve the problem

Suppose you have installed the Redis server.
Suppose you have the Redis cli command line tool open.
Suppose you know something about Redis;

Redis simply add, delete, change and look up examples

Example 1: add, delete, change and check a string


# increase 1 a key for ay_key The value of the 
127.0.0.1:6379> set ay_key "ay"
OK
# The query ay_key The value of the 
127.0.0.1:6379> get ay_key
"ay"
# Modify the ay_key The value of the 
127.0.0.1:6379> set ay_key "new_ay"
OK
127.0.0.1:6379> get ay_key
"new_ay"
# Modify the ay_key The name of the 
127.0.0.1:6379> rename ay_key new_ay_key
OK
127.0.0.1:6379> keys *
1) "new_ay_key"
# delete ay_key
127.0.0.1:6379> del ay_key
(integer) 0
# Does the query exist ay_key 0
127.0.0.1:6379> exists ay_key
(integer) 0

Example 2: add, delete, change and check the Set set


# Deletes all in the currently selected database key
127.0.0.1:6379> flushdb
OK
# generate set Collection, add 4 A data 
127.0.0.1:6379> sadd set_ay_key "ay" "al" "xy" "xl"
(integer) 4
# The query set All the values inside 
127.0.0.1:6379> smembers set_ay_key
1) "xy"
2) "al"
3) "ay"
4) "xl"
# delete value for "xl" ,  return  1  If you don't return  0
127.0.0.1:6379> srem set_ay_key "xl"
(integer) 1
127.0.0.1:6379> smembers set_ay_key
1) "xy"
2) "al"
3) "ay"
# add value for "xl"
127.0.0.1:6379> sadd set_ay_key "xl"
(integer) 1
127.0.0.1:6379> smembers set_ay_key
1) "xy"
2) "al"
3) "ay"
4) "xl"
# add value for "xl"  It doesn't add, but it doesn't report an error, set Repetition is not allowed 
127.0.0.1:6379> sadd set_ay_key "xl"
(integer) 0
# Not much to explain 
127.0.0.1:6379> sadd set_ay_key "xl"
(integer) 0
# Not much to explain 
127.0.0.1:6379> sadd set_ay_key "xl"
(integer) 0

Example 3: add, delete, change and check the List set


# add key for list_ay_key the list A collection of 
127.0.0.1:6379> lpush list_ay_key "ay" "al" "xy" "xl"
(integer) 4
# The query key for list_ay_key A collection of 
127.0.0.1:6379> lrange list_ay_key 0 -1
1) "xl"
2) "xy"
3) "al"
4) "ay"
# to list Tail-add element 
127.0.0.1:6379> rpush list_ay_key "together"
(integer) 5
# to list Header add element 
127.0.0.1:6379> lpush list_ay_key "first"
(integer) 6
# The query list A collection of 
127.0.0.1:6379> lrange list_ay_key 0 -1
1) "first"
2) "xl"
3) "xy"
4) "al"
5) "ay"
6) "together"
# update index for 0 The value of the   
127.0.0.1:6379> lset list_ay_key 0 "update_first"
OK
127.0.0.1:6379> lrange list_ay_key 0 -1
1) "update_first"
2) "xl"
3) "xy"
4) "al"
5) "ay"
6) "together"
# delete index for 1 On the value of the 
127.0.0.1:6379> lrem list_ay_key 1 "update_first"
(integer) 1
127.0.0.1:6379> lrange list_ay_key 0 -1
1) "xl"
2) "xy"
3) "al"
4) "ay"
5) "together"

Example 4: addition, deletion, alteration and check of the Hash set (similar to Java)


127.0.0.1:6379> flushdb
OK
# generate hash Collection, and add key  for uuid_one value  for "12345"
127.0.0.1:6379> hset hash_ay_key "uuid_one" "12345"
(integer) 1
127.0.0.1:6379> hlen hash_ay_key
(integer) 1
# Returns all of the key
127.0.0.1:6379> hkeys hash_ay_key
1) "uuid_one"
# Returns all value
127.0.0.1:6379> hvals hash_ay_key
1) "12345"
# Set add value 
127.0.0.1:6379> hset hash_ay_key "uuid_two" "22222"
(integer) 1
# Set add value 
127.0.0.1:6379> hset hash_ay_key "uuid_three" "33333"
(integer) 1
# To obtain key for uuid_one The value of the 
127.0.0.1:6379> hget hash_ay_key uuid_one
"12345"
# delete key for uuid_three The value of the 
127.0.0.1:6379> hdel hash_ay_key uuid_three
(integer) 1
127.0.0.1:6379> hkeys hash_ay_key
1) "uuid_one"
2) "uuid_two"
# Get all, including key and value
127.0.0.1:6379> hgetall hash_ay_key
1) "uuid_one"
2) "12345"
3) "uuid_two"
4) "22222"
# update key for uuid_one The value of the 
127.0.0.1:6379> hset hash_ay_key uuid_one "11111"
(integer) 0
127.0.0.1:6379> hset hash_ay_key "uuid_one" "11111"
(integer) 0
127.0.0.1:6379> hgetall hash_ay_key
1) "uuid_one"
2) "11111"
3) "uuid_two"
4) "22222"

Example 5: addition, deletion, alteration and check of SortedSet set

SortedSet is an ordered set of set


#sorted set Add value ay  Sort a value of  1
127.0.0.1:6379> zadd zset_ay_key 1 "ay"
(integer) 1
127.0.0.1:6379> zadd zset_ay_key 2 "al"
(integer) 1
127.0.0.1:6379> zadd zset_ay_key 3 "xy"
(integer) 1
127.0.0.1:6379> zadd zset_ay_key 4 "xl"
(integer) 1
# Query for all values 
127.0.0.1:6379> zrange zset_ay_key 0 -1
1) "ay"
2) "al"
3) "xy"
4) "xl"
# Delete all values 
127.0.0.1:6379> zrem zet_ay_key "xl"
(integer) 0
127.0.0.1:6379> zrange zset_ay_key 0 -1
1) "ay"
2) "al"
3) "xy"
4) "xl"

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: