redis brief introduction and installation summary

  • 2020-06-23 02:13:22
  • OfStack

1: Introduction of redis

Redis, like Memcached, is an ES7en-ES8en data store
Redis official website redis. io, the latest stable version 4.0.1
Support for more value types, in addition to and string, support for hash, lists (linked list), sets (collection), and sorted sets (ordered collection)
redis USES two file formats: full data (RDB) and incremental requests (aof). The full data format is to write the data in memory to disk for the next read file to load. An incremental request file serializes the data in memory into an action request, which is used to read the file for replay to get the data, similar to mysql binlog.
The storage of redis is divided into three parts: memory storage, disk storage and log file

2: redis installation

Download the latest stable edition


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

3: redis persistence

Redis provides two persistence methods, RDB (Redis DataBase) and AOF (Append Only File)
RDB, in short, generates snapshots of redis's data at different points in time and stores them on media such as disk.
AOF, on the other hand, has changed an Angle to implement persistence, that is, all write instructions executed by redis are recorded. When redis is restarted next time, as long as these write instructions are executed from front to back once again, data recovery can be achieved.
In fact, both RDB and AOF can be used at the same time. In this case, if redis is restarted, AOF will be preferred for data recovery, because the data recovery of AOF is more complete.
If you don't have a need for data persistence, you can turn off RDB and AOF, and redis will become a pure in-memory database, just like memcache1.

Redis persists related parameters
save 900 1 # means that persistence is triggered every 15 minutes with at least one key change
save 300 10 # means that persistence is triggered once every 5 minutes with at least 10 key changes
save 60 10000 # means that at least 10,000 key changes occur every 60 seconds, triggering a persistence
save "" # this disables rdb persistence
appendonly yes # Turns on aof persistence if it is yes
appendfilename "appendonly.aof" # specifies the name of the aof file
appendfsync everysec # specifies the fsync() invocation mode, with three no(no fsync),always(fsync with each write), and everysec(fsync once per second). The first is the fastest, the second is the safest data, but the performance will be 1 bit worse, the third is this scheme, default is the third.

4: redis data type

Redis data type -string

string is the simplest type, similar to Memcached1. One key corresponds to one value. The operation supported by string is similar to that of Memcached. Sets objects that can be stored in base 2.
Example:


#redis-cli
127.0.0.1:6379> set mykey "aminglinux.com"
OK
127.0.0.1:6379> get mykey
"aminglinux.com"
127.0.0.1:6379> mset key1 1 key2 a key3 c
127.0.0.1:6379> mget key1 key2 key3
1) "1"
2) "a"
3) "c"

Redis data type -list

list is a 1-list structure with the main functions of push, pop, getting all values of a range, and so on. key is the name of the linked list.
Using the list structure, we can easily achieve the latest news ranking and other functions (such as sina weibo's TimeLine). Another application of list is the message queue, which can use list's push operation to store the task in list, and then the worker thread can use the pop operation to take the task out for execution.
Example:


#redis-cli 
127.0.0.1:6379> LPUSH list1 "aminglinux"
127.0.0.1:6379> LPUSH list1 "1 2 3"
127.0.0.1:6379> LPUSH list1 "aaa bbb " 
127.0.0.1:6379> LRANGE list1 0 -1
1) "aaa bbb"
2) "1 2 3"
3) "aminglinux " 
127.0.0.1:6379> LPOP list1

Redis data type -set

set is a set, which is similar to the concept of set in our mathematics. There are operations such as adding or deleting elements to a set and finding the intersection and difference of multiple sets. key is understood as the name of the collection in the operation. For example, in a microblog application, all followers of a user can be stored in a set, and all followers can be stored in a set. Because Redis very personalized offers set intersection, and set, difference set, such as operation, can be very convenient to realize such as mutual concern and common be fond of, 2 degrees of friends, and other functions, all of the above set operations, you can also use a different command choose to return the results to the client or set into a new collection.
set sample


127.0.0.1:6379> SADD set1 a
127.0.0.1:6379> SADD set1 b
127.0.0.1:6379> SADD set1 c
127.0.0.1:6379> SADD set1 d
127.0.0.1:6379> SMEMBERS set1
1) "d"
2) "b"
3) "a"
4) "c"
127.0.0.1:6379> SREM set1 c// Remove elements 
127.0.0.1:6379> SADD set2 a 2 b
127.0.0.1:6379> SINTER set1 set2 // intersection 
127.0.0.1:6379> SUNION set1 set2 // And set 
127.0.0.1:6379> SDIFF set1 set2 // Difference set 

Redis data type -sort set

sorted set is an ordered set, it's more than set 1 score weighting parameters, make the elements in the collection by score ordered arrangement, such as a store of the class grade Sorted Sets, its collection value can be the student number of students, and score can is the exam score, so that when data is inserted into the collection, is the natural order.


127.0.0.1:6379> ZADD set3 12 abc
127.0.0.1:6379> ZADD set3 2 "cde 123"
127.0.0.1:6379> ZADD set3 24 "123-aaa"
127.0.0.1:6379> ZADD set3 4 "a123a"
127.0.0.1:6379> ZRANGE set3 0 -1
1) "cde 123"
2) "a123a"
3) "abc"
4) "123-aaa"

Reverse order

127.0.0.1:6379 > ZREVRANGE set3 0 -1
1) "123-aaa"
2) "abc"
3) "a123a"
4) "cde 123"

Redis data type -hash

In Memcached, we often package 1 structured piece of information into hashmap, which is serialized on the client side and stored as a string value (usually in JSON format), such as the user's nickname, age, gender, credits, etc.
The sample


127.0.0.1:6379> hset hash1 name aming
127.0.0.1:6379> hget hash1 name
"aming"
127.0.0.1:6379> hset hash1 age 30
127.0.0.1:6379> hget hash1 age
"30"
127.0.0.1:6379> hgetall hash1
1) "name"
2) "aming"
3) "age"
4) "30"


Related articles: