The first day of Redis
[TOC]
The introduction of Redis
-
The definition of Redis
-
Redis is open source and BSD compliant
-
Redis is a high-performance key-value storage system
-
Redis is similar to Memcached
-
Redis supports storing more value types
-
Redis data type
Data type name Data Name English string string The list list A collection of set An ordered set zset The hash hashs
-
-
The role of Redis
- Redis is a high performance key-value database.
- Redis has largely compensated for the lack of key/value storage like memcached.
- In some cases, it can complement the relational database.
- It provides Python, Ruby, Erlang, PHP clients, easy to use.
- The latest version is 3.6.0
-
The characteristics of Redis
- persistence
- Redis supports persistence of data
- You can keep data in memory on disk
- You can load it again when you restart
- diversity
- Redis supports more than simple key-value data
- It also provides the storage of list, set, zset, hash and other data structures
- security
- Redis supports backup of data
- Data backup in master-slave mode
- persistence
-
The alias of Redis
- Data structure server
- Redis is a completely open source, free, BSD compliant, advanced key-value persistence product
- Memory level database
- A memory – based network storage system
- Remote dictionary server
- Remote Dictionary Server
- Data structure server
-
Application scenarios for Redis
- The operation of fetching the latest N data
- Ranking application, select TOP N operation
- Apps that require precise expiration dates
- Counter application
- Uniq operation to obtain the weights of all data in a certain period of time
- Real-time system, anti-garbage system
- Pub/Sub builds real-time messaging systems
- Building a queue system
- The cache
-
The advantage of Redis
- High performance
- Redis can read 110,000 times per second
- I’m writing 81,000 times per second
- Rich data types
- Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets data type operations
- atomic
- All operations of Redis are atomic
- Redis also supports atomic execution of several operations after full union
- Rich features
- Redis also supports publish/subscribe, notification, key expiration, and more
- High performance
-
Redis’ competitive distinction
- Redis has more complex data structures and provides atomic operations on them, which is a different evolutionary path from other databases.
- Redis data types are transparent to programmers while being based on basic data structures without additional abstractions.
- Redis runs in memory but can persist to disk, so memory is a tradeoff between high-speed reads and writes to different data sets,
- Because the amount of data can’t be larger than the hardware memory. Another advantage of having an in-memory database is that, compared to the same complex data structures on disk,
- It’s very easy to operate in memory, so Redis can do a lot of things with a lot of internal complexity.
- At the same time, they are compact in terms of disk formats and are produced in an appending manner because they do not require random access.
The installation of Redis
- The installation of Redis in Ubuntu under Linux
- Apt to get the source installation
- $sudo apt-get update
- $sudo apt-get install redis-server
- Start the Redis
- $ redis-server
- Check whether Redis is started
- $ redis-cli
- Redis test
- PING
- PONG means successful installation
- PING
- Apt to get the source installation
- Installation of Redis under Windows
- Access the address in the browser
- Github.com/MSOpenTech/…
- After the download is complete, unzip the folder and rename it to ask Redis
- Open CMD
- Run the CD command to access the Redis directory
- Run the code
- redis-server.exe redis.windows.conf
- Open a new CMD window
- Do not close the original window, otherwise you will not be able to access the server
- Switch to the Redis directory
- Run the code
- Redis -cli.exe -h 127.0.0.1 -p 6379
- Access the address in the browser
Redis data type
The NX in the command means to add the Key if it does not exist
If yes, failure is returned
Used for mode overwriting
String(String)
- A String of introduction
- String is the simplest type, where a Key corresponds to a Value.
- String is binary.
- A String can contain any data, such as JPG images or serialized objects.
- SET (Add) key-value pairs (SET)
- Set Key name Value of the key
- set Key Value
- Sets a key and value, overrides if the key exists, and returns OK
- Set Key name Value of the key
- View key value pairs (GET)
- Name of the get key
- get Key
- Returns the value of a key
- Name of the get key
- Setting (Adding) Key-value Pairs (SETNX)
- Setnx Key name Value of the key
- setnx Key Value
- The value of a key is set only if the key does not exist
- Failure to return 0 if key already exists (to prevent overwriting)
- Setnx Key name Value of the key
- Set (add) a key-value pair with an expiration time (SETEX)
- Setex key name valid time the value corresponding to the key
- setex Key time Value
- Setex key name valid time the value corresponding to the key
- Viewing the remaining time of key-value pairs (TTL)
- Name of the TTL key
- ttl Key
- Returns, in seconds, the remaining lifetime of a given key
- When the key does not exist, -2 is returned.
- Returns -1 if the key exists but no remaining lifetime is set.
- Name of the TTL key
- Replacement string (SETRANGE)
- The name of the setrange key specifies the position of the string to be replaced the string to be replaced
- setrange Key num string
- Specifies that the position of the string to be replaced starts at 0
- Replace string (half-overwrite)
- The replacement length is determined by the string length
- Abcdefg replaces bad with c, resulting in abbadfg
- The name of the setrange key specifies the position of the string to be replaced the string to be replaced
- Batch Set (Add) Key/value pairs (MSET)
- Mset key 1 value 1 key 2 value 2 key 3 value 3
- mset V1 K1 V2 K2 V3 K3
- Set keys and values in batches
- Ok is returned on success
- Mset key 1 value 1 key 2 value 2 key 3 value 3
- Bulk acquisition (MGET)
- Mget key 1 key 2 key 3
- mget k1 k2 k3
- Gets the value of the specified key in batches
- Mget key 1 key 2 key 3
- Batch Set (Add) Key/Value Pairs (MSETNX)
- Msetnx key 1 value 1 key 2 value 2 key 3 value 3
- msetnx K1 V1 K2 V2 K3 V3
- Batch set nonexistent keys and values
- Success is achieved when the key does not exist
- OK is returned on success
- None of the Settings will succeed if the set key already exists
- Msetnx key 1 value 1 key 2 value 2 key 3 value 3
- View and change the value of the key-value pair (GETSET)
- Getset Key name Value of the key
- getset Key Value
- Gets the original value and sets the new value
- Getset Key name Value of the key
- Gets the value of the specified key-value pair (GETRANGE)
- Getrange Start position end position of the key name
- getrange Key Start End
- Get values in the specified range (get values in the specified Start to End position)
- The string position is evaluated from 0
- Getrange Start position end position of the key name
- Add +1 to the value of the key-value pair (INCR)
- Name of the incr key
- incr Key
- Add 1 to the value of the specified key
- The value must be a number
- Returns the added result
- Name of the incr key
- Add +n(INCRBY) to the value of the key-value pair.
- Incrby Number added to the name of the key
- incrby Key num
- Add num to the value of a key
- The value must be a number
- Returns the added result
- The key is set if it does not exist
- Num can be a positive or negative integer
- Incrby Number added to the name of the key
- DECR the value of the key-value pair.
- Name of the decr key
- decr Key
- Specifies that the value of the key is subtracted by 1
- The value must be a number
- Returns the subtracted result
- Name of the decr key
- -n(DECRBY) for the value of the key-value pair.
- Decrby The number to subtract from the name of the key
- decrby Key num
- Add num to the value of a key
- The value must be a number
- Returns the subtracted result
- A key-value pair is set if it does not exist
- Num can be a positive or negative integer
- Decrby The number to subtract from the name of the key
- APPEND the value of the key-value pair (APPEND)
- Append key name String to append
- append Key String
- Appends a string to the value of the specified key
- Returns the length of the new value
- Append key name String to append
- Get the length of the key (STRLEN)
- Strlen The name of the key
- strlen Key
- Gets a key length
- Returns the length of the
- Strlen The name of the key
Hashs (hash)
- The difference between hashes and strings
- String(String)
- All values (which can be any data type) are stored in one key
- In a block of memory
- Hashs (hash)
- All values are also stored in one key
- In different blocks of memory
- Each block is called a field
- String(String)
- Set (Add) a key-value pair (HSET)
- Hset Key name Field name of the field
- hset Key Field Value
- Set a key in which to hold fields and values
- Hset Key name Field name of the field
- Set (Add) a key-value pair (HSETNX)
- Hsetnx Key name Field name Of the field
- hsetnx Key Field Value
- Sets fields and values that do not exist in a key
- If the field exists, an error is reported
- Success returns 1
- Return 0 on failure
- Hsetnx Key name Field name Of the field
- Set the value of fields in key value pairs in batches (HMSET)
- Hmset Key name Field Name Value of the field Field name Value of the field Field name Value of the field Field name Value of the field
- hmset Key Field1 Value1 Field2 Value2 Field3 Value3
- In a key, batch set fields
- Hmset Key name Field Name Value of the field Field name Value of the field Field name Value of the field Field name Value of the field
- Gets the value of the specified field for the specified key (HGET)
- Hget key name Field name
- hget Key Field
- Gets the value of a specified field in a key
- Hget key name Field name
- Gets the value of multiple specified fields for the specified key (HMGET)
- Hmget Key name Field name 1 Field name 2 Field name 3
- hmget Key Field1 Field2 Field3
- Gets the value of one or more fields in the key
- Hmget Key name Field name 1 Field name 2 Field name 3
- Determine whether a field is in a key (HEXISTS)
- Hexists Key name Field name
- hexists Key Field
- Checks whether the specified field exists in a key
- Presence returns 1
- There is no return 0
- Hexists Key name Field name
- Gets the number of fields for the specified key (HLEN)
- Name of the hlen key
- hlen Key
- Returns the number of fields for the specified key
- Name of the hlen key
- Get all field names in the key (HKEYS)
- Name of the hkeys key
- hkeys Key
- Gets the names of all fields in the key
- Returns the names of all fields for the specified key
- Name of the hkeys key
- Get all column values in the key (HVALS)
- Name of the hVALS key
- hvals Key
- Gets all field values in the key
- Returns all field values for the specified key
- Name of the hVALS key
- Get all field names and field values in the key (HGETALL)
- Name of the hgetall key
- hgetall Key
- Gets all fields and values in the key
- Name of the hgetall key
- Add +n(HINCRBY) to the specified field value in the key
- Hincrby Key name Value to be added to the field name
- hincrby Key Field num
- Increments the value of the specified field in the key by the specified number
- Field values must be numeric
- Hincrby Key name Value to be added to the field name
- Delete one or more fields (HDEL)
- Hdel key name Field name 1 Field name 2 Field name 3
- hdel Key Field1 Field2 Field3
- Deletes one or more fields in a key
- Hdel key name Field name 1 Field name 2 Field name 3
The List (List)
- The List of profile
- List is a linked List structure
- The main function of a List is to push, pop, and get all the values of a range
- Key is the name of a linked List
- A List is essentially a bidirectional List where each child element is a string
- List allows duplicate values
- Insert one or more values from scratch (LPUSH)
- Lpush key name the value of the key 1 the value of the key 2 the value of the key 3
- lpush Key Value1 Value2 Value3
- Writes one or more values to the queue from the left side of the queue
- The left side of the queue is the queue header
- On the right is the end of the queue
- Lpush key name the value of the key 1 the value of the key 2 the value of the key 3
- Fetching the specified value from the list (LRANGE)
- Lrange key name start subscript End subscript
- lrange Key Start End
- Gets the specified return value from the queue
- From the left of the queue to the right
- The subscript
- 0 represents the first element in the queue
- 1 is the second element, and so on
- -1 represents the last element in the queue
- Minus 2 is the next-to-last element, and so on
- Lrange key name start subscript End subscript
- Insert one or more values from the tail (RPUSH)
- Rpush key name The value of the key 1 the value of the key 2 the value of the key 3
- rpush Key Value1 Value2 Value3
- Writes one or more values to the queue from the right side of the queue
- Rpush key name The value of the key 1 the value of the key 2 the value of the key 3
- Inserts a value at the specified position (LINSERT)
- Linset the value to be inserted before/after the name of the key as the positioned value
- linsert Key before / after Value1 Value2
- Inserts a new value before or after the specified element in the queue
- Linset the value to be inserted before/after the name of the key as the positioned value
- Modify the value specified in the key (LSET)
- The name of the lset key specifies the new subscript value
- lset Key num Value
- Sets a new value for the specified element in the queue
- The name of the lset key specifies the new subscript value
- Deletes the specified number of values specified in the key (LREM)
- Lrem key name Number of values to be deleted Values to be deleted
- lrem Key num Value
- Deletes num elements from the queue whose value is the value to be deleted
- Num > 0 Deletes num elements from the head of the queue to the end
- Num < 0 Deletes num elements from the end of the queue to the head
- Num = 0 deletes all elements whose values are the values to be deleted
- Lrem key name Number of values to be deleted Values to be deleted
- Trim queue (LTRIM)
- Ltrim Key name start subscript End subscript
- ltrim Key Start End
- Trim the queue
- Makes the queue hold only elements within the specified range
- Ltrim Key name start subscript End subscript
- Delete the first value (LPOP)
- Name of the LPOP key
- lpop Key
- Removes a value from the left of the specified queue
- Name of the LPOP key
- Delete the last value (RPOP)
- Name of the RPOP key
- rpop Key
- Removes a value from the right of the specified queue
- Name of the RPOP key
- Delete the last value in the original queue and write it to another queue (RPOPLPUSH)
- Rpoplpush The key name of the original queue and the key name of another queue
- rpoplpush Key1 Key2
- Removes the last element of the source queue
- Writes the element to the head of the destination queue
- The two queues are the same
- This operation is equal to putting the rightmost one on the left
- So you put the last one in the first one
- Rpoplpush The key name of the original queue and the key name of another queue
- Get the specified value (LINDEX)
- The name of the lindex key specifies the subscript
- lindex Key num
- Gets the value of the specified subscript element in the queue
- The name of the lindex key specifies the subscript
- Get the length of the queue (LLEN)
- Llen key name
- llen Key
- Get the length of the queue
- Llen key name
Unordered Sets
- Sets the introduction
- Sets are set
- Sets are unordered collections of type string
- It’s done by hashing
- For Sets, we can take union, intersection and difference Sets
- Through these operations we can realize the friend recommendation in social networking sites and blog tag function
- Sets cannot have duplicate values
- Set (Add) values to collections (SADD)
- Sadd Key name Value of the key 1 Value of the key 2 value of the key 3
- sadd Key Value1 Value2 Value3
- Adds one or more elements to the collection
- Sadd Key name Value of the key 1 Value of the key 2 value of the key 3
- Get all values in the collection (SMEMBERS)
- Name of the smembers key
- smembers Key
- Gets all the elements in the collection
- Name of the smembers key
- Delete one or more values from the collection (SREM)
- Srem key name The value of this key is 1 and the value of this key is 2
- srem Key Value1 Value2
- Removes the specified one or more elements from the collection
- Srem key name The value of this key is 1 and the value of this key is 2
- Randomly remove an element from the collection (SPOP)
- Name of the SPOP key
- spop Key
- Randomly removes an element from the collection
- And returns the deleted element
- Name of the SPOP key
- Randomly get one or more elements of the collection (SRANDMEMBER)
- The number returned by the name of the srandMember key
- srandmember Key Num
- Returns one or more elements of the collection randomly, but does not delete them
- The number returned by the name of the srandMember key
- Get the number of collection elements (SCARD)
- Name of the scard key
- scard Key
- Gets the number of elements in the collection
- Name of the scard key
- Determine whether it is an element of a set (SISMEMBER)
- Value specified by the name of the sismember key
- sismember Key Value
- Determines whether a specified value is a member of the collection
- Is to return 1
- Instead of returning 0
- Value specified by the name of the sismember key
- Return the difference set of two combinations (SDIFF)
- Sdiff key name 1 Key name 2
- sdiff Key1 Key2
- Returns the difference between sets 1 and 2. Let’s do set 1
- Sdiff key name 1 Key name 2
- Put the difference set of two sets into a new set (SDIFFSTORE)
- Sdiffstore New key Name Key name 1 Key name 2
- sdiffstore Key-N Key1 Key2
- Returns the difference between sets 1 and 2 and stores the result into the new set
- Sdiffstore New key Name Key name 1 Key name 2
- Get the intersection of two sets (SINTER)
- Name of the sinter key 1 name of the sinter key 2
- sinter Key1 Key2
- Gets the intersection of two sets
- Name of the sinter key 1 name of the sinter key 2
- Put the intersection of two sets into a new set (SINTERSTORE)
- Sinterstore New key name. Key name 1. Key name 2
- sinterstore Key-N Key1 Key2
- Get the intersection of sets 1 and 2 and store the result into the new set
- Sinterstore New key name. Key name 1. Key name 2
- Obtain the union of two sets
- Sunion key name 1 Key name 2
- sunion Key1 Key2
- Gets the union of the specified set
- Sunion key name 1 Key name 2
- Put the union of two sets into a new set (SUNIONSTORE)
- Sunionstore New name key name 1 key name 2
- sunionstore Key-N Key1 Key2
- Gets the union of the specified collection and saves the result as a new collection
- Sunionstore New name key name 1 key name 2
- Puts the specified value of one collection into another collection (SMOVE)
- Smove Name of the key to be moved Name of the key to be moved Value to be moved
- smove Key-O Key-N Value
- Moves the specified value from the source collection to the target collection
- Smove Name of the key to be moved Name of the key to be moved Value to be moved
Sorted Sets
- Sorted Sets
- Sorted Sets is an upgraded version of Sets
- Sorted Sets define a score for each element in the set
- Sorted elements in Sorted Sets are Sorted by their fractions
- Sorted Sets are not allowed to have duplicate values
- Each member of a Sorted set has a score
- In Sorted Sets you can specify multiple scores/member combinations
- In a Sorted set, if a specified member is already in the corresponding ordered set
- The score is then updated to the latest
- And the member will be readjusted to the correct position
- To make sure the set is in order
- The value of a score in a Sorted set must be a string representing a number
- And can be a float of type double
- Set (Add) key score value pairs (ZADD)
- Zadd key name Score 1 Value of the number of scores 1 value of the number of scores 2 Value of the number of scores 2
- zadd Key Score1 Value1 Score2 Value2
- This command adds the specified member to the ordered collection corresponding to the Key
- Zadd key name Score 1 Value of the number of scores 1 value of the number of scores 2 Value of the number of scores 2
- Returns the value specified in the collection (ZRANGE)
- Zrange key name start subscript end subscript
- zrange Key Start End [withscores]
- Returns a member of an ordered collection within a specified interval
- Members are ranked from smallest to largest by score
- Members with the same score are sorted in lexicographical order
- withscores
- Returns the same time as the elements in the collection
- Returns its score
- Zrange key name start subscript end subscript
- Returns the value specified in the collection (ZREVRANGE)
- Zrevrange key name start subscript end subscript [whether to return its score]
- zrevrange Key Start End [withscores]
- Returns a member of the specified interval in an ordered collection
- Its members are ranked from highest to lowest score
- Members with the same score are sorted in lexicographical order
- withscores
- Returns the same time as the elements in the collection
- Returns its score
- Zrevrange key name start subscript end subscript [whether to return its score]
- Returns the value of the specified score in the collection (ZRANGEBYSCORE)
- Set of names for the ZrangebyScore key
- zrangebyscore Key Num-S Num-E [withscores]
- Returns the value of score in the specified interval in the ordered collection
- Members with the same score are sorted in lexicographical order
- withscores
- Returns the same time as the elements in the collection
- Returns its score
- Set of names for the ZrangebyScore key
- Remove the specified value from the collection (ZREM)
- Name of the zrem key. The value of this key is 1. The value of this key is 2
- zrem Key Value1 Value2
- Deletes a value specified in an ordered collection
- Name of the zrem key. The value of this key is 1. The value of this key is 2
- Assign scores to values in the set +n(ZINCRBY)
- Zincrby key name increment to increment the value of the score
- zincrby Key num Value-S
- Increments (num) to the score value of a member of an ordered set with a specified value
- If the set does not have this value, add a value with a fraction of num
- Zincrby key name increment to increment the value of the score
- Returns the value specified in the collection (ZRANK)
- Value specified by the name of the zrank key
- zrank Key Value
- Returns the index of the specified value in an ordered collection
- Values are sorted from smallest to largest by score
- Value specified by the name of the zrank key
- Returns the specified value in the collection (ZREVRANK)
- Value specified by the name of the zRevrank key
- zrevrank Key Value
- Returns the index of the specified value in an ordered collection
- Values are sorted from highest to lowest by score
- Value specified by the name of the zRevrank key
- Returns the number of values between specified fractions in the set (ZCOUNT)
- Zcount Key name Start score End score
- zcount Num-S Num-E
- Return to the ordered collection
- Score The number of points between the starting score and the cut-off score
- Zcount Key name Start score End score
- Returns the number of values in the set (ZCARD)
- Name of the zcard key
- zcard Key
- Returns the number of ordered collection elements
- Name of the zcard key
- Deletes the value of the specified score range from the collection (ZREMRANGEBYSCORE)
- Zremrangebyscore Specifies the name of the key
- zremrangebyscore Key Num-S Num-E
- Deletes an ordered collection
- The element whose score is in the specified interval
- Zremrangebyscore Specifies the name of the key
- Put the intersection of multiple collections into a new collection (ZINTERSTORE)
- Zinterstore New key name Set number of intersection key name 1 Key name 2
- zinterstore Key-N num Key1 Key2
- Take the intersection of sets 1 and 2 and save the result into the new set
- Before calculating the intersection, you need to specify the number of sets to calculate the intersection
- In an intersection set, the fraction of a value is the sum of fractions in multiple sets
- Zinterstore New key name Set number of intersection key name 1 Key name 2
- Put the union of multiple collections into a new collection (ZUNIONSTORE)
- Zunionstore Name of the new key Number of the set to take the union name of the key 1 Name of the key 2
- zunionstore Key-N num Key1 Key2
- Take the union of sets 1 and 2 and save the result into the new set
- Before calculating the union, you need to specify the number of sets to evaluate the union
- In a union set, the fraction of a value is the sum of fractions in multiple sets.
- Zunionstore Name of the new key Number of the set to take the union name of the key 1 Name of the key 2
Selection of individual data types
The data type | The right job to do |
---|---|
Hash types (Hashs) | Suitable for data caching |
Linked List types | Good for array search |
Unordered set types (Sets) | Good for friend push |
Sorted Sets (Sorted Sets) | Fit for commodity ranking |
PS
- Find the specified key (KEYS)
- Keys Specifies the name of the key
- keys K? e*y
- Wildcard characters are supported
- View all the keys in the library
- keys *
- Keys Specifies the name of the key
- Delete key value Pairs (DEL)
- Name of the del key
- del Key
- Delete a key
- Success returns 1
- Failure returns 0 if the key-value pair does not exist
- Name of the del key
- How to enable Redis
- First, install Redis on the server. Remember to change the configuration
- Next, install phP-Redis
- Then, you need to know the basic sentences