by Devin Yang
(This article was automatically translated.)

Published - 2 years ago ( Updated - 2 years ago )

Redis is an advanced NoSQL key-value data storage, also known as a data structure server.
Because it has powerful data types, such as Strings, Hashes, Lists, Sets, Stored Sets, Bitmas and HyperLogLogs.
By default, Redis stores all data in memory, so the read and write speed very fast.
He can also save the data permanently on the disk, such as snapshotting and journaling as we know it.
 

Command connection mode (here -h means host, redis after -h is the host name)

redis-cli -h redis

Then enter the password

auth password
< h3>Universal data type

String
Strings are the most general data type in Redis because they have many commands and multiple uses. Depending on its value and the command used, a string can represent an integer, floating point number, text string, or bitmap. It can store any type of data: text (XML, JSON, HTML or raw text), integers, floating point numbers or binary data (video, image or audio files). String values ​​cannot exceed 512 MB of text or binary data.

Cache mechanisms: Text or binary data can be cached in Redis, which can be anything from HTML pages and API responses to images and videos.
You can use the commands SET, GET, MSET and MGET to implement a simple caching system.

Cache with automatic expriation: strings combined with automatic key expiration
You can use the SETEX, EXPIRE, and EXPIREAT commands to build a robust caching system. This is useful when database queries take a long time to run,
and can be cached for a given period of time. Therefore, this avoids running these queries too frequently and can improve the performance of the application.

Counting: A counter can be easily implemented using strings and the commands INCR and INCRBY. Good examples of counters are page views, video views, and likes.
String also provides other counting commands such as DECR, DECRBY, and INCRFLOATBY.

Lists

Command BRPOP removes the last element of a Redis list. If the List is empty, it waits until there is something to delete. BRPOP is a blocking version of RPOP. However, RPOP is not ideal. If the List is empty, we need to implement some kind of polling ourselves to ensure that items are processed as soon as they are added to the queue. It's better to take advantage of BRPOP and not worry about empty lists.

Hash
Hash is a great data structure for storing objects because you can map strings to values. They are optimized to use memory efficiently and to query data very quickly.
In Hash, both string names and values ​​are strings. So Hash is a mapping of String to String.

The HGETALL command may have problems if the Hash has many strings and uses a lot of memory. It has the potential to slow down Redis since it needs to transfer all data over the wire.
A good alternative in this case is HSCAN. HSCAN does not return all strings at once. It returns a cursor and hash string and its value in the block.
You need to execute HSCAN until the returned cursor is 0 to retrieve all the strings in the Hash.


Advanced Data Type

Sets

Set in Redis is a different character An unordered collection of strings - it is not possible to add duplicate elements to a Set. Internally, Set is implemented as a hash table, which is why certain operations are optimized: member addition, removal, and lookup run in O(1), constant time.

A Sorted Set is very similar to a Set, but each element of a Sorted Set has an associated score. In other words, a Sorted Set is a collection of distinct strings sorted by score. There may be elements with duplicate scores. In this case, repeated elements are sorted lexicographically (alphabetically). Use zadd to add a sorted set.

We've talked a lot about using Redis to store data in memory. Memory is short.
Therefore, if the Redis instance shuts down, crashes, or needs to be restarted, all stored data will be lost.
In order to solve this problem, Redis provides two mechanisms for handling persistence: Redis database (RDB) and append-only file (AOF).

These two mechanisms can be used individually or concurrently within the same Redis instance.
RDB is great for backup and disaster recovery as it allows you to save an RDB file hourly, daily, weekly or monthly as needed. This approach allows you to easily restore any dataset at any given time using RDB files.
 The SAVE command immediately creates an RDB, but should be avoided as it blocks the Redis server during snapshot creation. The command BGSAVE (Background Save) should be used;
It has the same effect as SAVE, but it runs in a child process so as not to block Redis.

The above are not all Redis functions, other such as redis publish and subscribe, please Google it yourself.
 

Tags: redis

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments