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 data types in Redis

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

Share

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

What are the data types in Redis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

STRING

STRING is the most basic data type in redis, and the STRING type in redis is binary safe, that is, it can contain any data, such as a serialized object or even a jpg image. Note that the upper limit of string size in redis is 512m.

LIST

LIST is a simple list of strings sorted in the order of insertion. We can insert an element from the head (LEFT) or tail (RIGHT) of the LIST, or pop up an element from the head (LEFT) or tail (RIGHT) of the LIST.

HASH

HASH, similar to Map in Java, is a collection of key-value pairs that can be used to store objects in redis.

SET

SET is an unordered collection of type STRING, and unlike elements in LIST,SET, elements cannot be repeated.

ZSET

ZSET, like SET, is also a collection of elements of type STRING, except that each element in ZSET is associated with a score of type double, and the members of ZSET are unique, but the associated scores can be repeated.

OK, through the above introduction, I believe my friends have a general understanding of the five big data types. Next, let's take a look at how to operate these five data types.

Key related commands

Due to the differences in the data structure of the five big data types, the corresponding commands will also be different, but there are some commands that exist for any data type, so let's take a look at these special commands today.

First start redis with the redis-server redis.conf command, and then enter the console with the redis-cli command, as follows:

First, we can insert a record with the set command:

127.0.0.1 6379 > set K1 v1OKDEL command

Seeing OK indicates that the insertion is successful. With the DEL command, we can delete an existing key, as follows:

127.0.0.1 6379 > DEL K1 (integer) 1

Seeing (integer) 1 indicates that the data has been deleted successfully.

DUMP command

The DUMP command serializes a given key and returns the serialized value:

127.0.0.1 xe1bI 6379 > DUMP K1 "\ X00\ x02v1\ b\ X00\ xe6\ xc8\\ xe1bI\ xf3c" EXISTS command

The EXISTS command is used to detect the existence of a given key, as follows:

127.0.0.1 EXISTS 6379 > EXISTS K1 (integer) 1127.0.0.1 EXISTS 6379 > EXISTS K2 (integer) 0127.0.0.1

The above running results indicate that K1 exists and K2 does not exist.

TTL command

The TTL command can view the valid time of a given key:

127.0.0.1 TTL 6379 > TTL K1 (integer)-1127.0.1 TTL K2 (integer)-2

-2 indicates that key does not exist or has expired;-1 indicates that key exists and no expiration time is set (permanent). Of course, we can set an expiration time for key with the following command:

EXPIRE command

The EXPIRE command sets the validity period for the key, after which the key is destroyed.

127.0.0.1 EXPIRE 6379 > TTL K1 30 (integer) 1127.0.0.1 TTL K1 (integer) 25127.0.0.1

30 means 30 seconds, and a return of 25 from TTL K1 indicates that there are 25 seconds left in the validity period of this key.

PERSIST command

The PERSIST command indicates the expiration time of removing a key so that the key never expires:

127.0.0.1 EXPIRE 6379 > PERSIST K1 60 (integer) 1127.0.0.1 ttl 6379 > PERSIST K1 (integer) 57127.0.1 ttl 6379 > ttl K1 (integer) 1127.0.1 ttl 6379 > integer K1 (integer)-1PEXPIRE command

The function of the PEXPIRE command is basically the same as that of the EXPIRE command, except that the parameter set here is millisecond:

127.0.0.1 1PTTL 6379 > PEXPIRE K1 60000 (integer) command

The PTTL command is basically the same as the TTL command, except that PTTL returns milliseconds:

127.0.0.1 25421KEYS 6379 > PTTL K1 (integer) command

The KEYS command gets all the key that satisfies a given pattern, such as:

127.0.0.1 KEYS 6379 > K1) "K3" 2) "K2" 3) "K1"

KEYS * means to get all the KEY,* can also be a regular expression.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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