First, Redis Introduction
1. Redis is a key-value storage system
2, the official site Http://redis.io
3, Redis and memcached similar, but support data persistence
4, Support more value type, in addition to string, also support hash, lists (linked list), sets (collection), and sorted sets (ordered collection) several data types
5. Redis uses two file formats: full-volume data (RDB) and incremental request (AOF). The full data format is to write the in-memory data to disk for the next read of the file to load. The incremental request file serializes the in-memory data into an operation request for reading the file for replay to get the data
6, Redis storage is divided into memory storage, disk storage and log files three parts
Second, Redis download installation configuration
# wget ' https://codeload.github.com/antirez/redis/tar.gz/2.8.21 '-o redis-2.8.21.tar.gz
# tar ZXVF redis-2.8.21.tar.gz
# CD redis-2.8.21
# make
# Make Prefix=/usr/local/redis Install
# MKDIR/USR/LOCAL/REDIS/ETC
# wget Http://www.apelearn.com/study_v2/.redis_conf-O/usr/local/redis/etc/redis.conf 2>/dev/null
# wget Http://www.apelearn.com/study_v2/.redis_init-O/etc/init.d/redis 2>/dev/null
# useradd-s/sbin/nologin redis && mkdir/usr/local/redis/var && chmod 777/usr/local/redis/var && ; chmod 755/etc/init.d/redis
# chkconfig--add Redis
# Chkconfig Redis on
# service Redis Start
Third, REDIS data structure
1. Redis Data Type-string
L string is the simplest type, you can understand the same type as memcached, a key corresponds to a value, and the operation supported on it is similar to the operation of memcached, his function is richer. Sets the object that can be stored in binary.
l Example
# REDIS-CLI Set MyKey "Fanjinbao"
# redis-cli Get MyKey "Fanjinbao"
"Fanjinbao"
You can also enter the Redis terminal to create
# REDIS-CLI
127.0.0.1:6379> mset key1 zhangsan key2 Lisi Key3 Hello
127.0.0.1:6379> mget key1 Key2 Key3
1) "Zhangsan"
2) "Lisi"
3) "Hello"
2. Redis Data Type-list
The list is a linked list structure, the main function is push, pop, get all the values of a range and so on. In operation, key is understood as the name of the list, and using the lists structure, we can easily achieve the latest message ranking and other functions (such as timeline Sina Weibo). Another application of list is the message queue, which can take advantage of the push operation of list, the task exists in list, and then the worker thread takes out the task with the pop operation to execute.
l Example
Left insert
# REDIS-CLI Lpush List AAA
Left value
# REDIS-CLI Lpop List
Right insert
# REDIS-CLI Rpush List Lisi
Right value
# REDIS-CLI Rpop List
View all list values
# REDIS-CLI Lrange List 0-1
3. Redis Data Type-set
L set is a set, similar to our set concept in mathematics, the operation of the set has the addition of delete elements, there are multiple sets of orthogonal poor operation. In Operation Key is understood as the name of the collection. For example, in a microblog application, you can have a collection of all the followers of a user, and a collection of all their fans. Because Redis is very human for the collection to provide the intersection, set, difference sets and other operations, it can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, on all of the above set of operations, you can also use a different command to choose whether to return the results to the client or to a new collection. QQ has a social function called "friend Tag", you can have a friend tag, such as "Big Beauty", "Local Tyrants", "Aubagne" and so on, then you can use the collection of Redis to achieve, each user's tags are stored in a collection.
l Example
Adding elements to the collection
# REDIS-CLI Sadd Set1 ABC
List elements in a collection (unordered display)
# REDIS-CLI Smembers Set1
4. Redis data type-sort set
L Sort set is an ordered set, it is more than set a weight parameter score, so that the elements in the set can be ordered by score, such as a storage class of the results of the sort set, its set value can be the student's school number, and score can be its test scores, When such data is inserted into the collection, it is already sorted in a natural way.
l Example
Adding elements to the collection
# REDIS-CLI Zadd Set2 ABC
# redis-cli Zadd Set2 2 "CDE 123"
# redis-cli Zadd Set2 123-aaa
# redis-cli Zadd Set2 4 a123a
List elements in a collection (ascending by weight parameter)
# redis-cli Zrange Set2 0-1
1) "CDE 123"
2) "a123a"
3) "ABC"
4) "123-aaa"
List elements in a collection (descending by weight parameter)
# redis-cli Zrevrange Set2 0-1
5. Redis Data Type-hash
In memcached, we often package structured information into HashMap, which is stored as a string value (usually in JSON format) after the client is serialized, such as the nickname of the user name, age, gender, integral, and so on.
l Example
Adding elements to the collection
# redis-cli Hset Hash name Fanjinbao
# redis-cli Hset Hash Age 31
Take an element from the collection
# redis-cli Hget Hash Name
# REDIS-CLI Hget Hash Age
List all elements
# REDIS-CLI Hgetall Hash
Iv. Redis Persistence
1, Redis provides two ways to persist, namely, RDB (Redis datab) and aof (Append only File)
2, RDB, in short, is a different point in time, the Redis stored data to generate snapshots and storage on the disk and other media.
3, AOF, is to change an angle to achieve persistence, that is, Redis executed all the written instructions recorded, at the next Redis restart, as long as these instructions from the front to the back of the execution again, you can achieve data recovery.
4, in fact, RDB and aof two ways can also be used at the same time, in this case, if the Redis restart, it will take precedence to the AoF method for data recovery, because the AOF mode of data recovery is more complete.
5, if you do not need data persistence, you can also completely close the RDB and aof mode, so that Redis becomes a pure memory database, like memcached
V. Redis configuration
View Redis configuration information
127.0.0.1:6379> Config Get *
Redis the basic configuration parameters:
Daemonize no #默认情况下, Redis is not run in daemon form. Daemonize Configuration Items Control the way Redis operates
Pidfile/path/to/redis.pid #当以daemon形式运行时, Redis generates a PID file that is generated by default in/var/run/redis.pid
Bind 192.168.1.201 192.168.1.202 #指定绑定的ip, can have multiple
Port 6379 #默认端口
Unixsocket/tmp/redis.sock #也可以监听socket
Unixsocketperm 755 #当监听soket时可以指定权限为755
Timeout 0 #当一个redis-client has not requested the discovery server side, then the server side has the right to actively shut down the connection, you can set the "Idle Timeout Time" by timeout, 0 means never shut down
Tcp-keepalive 0 #TCP连接保活策略, can be set by tcp-keepalive configuration items, in seconds, add 60 seconds to connect to the idle client to initiate an ACK request to check whether the client has been hung off, For unresponsive clients, the connection is closed, and if set to 0, keepalive detection is not performed.
LogLevel Notice #日志级别, there are four types of debug, verbose, Nbotice, warning
LogFile "" #定义日志路径
Syslog-ident Redis #如果希望日志打印到syslog中, controlled by syslog-enable, plus syslog-ident lets you specify log flags in the syslog.
Syslog-facility local0 #指定syslog的设备, can be user or local0-local7
Databases #设置数据库的总数量, select n Choose Database, 0-15
Redis Snapshot configuration (rdb persistence)
Save 1 #表示表示每15分钟且至少1个key改变 triggers a persistence
Save 10000 #表示每60秒至少有10000个key改变 trigger a persistence
Save #表示每5分钟且至少10个key改变, it's triggered.
Save "" #这样就可以禁用rdb持久化
Stop-writes-on-bgsave-error Yes #rdb持久化写入磁盘避免不了会出现失败的情况, if the default fails, Redis stops the write operation immediately, and if you feel it doesn't matter, you can use this option to turn off this feature.
Rdbcompression Yes #是否要压缩
Rdbchecksum Yes #是否进行数据校验
Dbfilename Dump.rdb #定义快照文件的名字
Dir./#定义快照文件存储路径
Redis security-related configuration
Requirepass Fansik #设置redis-server's password
If you set a password, sign in:
# redis-cli-a Fansik
Rename-command CONFIG fansik.config #将CONFIG命令更名为fansik. conf, so you can avoid misoperation, but if you use aof persistence, we recommend that you do not enable this feature
Rename-command config "" #也可以后面定义为空 so that you can disable the Config command
Redis Restricting related configurations
MaxClients 10000 #限制最大客户端连接数
MaxMemory <bytes> #设定最大内存使用数, Unit is byte
Maxmemmory-policy Volatile-lru #指定内存移除规则
Maxmemory-samples 3 #LRU算法和最小TTL算法都并非是精确的算法, but the valuation, so you can set the size of the sample, joining Redis by default will check three key and select the one of the LRU, then you can change the number of this key sample
Redis AOF persistence-related configuration
AppendOnly on #如果是yes, aof persistence is turned on
Appendfilename "Appendonly.aof" #指定aof文件名字, save the directory specified in the dir parameter
Appendfsync everysec #指定fsync () call pattern, there are three kinds of no (do not call Fsync), always (each write will call Fsync), everysec (call once per second Fsync), the first fastest, the second data is the safest, But the performance will be worse, the default is the third
No-appendfsync-on-rewrite No #使用no可避免当写入量非常大时的磁盘io阻塞
Auto-aof-rewrite-percentage #规定什么情况下会出发aof重写. Change to a scale, 10 means that the override mechanism is triggered when the AoF file increases by 10%
Auto-aof-rewrite-min-size 64MB #重写会有一个条件, is not less than 64MB
Redis slow Log-related configuration
For slow logs, you can set two parameters, one is the execution length, the unit is microseconds, the other is the length of the slow log. When a new command is written to the log, the oldest one is removed from the command queue
Slowlog-log-slower-than 10000 #慢于10000ms则记录日志
Slowlog-max-len #日志长度 (number of)
Redis Installation and Basic configuration