Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic data types of Redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the basic data types of Redis". In the daily operation, I believe that many people have doubts about the basic data types of Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the basic data types of Redis?" Next, please follow the editor to study!

Install Redis

Before learning, we have to install a Redis environment to do it ourselves and feel it.

Windows

Download address:

Https://github.com/MSOpenTech/redis/releases

Windows users can download the corresponding version of the compressed package at this address, decompress it on disk C, and rename the directory to redis after decompression. Enter the directory in cmd and run redis-server.exe redis.windows.conf. Alternatively, you can add the directory to the environment variable so that you no longer need to cd into the directory.

After the server for Redis is installed, open a new cmd and run redis-cli.exe-h 127.0.0.1-p 6379 to get started. The-h parameter indicates that the host,-p parameter represents port, which can be omitted. The default is 6379.

Linux

$wget http://download.redis.io/releases/redis-4.0.11.tar.gz

$tar xzf redis-4.0.11.tar.gz

$cd redis-4.0.11

$make

Execute the above command to download and install Redis, then go to the src directory and run redis-server. Re-execution

$. / redis-cli

You can start the operation.

Ubuntu

Ubuntu can be installed directly using apt-get

$sudo apt-get update

$sudo apt-get install redis-server

The startup method will not be repeated here.

Mac

Mac users can install Redis using homebrew

Brew install redis

Other methods

In addition to the above methods, we can download the source code from GitHub and compile the source code. URL is git@github.com:antirez/redis.git. You can also download Docker from the official website and operate it by running Docker.

Basic data type

Redis supports five basic data types, which we will introduce one by one. Since I am a Java programmer myself, I will compare these data types to those in Java. Of course, you can also understand it in a language you are familiar with.

String

String is the most basic and most commonly used type. It is binary safe, that is, we can serialize the object into a json string and store it in Redis as a value. When allocating memory, Redis allocates some redundant space for a string to avoid frequent memory allocation operations due to changes in the value of the string. When the string length is less than 1m, the existing space will be doubled for each expansion. When the length is greater than 1m, the maximum length of the 1m Magi Redis string will be 512m.

Hash

Hash is a collection of key-value pairs, which is equivalent to HashMap in Java. The actual structure, like HashMap, is the structure of array + linked list. The difference is that the expansion method is different. HashMap performs a rehash, while Redis creates a new array in order not to block the service. When querying, it will query both Hash, and then gradually transfer the old Hash content to the new one. A Hash can store up to 232-1 key-value pairs.

List

List is equivalent to LinkedList in Java. The time complexity of insert and delete operation is O (1), while the time complexity of query operation is O (n). We can use List's rpush, rpop, lpush, and lpop commands to build queues or stacks. The list can store up to 232-1 elements.

Set

Set is an unordered set of String type, and the element is unique, which is equivalent to HashSet in Java. The time complexity of its insert, delete and query operations is all O (1). The maximum number of elements is also 232-1.

Zset

Zset can be seen as a combination of SortedSet and HashMap in Java. On the one hand, it does not allow elements to repeat, on the other hand, it sorts each element through score.

Two rules

There are two general rules for the above five data structures:

If it does not exist, create it first and then do the operation

If the element is empty, memory is freed

Expiration time

We can set the expiration time for all of the above types, and if the time is up, Redis will automatically delete the corresponding objects.

At this point, the study on "what are the basic data types of Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report