One, Common command 1, String type
(1) Set key value nx: Create If key does not exist      XX: If key is present, modify its value (2) Get key: Value (3) mset key1 value1 key2 value2 set multiple values at once (4) mget key1 key2 : Get multiple values at once (5) Getrange key start stop: Gets the string [ Start, stop] The value of the range for the subscript of the string, the left number starts at 0, and the right number starts from 1 Note: When Start>length, returns an empty string when the stop >=length is truncated to the end of the string if start is positioned at stop to the right, an empty string (6) is returned getset Key nrevalue: Gets and returns the old value, set the new value (7) Incr key: Self-increment, returns the new value, If incr a value other than int returns an error, incr a nonexistent key, set key to 1 (8) incrby KEY 2: Jump 2 Self-increment (9) Strlen key: Takes the length of the value of the specified key setex key time value: sets the value corresponding to the key of value, and set the validity period to 1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728
2. List Type
(1) Lpush key value: Inserts the value into the list header (2) Rpush key value: Inserts the value into the end of the list (3) Lpop key: Returns and deletes the list header element (4) Rpop key: Returns and deletes the linked list trailing element (5) Lrange key Start stop: Returns the element in the list [Start, stop] (6) Lrem key count value: Remove value value from linked list, remove count of absolute values after count > 0 Delete from table header Count < 0 Remove count=0 from footer Delete all (7) LTrim key start stop: Cut key corresponding link, cut [Start, stop] section and re-assign the restructuring to key (8) lindex key index: Returns the value on the index (9) Llen key: Count the number of elements in the list Linsert key After|before search value: Search in the key list and before the search value | Then insert value (one) Rpoplpush source dest: Take out the end of the source and put it on the dest head, and returns the cell value 1234567891011121314151617181920212223242512345678910111213141516171819202122232425
3. Hash type
(1) Hset myhash field value: Set the field for Myhash as value (2) Hsetnx myhash field value: Setting Myhash field for Value (3) Hmset if not present Myhash field1 value1 field2 value2: Set multiple field (4) Hget myhash field: Gets the specified hash field (5) Hmget Myhash field1 Field2: Get more than one field at a time (6) Hincrby myhash field 5: Specify the hash field plus the given value (7) hexists myhash field: Test whether the specified field exists (8) Hlen Myhash: Number of field returned hash (9) Hdel myhash field: Delete the specified field (Ten) Hkeys Myhash: Return hash all field (one) hvals Myhash: Returns hash of all value (Hgetall) Myhash: Get all the field and value123456789101112131415161718192021222324123456789101112131415161718192021222324 in a hash
4. Set type
(1) Sadd key value1 value2: Add elements to the collection (2) smembers key: Gets all the elements of the collection (3) Srem key value: Delete the collection element (4) Spop Key: Returns and Deletes 1 random elements in the collection (can be drawn in the lottery, not repeatedly pumped to someone) (5) Srandmember key: Randomly take an element (6) Sismember key value: Determine if the collection has a value (7) SCard Key: Returns the number of collection elements (8) Smove source Dest Value: Moves the value of source to the Dest collection (9) sinter key1 Key2 Key3: Intersection of Key1 Key2 Key3 (Ten) Sunion Key1 Key2: Key1 key2 (one) Sdiff key1 Key2: The difference set of Key1 Key2 (a) sinterstore res key1 Key2: Find the intersection of Key1 Key2 and exist in res 1234567891011 12131415161718192021222324123456789101112131415161718192021222324
5. Zset Type
(1) Zadd key score1 value1: add Element (2) Zrange key start Stop [Withscore]: After sorting the set, the elements returning to the rank [start,stop] default to the ascending order WITHSC Ores is printing the score. (3) Zrank Key member: Ranking of query member (ascending 0) (4) zrangebyscore key min Max [Withscores] collection (ascending) sorted after score in [min , Max] elements within (5) Zrevrank Key Member: query member rank (0 start in descending order) (6) zremrangebyscore key min Max: delete element according to score, delete score at [min, Max] Between (7) Zrem key value1 value2: Delete the element in the collection (8) Zremrangebyrank key start end: Delete the element by rank, delete the rank between [Start, end] (9) Zcard Key: Returns the number of collection elements (Ten) zcount key min Max: return [min, Max] Number of elements in interval 1234567891011121314151617181920212212345678910111213141516171819202122
Second, advanced features 1, transactions
Redis implements transactional functionality through the MULTI, DISCARD, EXEC, and WATCH four commands, and support for transactions is now relatively straightforward. Redis can only guarantee that commands in one client-initiated transaction can be executed consecutively, but not in the middle of the other client's commands.
Since Redis is a single-threaded process for all client requests, it is easy to do so. In general, Redis will process and return processing results immediately after receiving a command from a client, but when a client issues a multi command in a connection, the connection goes into a transaction context, and the subsequent command is not executed immediately, but is first placed in a queue. When the EXEC command is received from this connection, Redis executes all the commands in the queue sequentially. The results of all commands are packaged together and returned to the client. The connection then ends the transaction context.
Example:
Redis 127.0.0.1:6379> multiokredis 127.0.0.1:6379> incr aqueuedredis 127.0.0.1:6379> incr BQUEUEDredis 127.0.0.1:6379> EXEC1. (integer) 12. (integer) 11234567891012345678910
2. Persistence
Redis is an in-memory database that supports persistence, which means that Redis often needs to synchronize in-memory data to disk to ensure persistence. Redis supports two persistence modes, one is snapshotting (snapshot) is the default, and the other is the way Append-only file (abbreviated AOF).
Snapshots are the default persistence mode. This is the way in which the in-memory data is written to the binary in a snapshot, the default file name is Dump.rdb. You can automatically make snapshot persistence by configuring settings. We can configure Redis to take snapshots automatically if more than M key is modified in n seconds, the following is the default snapshot save configuration:
Save 1 #900秒内如果超过1个key被修改, the snapshot is saved #300秒内容如超过10个key被修改, the snapshot is saved save 60 10000123123
AOF is more persistent than snapshot mode, because Redis appends each received write command to a file by using the Write function (default is appendonly.aof) when aof persistence is used.
When Redis restarts, the contents of the entire database are rebuilt in memory by re-executing the write commands saved in the file. Of course, because the OS caches write modifications in the kernel, it may not be written to disk immediately. The persistence of this aof method is also likely to lose some of the modifications. But we can tell Redis through the configuration file that we want to force the OS to write to disk through the Fsync function.
There are three ways of doing this:
appendonly Yes #启用aof持久化方式 # Appendfsync always #每次收到写命令就立即强制写入磁盘, the slowest, but guaranteed full persistence, is not recommended to use Appendfsync everysec #每秒钟强制写入磁盘一次, a good compromise in performance and persistence, recommended # Appendfsync No #完全依赖os, best performance, no guarantee 1234512345
3, master-slave replication
Redis supports master-slave replication, and the configuration is relatively straightforward. Master-slave replication enables the backup and redundancy of data to build highly available systems. When Master does not work properly, you can switch slave to master.
Principle Process
After the slave is started and connected to master, it will actively send a sync command. Master will then start the background disk process and collect all the received commands to modify the dataset, and master will transfer the entire database file to slave to complete a full synchronization once the background process has finished executing. The slave server then disks and loads the database file data into memory after it receives it. After that, Master continues to pass all the modified commands that have been collected, and the new modification commands to Slaves,slave will execute these data modification commands at this time to achieve final data synchronization.
650) this.width=650; "title=" "alt=" here write a picture description "src=" http://img.blog.csdn.net/20160427120126094 "/>
4, publish the subscription
A publish Subscription (PUB/SUB) is a message communication pattern that is primarily designed to decouple the coupling between a message publisher and a message subscriber, which is similar to the observer pattern in design patterns.
Pub/sub not only solves the direct code level coupling between publishers and subscribers, but also solves the coupling between the two on the physical deployment.
As a pub/sub server, Redis serves as a message routing feature between subscribers and publishers. Subscribers can subscribe to Redis server with the subscribe and Psubscribe commands for the type of message they are interested in, and Redis calls the message type channel. When a publisher sends a specific type of message to Redis server through the Publish command. All clients subscribing to this message type receive this message. The delivery of the message here is many-to-many. A client can subscribe to multiple channel, or it can send messages to multiple channel.
Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:
650) this.width=650; "title=" "alt=" here write a picture description "src=" http://img.blog.csdn.net/20160427122512654 "/>
When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:
650) this.width=650; "title=" "alt=" here write a picture description "src=" http://img.blog.csdn.net/20160427122523310 "/>
Summarize:
From the introduction of Redis to simple operation commands, and then understand its transactions, persistence, master-slave replication and other features, I hope in the future use of the project will be handy, as for the other features of Redis, such as virtual memory, we can be a little bit deeper in the future. And in the actual application process will also appear a variety of problems, then one by one to solve.
This article from "Jiaxing Hardware network www.jxwujin.cn" blog, declined reprint!
NoSQL's in-depth redis