Location>code7788 >text

Getting Started with Redis - Learning the Five Basic Types and Their Directives

Popularity:359 ℃/2024-09-11 03:09:19

After the previous Redis introductory series of three articles to learn, I believe you are ready to learn new knowledge, to here is also considered to be the real beginning of learning Redis. Learning the software installation, client selection, then the next should also come to understand what Redis, can do.

As we said in Chapter 1, Redis supports a rich set of data types, and today we're going to learn about the five basic Redis types: string (String), set (Set), ordered set (Sorted Set), list (List), and hash (Hash).

01, String (String)

The string type in Redis is a binary safe data type. Strings can be understood as an array of characters that holds many characters of a specific encoding, so this design, all strings in Redis can store recognizable data types: integers, decimals, strings, images, serialized objects, binary data, and so on.

We briefly explain a few of the most common commands.

1. Set the value of the specified key, syntax:set key value

2. Get the value of the specified key, syntax:get key

3. Delete the specified key. syntax:del key

Of course there are many other commands for strings, so I won't list them all here, so you can try it yourself if you are interested.

02, Sets

A collection type in Redis can be understood as an ensemble that holds a set of unordered, unduplicated elements. You can add, delete, and check elements, as well as perform difference, intersection, and union operations.

We briefly explain a few of the most common commands.

1. Add one or more elements to the specified key collection, syntax:sadd key value1 value2…

2. Get all the elements in the specified key set, syntax:smembers key

3. Delete one or more elements in the specified key set, syntax:srem key value1 value2…

Of course there are many other commands in the collection, so I won't list them all here, so you can try it yourself if you are interested.

03Sorted Set

Redis ordered collection type can be understood as a collection type + ordered, that is, each element corresponds to a score, so the collection type has the function, ordered collection type basically also have, but also more on the score of aggregation, filtering, sorting and other functions.

We briefly explain a few of the most common commands.

1. Add one or more pairs of elements and their scores to the specified key ordered set, syntax:zadd key score1 value1 score2 value2…

2. Get the score of the specified element in the specified key ordered set, syntax:

3. Delete the specified element in the specified key ordered set, syntax:zrem key value

Of course there are many other commands for ordered sets, so I won't list them all here, so you can try it yourself if you are interested.

04List

The list type in Redis is a collection of strings arranged in a strict sequential order of insertion of the elements, and can be inserted and removed at either end of the collection, as well as found or removed by element value or index.

We briefly explain a few of the most common commands.

1. Insert one or more elements from the left into the specified key list, syntax:lpush key value1 value2 value3

2. Remove from the right and get the first element of the specified key list, syntax:rpop key

Of course there are many other commands in the list, so I won't list them all here, so you can try it yourself if you are interested.

05Hash

Redis hash type can be understood as a set of key-value pairs collection, the key represents a string field, the value represents the data object, and support for adding, acquiring or deleting a single item that is the key-value pairs, but also access to the entire hash collection and other functions.

We briefly explain a few of the most common commands.

1. Add one or more key-value pairs to the specified key hash. syntax:hset key field1 value1 field2 value2

2. Get the value corresponding to the specified key in the specified key hash, syntax:hget key filed

Of course there are many other commands for hashing, so I won't list them all here, if you are interested, you can try it yourself.

Of course Redis more than these five data types, there are other more advanced data types, we as entry-level tutorials, or first master the five basic types. Only mastered these basics, only Redis what can be done, what can be done, in order to skillfully use Redis on the project, in order to use Redis to solve a variety of complex problems.

The most important thing is to build a good foundation, so the instructions that are not listed in the article also need to be tried more and felt by yourself in order to better understand, remember and master them.

classifier for sums of money: The test method code as well as the sample source code have been uploaded to the code repository for those who are interested./hugogoos/Planner