Introduction and basic usage of Redis's five basic data types

Redis five data types

Redis key basic command

set key name -- Set a basic key value pair
keys * -- View all keys
exists key -- View the corresponding key Does it exist
move key Number of corresponding database -- Put this key Move the corresponding data to another database
expire key Expiration time(Unit second) -- Set how long this data expires
ttl key -- Check how long the data will expire
			-- here-1 Indicates that there is no expiration time
			-- -2 It means it has expired
type key -- View current key What is the corresponding data type

String - string

  • This is the string type
  • Generally, the key and name added by using the set command are of string type

String related commands

  1. Splice string
append key String to add
  • If the current string does not exist, the corresponding key value pair will be automatically created

  1. Get string length
strlen key

Get string length through key

  1. i + + operation
  • It is an operation that automatically helps us add 1 to the counter
incr key

  • Think about some basic website views and playback volume, which can be realized by redis
decr key
  • This is the corresponding i - operation
incrby key numerical value
  • This is to increase the corresponding value
decrby key numerical value
  • Subtraction operation

  1. Get string range operation
getrange key Start end

  1. String replacement operation
setrange key The subscript of the string to start the replacement and the character to complete the replacement

  1. Action on expiration time
setex key Expiration time value
  • This is to set the corresponding expiration time

setnx key values
  • This is only set when there is no corresponding key

  • Note that this instruction is often used in distributed locks
  1. Batch setting key value pairs
mset key1 value1 key2 value2 ...
	-- Note that it can also be used here msetnx of,It is created only if the value does not exist
	-- And this is an atomic operation,Only when all values can be created can it succeed
	-- As long as one is not created successfully, it will fail

mget k1 k2 k3
  • This is batch acquisition
  1. Create an object
set user:1 {name:wan,age:18}
  • Generally, it is set as a json string to save the object
set user:1:name wan
set user:1:age 18
  • But more recommend this design
  • Here is the object name: Number: field
  1. View before set command
getset key value
  • This is to check the value corresponding to the key first
  • Then change the corresponding value to your newly set value

List - List

  • Remember first that all list commands start with l
  1. Create a List collection
lpush Collection name value
  • To add a value, just continue using this instruction

  1. Get all values
lrange Collection name 0 -1

  • Note that the acquisition order here is reversed
  1. Add data to beginning
rpush Collection name value

  • To understand, r and l correspond to right and left respectively
  1. Get value
r/lpop key
  • Choose to get the value from the left and right

lindex list subscript
  • This is just to get the value of the corresponding subscript

  1. Gets the length of the list collection
llen list

  1. Removes the specified value
lrem The collection name corresponds to the quantity to be removed value

ltrim The range of collection names to keep

rpoplpush Set 1 set 2
  • This is to add the rightmost element of set 1 to the leftmost element of set 2

  1. Some insert operations
exists Collection name

lset The element name to be modified at the corresponding position of the collection name
	-- This means that an error will be reported if the corresponding position does not exist,If it exists, it will be modified to the current value

linsert Collection name before The corresponding value is the value to add
	-- This is to add the value to be added in front of the corresponding value
	-- If you change to after Is to add a value after the corresponding value

Set - set

  • The attributes of this collection are the same as those in java, that is, the values cannot be repeated
  • Remember first that all set commands start with s
  1. Create a set collection
sadd Collection name value name

  1. View collection elements
smembers Collection name
  1. Determine whether a value exists
sismember Value name corresponding to collection name	

  1. See how many elements there are in the collection now
scard Set name

  1. Remove an element
srem Collection name element name

  1. Get elements randomly
srandmember Collection name

spop Collection name 
	-- This is ridiculous,Randomly delete elements in the collection
  1. Move elements to another collection
smove Set 1 set 2 elements
	-- This is to move the elements in set 1 to set 2

  1. Some mathematical operations
  • This has some mathematical operations
  • Such as intersection and union set, difference set and so on
sdiff Set 1 set 2
	-- This is to find set 1 and compare the elements that are not in set 2

sinter Set 1 set 2
	-- This is the union

sunion Set 1 set 2
	-- This is the union of set 1 and set 2

  • It is also verified that the order of elements in set is chaotic

Use

Can be used for common concern function

Just use the union of set

Hash - hash

  • This can be viewed as a map set
  • The key - map is stored inside
  • Another map can be extracted through the key
  • That is, key value pairs store a collection
  • Remember that all hash commands begin with h
  1. Create a hash collection
hset Collection name key value

  • mget and mset can be used here as well

  1. Get value
hgetall hash Collection name

  1. delete
hdel hash Collection name key name
  • This is to delete the specified key and the corresponding value

  1. Get length
hlen Set name

hkeys myhash
	-- This is acquisition hash All of key

  1. Other operations
  • Remember the previous operations?
  • Is the increase and decrease in String
  • There are also in the hash
hincrby hash Collection name key Value added by name

  • Even setnx exists
hsetnx hash Collection name key name values name

Use

This is much better than the previous method of using object name: id: field name

Zset - ordered set

  • This is just more sorting than set
  • That's weird, mom. There's a lot more, okay
  1. Create a zset collection
zadd Collection name key value

In fact, it is the same as the previous set

  1. Implementation of sorting
zrangebyscore The name of the collection to sort -inf +inf

The following - inf and + inf mean from negative infinity to positive infinity

  • In fact, the scope here can also be specified
  • If I change the + inf to 2000, the lin will not be displayed

zrevrange zset Name 0 -1
  • This is the order from big to small

  • For details, check the zrangebyscore instruction
  1. Delete the element in the corresponding collection
zrem Corresponding to the collection name value Value of

  1. Get how much data there is in the corresponding library
zcard zset name

zcount zset Name start end
	-- This indicates how many pieces of data are in the range from start to end

Use

Can store class grades, wages and so on

You can also set the weight of the data. For example, 1 represents the most important, followed by 2

And leaderboards and so on

Keywords: Database Redis data structure Data Warehouse

Added by colake on Tue, 21 Sep 2021 14:39:22 +0300