Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use strings in Redis

Shulou Source: shulou.com Published: 2022-06-01 11:01:09 09月12日 Update

This article mainly shows you "how to use strings in Redis", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use strings in Redis" this article.

Redis is stored in the data structure of key value, each key is unique, the string is the simplest data structure in redis, its interior is actually an array of characters.

The string in redis is a dynamic character array, similar to arrayList in Java. Pre-allocation redundancy is used to reduce frequent memory allocation. The actual space capacity allocated for the current string is generally higher than the actual string length len. When the string length is less than 1MB, the expansion doubles the existing space. If the length of the string exceeds 1MB, only the space of 1MB will be expanded at one time, and the maximum length of the string is 512MB.

Let's try some common commands.

> set name mangoOK > get name mango "mango" > exists name (intgeter) > del name (intgeter) > get name (nil)

Batch processing

> mset name1 mango name2 loveOK > mget name1 name21) "mango" 2) "love"

Postpone

> set name mangoOK > get name "mango" > expire name 5.5s later > get name (nil)

> setex name 5 mango # 5s expires, which is equivalent to set+exp worker re > get name "mango"... 5s later > get name (nil)

> setnx name mango # if name does not exist, execute set (integer) 1 > get name "mango" > setnx name love (integer) 0 # because name already exists, so set creation is not successful # if name does not exist, execute set creation > get name "mango" # the value has not changed

Count

If value is an integer, you can perform a self-incrementing operation, and if the self-incrementing range signed long is between the maximum and minimum values, Redis will report an error if it exceeds this range.

> set age 18OK > incr age (integer) 19 > incrby age 2 (integer) 21 > incrby age-2 (integer) 19 > set codemax 9223372036854775807OK > incr codemax (error) ERR increment or decrement would overflow are all the contents of this article "how to use strings in Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Character string length content space article assignment maximum actual data data structure array structure range learning help minimum frequency success between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL NVidia Linux Docker