Getting started with Redis

1. What is Redis

Redis (REmote DIctionary Server, remote data dictionary server) is an open source in-memory database that is often used as a cache or message queue.

2. Data structure in Redis

1. String

(1) Redis exists in memory and uses hard disk as persistence; 100,000 reads and writes per second.
(2) It has rich data structures, such as strings, hash tables, lists, sets, and ordered sets; it provides operations such as intersection, union, and difference.
(3) Set the TTL survival time, and it will be automatically deleted when it expires.
(4) Redis single-threaded, Memcached multi-threaded; for general application scenarios, single-threaded is also enough to use, the advantage is still in multiple data types and persistence.
(5) Data can be replicated to any number of slave servers.
2. Hash table

(1) HSET keyname key value Add a hash attribute and value
(2) HGET keyname key to get the value
(3) HKEYS keyname to get all the key values
​​(4) HVALS keyname to get all the value values
​​(5) HGETALL keyname Get all the values
​​3. List (implemented through a doubly linked list, the head and tail operations are O(1), and it is slow to obtain the specified element)

(1) LPUSH keyname value1 or LPUSH keyname value1 value2 Insert
(2) RPUSH keyname on the left value2 is inserted on the right
(3) LRANGE keyname 0 -1 to query all elements
(4) LPOP keyname
(5) RPOP keyname
Four. Set (elements are unique, but there is no order. Implemented using a hash table with an empty value, operations are O(1).)

Elements are unique, but there is no order. Implemented using a hash table with an empty value, the operations are all O(1).
(1) SADD keyname value1 value2 add value
(2) SREM keyname value1 delete
(3) SMEMBERS keyname query all elements
4. Ordered set (implemented using hash and jump table, the intermediate speed is also very fast.)

(1) ZADD keyname Add key value
(2) ZRANGE keyname start end Get elements in a specified range Comparison

with list: the
same:
(3) All are ordered
(4) All elements can be obtained in a certain range
Different :
(5) The list passes through a doubly linked list Realization, data access at both ends is extremely fast, and the middle is slow
(6) The ordered set is implemented by hash and jump table, and the middle speed is also very fast, which is O(log(N))
(7) The list cannot simply adjust a certain The position of the element, the ordered set can be
(8) the ordered set is more expensive than the list
3. Redis persistence Redis

provides two persistence methods: 1 RDB snapshot method 2 AOF method

RDB method:

When certain conditions are met, a child process will be created, the current data will be copied, the data will be written to a file on the hard disk, and the original storage file will be replaced after the writing is completed. Data is generally stored in dump.rdb. The UNIX system supports copy-on-write, that is, the operation of persistent writing to disk will be performed at the beginning. If other data changes at this time, a copy of the data will be performed.
In addition to this automatic snapshot method, command-based persistence is also supported:

SAVE: By blocking, the parent process is used to persist, and other requests cannot be executed at this time.
BGSAVE: Persistence by fork child process.
AOF method:

Commands are recorded for each operation, which will cause redundancy of some commands. For example, if an attribute is added and then deleted, both operations are redundant. Redis provides some optimizations, so this redundant information can be avoided. The command is recorded in appendonly.aof
4. Redis message queue

Redis is used for message queue, and there are usually two ways to use it:

LIST: List-based method, all consumer data add up to all the data in the list.





Publish/Subscribe : Each consumer subscribes to an independent channel, and each data is independent.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326191907&siteId=291194637