Introduction
Redis is similar to Memcached and also belongs to kv data storage
Redis official website redis.io, the latest stable version 4.0.1
Supports more value types, in addition to string, it also supports hash , Lists (linked list), sets (collection) and sorted sets (ordered collection)
redis uses two file formats: full data (RDB) and incremental request (aof). The full data format is to write the data in the memory to the disk, which is convenient for reading the file next time for loading. The incremental request file is to serialize the data in the memory into an operation request, which is used to read the file for replay to obtain the data, which is similar to mysql binlog.
The storage of redis is divided into three parts: memory storage, disk storage and log files
.
Installation
cd /usr/local/src/
wget http://download.redis.io /releases/redis-4.0.1.tar.gz
cd redis-4.0.1
make && make install
cp redis.conf /etc/redis.conf
vim /etc/redis. conf //Modify the following configuration
daemonize yes
logfile “/var/log/redis.log”
dir /data/redis_data/
appendonly yes
mkdir /data/redis_data
sysctl vm.overcommit_memory=1
echo never> /sys/kernel/mm/transparent_hugepage/enabled
redis-server /etc/redis.conf
.
Redis persistence
Redis provides Two persistence methods are RDB (Redis DataBase) and AOF (Append Only File)
RDB, in short, is to generate snapshots of the data stored in redis at different points in time and store them to disk, etc. On the medium.
Open the rdb, in the configuration file:
save [number of seconds] [number of changes in the previous number of seconds] Save
AOF, it is a different angle to achieve persistence, that is, redis has been executed All write instructions are recorded. When redis is restarted next time, as long as these write instructions are executed again from front to back, data recovery can be achieved.
Open aof, appendfsync [frequency]
In fact, RDB and AOF can also be used at the same time. In this case, if redis restarts, the AOF method will be preferred for data recovery. This is because The data recovery in AOF mode is more complete.
If you don’t need data persistence, you can also turn off RDB and AOF. In this case, redis will become a pure memory database, just like memcache.
save 900 1 #means that persistence is triggered every 15 minutes and at least 1 key changes?
save 300 10 #means that persistence is triggered every 5 minutes and at least 10 keys are changed< br>save 60 10000 # Means that at least 10000 keys change every 60 seconds, and a persistence is triggered
save “” #This can disable rdb persistence
appendonly yes #If yes, enable aof persistence
save “” br>appendfilename “appendonly.aof” # Specify the name of the aof file
appendfsync everysec #Specify the fsync() call mode, there are three no (do not call fsync), always (fsync is called every time you write), everysec (every second Call fsync once). The first is the fastest, and the second is the safest, but the performance will be worse. The third is this solution, and the default is the third.
. The redis data type stringstring is the simplest type. It is the same type as Memcached. A key corresponds to a value. The supported operations are similar to those of Memcached, and its functions are more abundant. Set up objects that can be stored in binary. Listlist is a linked list structure, the main function is to push, pop, get all the values of a range, and so on. In the operation, the key is understood as the name of the linked list. Using the list structure, we can easily implement the latest news ranking and other functions (such as TimeLine on Sina Weibo). Another application of the list is the message queue. You can use the push operation of the list to store the task in the list, and then the worker thread will use the pop operation to take the task out for execution. A setset is a set, which is similar to the concept of a set in our mathematics. The operations on a set include adding and deleting elements, and operations such as intersecting and subtracting multiple sets. In the operation, the key is understood as the name of the collection. For example, in a Weibo application, all the followers of a user can be stored in a collection, and all of its fans can be stored in a collection. Because Redis is very user-friendly and provides operations such as intersection, union, and difference for collections, it can be very convenient to implement functions such as common attention, common preferences, and second-degree friends. For all the above collection operations, you can also You can use different commands to choose whether to return the results to the client or save them to a new collection. Sort-set sorted set is an ordered set. It has one more weight parameter score than set, so that the elements in the set can be arranged in order according to score. For example, a Sorted Sets that stores the grades of the whole class, the set value can be the classmate’s Student ID, and score can be the test score, so that when the data is inserted into the collection, it has already been sorted naturally. Hash In Memcached, we often package some structured information into a hashmap, and store it as a string value (usually in JSON format) after serialization on the client side, such as the user’s nickname, age, gender, points, etc. .Redis common operations set key1 aminglinuxget key1set key1 aming//The second assignment will overwrite setnx key2 aaa //return 1 If key2 does not exist, create keysetnx key2 bbb //return 0, if key2 exists, return 0setex key3 10 1 // Set the expiration time for key3 to 10s and the value to 1. If the key already exists, the new value will be overwritten. mset k1 1 k2 a k3 cmget k1 k3 k2 lpush lista a //Add an element from the left lpush lista blrange lista 0 -1 lpop lista //Remove the first element from the left rpush lista 1 //Add an element from the right rpop lista //Remove the first element from the right linsert ?lista ?before ?2 3 ?//Insert before 2 One element is 3lset lista 4 bbb ?//Modify the fifth element to bbblindex lista 0 ?//View the first element lindex lista 3 ?//View the fourth element llen lista ?//View how many in the linked list Element sadd seta aaa ?//Put element smembers seta into the set seta? //View all elements in the set srem ?seta? ?Aaa //Delete element spop ?seta? ?//Remove an element at random and delete sdiff? seta ?setb? //Find the difference set, with seta as the standard sdiffstore setc seta setb? //Find the difference set and store it, and store it in setc sinter seta setb //Find the intersection sinterstore ?setd seta setb ?//Store the intersection setd sunion seta setb //Find the union sunionstore sete seta setb //Find the union and store it in setesismember seta aaa? //Determine whether an element belongs to a set srandmember ?seta //Remove an element randomly, but do not delete zadd zseta 11 123 //Create an ordered set zrange zseta 0 -1 //Display all elements, display in order zrange zseta 0 -1 withscores //You can bring up the score zrem zseta 222 //Delete the specified element zra nk zseta 222 //Returns the index value of the element. The index value starts from 0 and is sorted by score in the forward direction. zrevrank zseta 222 //Same as above, except that it is sorted in reverse order by score. zrevrange zseta 0 -1 Displays all elements in reverse order, and With score zcard zseta //Returns the number of all elements in the set zcount zseta 1 10 //Returns the number of elements with a score range of 1-10 zrangebyscore zseta 1 10 //Returns elements with a score range of 1-10 zremrangebyrank zseta 0 2 //Delete the elements in the index range 0-2, and sort by the positive score zremrangebyscore zseta 1 10 //Delete the elements in the score range 1-10 hset user1 ?name aming ?//create hashhset user1 age 30 hset user1 job ?ithgetall user1hmset user2 ?name aming age 30 ?job it?? ?//Check how many filedkeys there are in the hash * //Remove all keykeys my* //Fuzzy match exists name //Return 1 if there is a name key, otherwise return 0; del key1 // Delete a key //Return 1 if successful, otherwise return 0; EXPIRE key1 100 //Set key1 to expire after 100s ttl key // Check how long the key will expire, the unit is s, when the key does not exist, return -2. When the key exists but the remaining time to live is not set, -1 is returned. Otherwise, return the remaining lifetime of the key. select 0 //Represents the selection of the current database, the default is to enter 0. Database move age 1 // Move age to 1 Database persist key1 //Cancel the expiration time of key1 randomkey //Return a random keyrename oldname newname //Rename keytype key1 // Return key type dbsize? //Return the number of keys in the current database info??//Return redis database status information flushdb?//Empty all the keys in the current database fluxhall????//Empty all databases in all keybgsave //Save the data to the rdb file, run save in the background //Same as above, but run config get * in the foreground config get * //Get all configuration parameters config get dir //Get configuration parameters config set dir //Change configuration parameters data recovery : First define or determine the dir directory and dbfilename, then put the backed up rdb file under the dir directory, restart the redis service to restore the data. The redis security settings are set to monitor ipbind 127.0.0.1 2.2.2.2// can be multiple ips, Separate with a space to set the listening port port 16000 Set the password requirepass aming>comredis-cli -a’aming>com’ rename the config command rename-command CONFIG aming disable the config command rename-command CONFIG “”