diligence, desolate, journeys by chance, destroyed in a random
--Han Yu's "Jin Xue Solution"because of the need to use Redis in the work, so recently took some time to read, now will learn the contents of the collation.
First, Introduction1. What is Redis? Redis is an in-memory, Key-value-based
data structure storage System that can be used as a database, cache, and message middleware. Official website: Redis.io, Chinese website: redis.cn. 2. Why use Redis? -Multifunction: Can be used as database, cache and message middleware (for interprocess communication);-supports multiple types of data structures, such as strings, hashes, lists, etc.;-support for most programming languages, such as: C + +, Java, Python, etc.;-Provide different levels of persistence (landing) method to ensure data security;-provides high availability through Redis Sentinel and auto partitioning;-Atomic operation to ensure the correctness of the data; 3. Download and install (Debian)Download: $ sudo apt-get install redis-server
configuration: Redis configuration file is redis.conf , the file contains all the parameters and detailed explanations that may be usedStartup: $ redis-server connections: $ redis-cli
II. supported data StructuresRedis is a key-value storage system, and Redis's key is binary safe and can use any binary sequence as a key, plain string, or even a JPEG file. Redis supports multiple types of data structures: strings, lists, sets, sorted sets, hashes, bitmaps, hyperloglogs. 1, Strings-stringthe value of the strings type, like key, can be any type of string.
1 Example:
2> SET name aut #Set the value of name to aut
3> GET name #Get the value of name
4> SET age 20 #Set value
5> INCR age # num is incremented by 1
6> MSET name aut age 20 #Set values for multiple keys at the same time
Examples of use: counters, storing binary files such as images2. Lists-lista key-value list that supports the addition and deletion of both ends, the range of values, and other operations
1 Example:
2> RPUSH study redis #right end insert
3> LPOP study #Left end delete
4> LRANGE study 1 5 #Remove the value in the range, the subscript starts from 0, -n means the nth position from the bottom
5> LTRIM study 1 2 #Take the specified length from the left
Examples of use: Message Queuing for interprocess communication, blog engine store comments3. Hashes-Hash Tablea key in the hashes can correspond to a structure that contains multiple field-value pairs, and contains a field-value pair that is only limited by memory
1 Example:
2> HMSET user: 2547 name aut school xupt age 20 #Set the value of the field
3> HGET user: 2547 name #Get the value of a single field
4> HMGET user: 2547 name school #Get values of multiple fields
5> HGETALL user: 2547 #Get the value of all fields
6> HINCRBY user: 2547 age 10 #Increment the specified field by a certain value
Use Example: Store user information, item information 4, sets-(unordered) collectionsets is used for storing unordered and non-duplicated data, providing insertions and deletions within a collection, orthogonal between collections, and so on .
1 Example:
2> SADD score 98 95 64 #insert element
3> SMEMBERS score #Get all elements
4> SISMEMBER score 80 # test if an element is in the set
5> SINTER score1 score2 #Get the intersection of two sets
6> SRANDMEMBER score #Get one element in the set randomly
Use Example: Store all objects with a common attribute5, Sorted sets-(ordered) setsorted sets uses a floating-point number score to sort the elements within the set, and when score is sorted alphabetically by alphabetical order
1 example:
2> zadd user 2547 aut insert the element, and update the score if the element already exists
3> zrange user 0-1> get elements in the specified range
4> zrank user aut ා get the ranking of the specified element
5> zrangebyscore user-inf 2000 WithCores ා get elements with score greater than or equal to 2000, and print out the score together
Use Example: Store rank information6. Bitmaps-Bitmapbitmaps implements certain tagging functions in bits, providing values/values, statistics, etc.
1 Example:
2> SETBIT key 10 1 #Set the specified bit to 1
3> GETBIT key 10 #Get the value of the specified bit
4> BITCOUNT key # Count the number of bits of the specified element to 1
Use Example: Mark User ID (whether registered, online or offline)7, Hyperloglogs-(do not know how to translate ~ ~)Hyoerloglogs calculates the cardinality estimate of the input element by the INPUT element, detail reference: REDIS data structure Hyperloglog
1 Example:
2> PFADD str1 "apple" "banana" "cherry" #Add elements
3> PFCOUNT str1 #Statistic approximate cardinality of str1
4> PFMERGE str1 & 2 str1 str2 #Merge str1 and str2 into str1 & 2
Examples of use: statistics on the number of independent IP sites visited Other common operations:
1> EXISTS key #Check if key exists
2> DEL key #Delete key
3> TYPE key #Detect the type of key
4> EXPIRE key 5 #Set the valid time of the key to 5 seconds
5> TTL key #Get the remaining valid time of the key
more commands to use see commands
third, disk persistencesince Redis is an in-memory data structure storage system, all data is manipulated in memory, and redis persistence is required when data needs to be persisted. Persistence is the writing of data from memory to disk, also known as "Landing", and Redis provides the following persistence methods:-RDB: Snapshot storage of data within a specified time interval;-AOF: The operation command is written to the log file after each write operation to the record;-Do not use persistence: The record only exists in memory when the server is running; 1. RDBRDB persistence saves data to a binary file named Dump.rdb within a specified time interval (or when a specified condition is met). Working mode:①redis Fork a sub-process;The ② child process writes the dataset to a temporary RDB file;③redis replaces the original Rdb file with the new Rdb file and deletes the old file. Advantages:-The Rdb file is compact and single and can be easily transferred to other backup centers for disaster recovery;-persistent work done by the child process to maximize the performance of Redis;-When recovering a larger data set, the RDB is restored directly, faster (more aof);Disadvantages:-If the persistence interval is large, it is easy to lose too much data;-If the persistence is too frequent, the continuous fork will severely degrade the performance of Redis;
Usage Advice : Save data for a period of time every day, save data for the past 24 hours per hour, and use it with AOF. 2, AOFaof each time a command to change the dataset is executed (with a different policy), the command is appended to the end of the AoF file, which is the command that saves the operation, recovering the file by re-executing the saved command. Working mode:①redis Fork A child process, the child process creates a new aof file;The ② child process writes the original AOF file contents to the new file;③redis writes the executed modification command to the new file;④redis Delete old files. Advantages:-a variety of different fsync strategies to minimize the amount of lost data;-The AoF file is simply an additional command, so space-saving and easy to read, easy to recover;-Redis can automatically override aof in the background when the aof file size becomes too large, and the rewritten aof file contains only the minimum set of commands to restore the current dataset;Disadvantages:-because the command is saved, the aof file is larger than the Rdb file;-Depending on the Fsync strategy used, the AOF speed may be slower than the RDB;
Usage Recommendation : Use a fsync per second policy. Recommendations for persistence:-If the data exists only when the server is running, then you do not have to choose persistence;-If you can tolerate data loss within minutes, you can use only the RDB persistence;-if data is required to be updated in real time, the aof persistence of fsync policy per second can be used;-The two persistence modes can be used in conjunction with the application scenario.
Summary: According to the user data to get the following conclusions, reference address: The difference between Redis and memcachedThe best use scenario for ①redis is all data in-memory;more scenes of ②redis are used as substitutes for memcached;③ Redis is more appropriate when more data type support is required;④ The use of Redis is more appropriate when the stored data cannot be excluded. (end of full text)
Redis Learning-Getting Started