Getting started with Redis-common operations on Technical Data Types
I. string type of Common Data Types
Set key = value, get key output value (the key value cannot be too long; the value must not be greater than 1 GB) ① use as a counter in business scenarios: set counter = 100; incr counter => 101; incr counter by 10 => 111; decr is the same; Note: incr converts a number of character types to a numeric type.
List type
Redis lists is implemented based on Linked Lists. This means that even if there are millions of elements in a list, the time complexity of adding an element to the header or tail is constant. Use the LPUSH command to add new elements to the list header of ten elements, which is the same as adding new elements to the list header of ten millions of elements. The reason why Redis Lists uses linked list for implementation is that, for the database system, the key feature is that it can quickly add elements to a large list. Another important factor is that, as you will see, Redis lists can get the constant length at the constant time. Lpush, rpush => rpush messages "hello ???" , Insert content before and after the list. Lrange: Obtain the content in the list. lrange 0 10 => obtain 10 records from 0. In business scenarios, the reference id of the object, rather than the content, is pushed into the list.
Set Type
Redis can store a series of non-repeated values into a set. Sadd things todo1; sadd things todo2; sadd things todo3; smembers things; elements in the output set, but there is no order. There are also other APIs for set operations. For details, see the command.
2. Publish/Subscribe subscription information pipeline
Subscribe todotask; pushlish todotask todeluser1; pushlish todotask todeluser2 ;... A blocking consumer model is implemented, which encapsulates the tasks to be processed to form tasks, subscribe to the corresponding tasks for task processing, and broadcast event models, such as the dismounting of products in the Product Center, send messages to the business system that follows off-shelf goods. Note: Multiple subscribe clients are supported.
Batch subscription in certain Mode
Psubscribe todo * for batch subscription
3. Data expiration settings
Ttl is used to obtain the expiration time of the corresponding key.-1 is permanent. Expire key second to set the expiration time.
Transaction
Redis controls transactions by controlling command batch execution. Multi starts transactions; discard cancels execution; exec executes;
Five management commands
Info => description of redis and other states. dbsize => persistent database capacity. flushdb => clear persistent data.
6. Data Persistence data Snapshot
Data snapshots traverse all data files in the memory from time to time and store them to an rdb data file on the hard disk. This operation is completed using the save command, which must be performed in redis. the conf file is configured as follows. Save 900 1 save 300 if one key changes within 10900 seconds, save if 10 keys change within seconds.