1 What is Redis
Redis (remote DIctionary server) is an open source memory database that is commonly used as a cache or message queue.
Features of Redis:
- Redis exists in memory, uses hard disk as persistence, and reads and writes 100,000 per second.
- With rich data structure, string, hash table, list, collection, ordered set, provide intersection, set, difference set and other operations.
- Set the TTL to survive and automatically delete the expiration time.
- Redis single-threaded, memcached multithreading, for general application scenarios, single-threaded is enough to use, the advantage is the multi-data type, persistence.
- Data can be copied to any number of slave servers.
2 data structure strings in Redis
- Set KeyName value set key value KeyName
- Keys * Gets all the key values
- Get KeyName Gets the value of KeyName
- EXISTS KeyName Determine if there is a key value KeyName
- DEL keyname Delete key value KeyName
- Type KeyName to determine the types of KeyName (string string, hash hash table, list, set set, Zset ordered set)
Hash table
- Hset KeyName Key value adds a hash attribute and value
- Hget keyname Key Get value
- Hkeys KeyName Get all the key values
- Hvals KeyName Get all the value values
- Hgetall KeyName get all the values
List
Through the two-way linked list implementation, the tail-to Operation is O (1), get the specified element is relatively slow.
- Lpush keyname value1 or Lpush keyname value1 value2 on the left insert
- Rpush keyname value2 Insert on right
- Lrange KeyName 0-1 querying all the elements
- Lpop KeyName
- Rpop KeyName
Collection
Elements are unique, but without order. Using a hash table with an empty value, the operation is O (1).
- Sadd keyname value1 value2 Add value
- Srem KeyName value1 Delete
- Smembers KeyName Querying all elements
Ordered collection
Using hashing and jumping tables, the intermediate speed is also very fast.
- Zadd KeyName Key Value Added
- Zrange keyname start end gets the element of the specified range
Comparison to the list:
Same:
- It's all orderly.
- Can get an element of a range
Different:
- The list is implemented by two-way linked lists, with very fast data access and slow intermediate
- The ordered set is implemented by hashing and jumping tables, and the intermediate speed is also fast, O (log (N))
- A list cannot simply adjust the position of an element, and an ordered collection can
- An ordered set is better than a list to charge memory
3 Redis Persistence
Redis provides two ways to persist: 1 Rdb Snapshot mode 2 aof mode
RDB mode:
When certain conditions are met, a child process is created, the current data is copied, the data is written to a file on the hard disk, and the original storage file is replaced after the write is completed. Data is typically stored in Dump.rdb. On UNIX systems, write-time replication is supported, that is, a persistent write to disk is started, and if other data changes at this time, a copy of the data is executed.
In addition to this automatic snapshot approach, command mode persistence is supported:
- SAVE: Persisted with the parent process in a blocking manner, and no other request could be executed at this time.
- BGSAVE: Persistent through the way of fork subprocess.
AoF Way:
Each operation logs a command, which causes redundancy in some commands, such as adding a property and then deleting it, both of which are redundant. Redis provides some optimizations, so you can avoid these redundant information. Commands are recorded in appendonly.aof.
4 Redis Message Queuing
Redis is used for Message Queuing, and there are usually two ways to use it:
List: All of the consumer data is added to the list, and all of the data in it is listed.
Publish/Subscribe: Each consumer subscribes to a separate channel and each data is independent.
Redis (20) a summary of Redis