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

Detailed introduction of the redis data type strings

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "detailed introduction of redis data type strings". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of the redis data type strings.

1. String (string)

String is the simplest type, you can understand it as the same type as Memcached, one key corresponds to one value, and the operations supported on it are similar to those of Memcached. But it has more functions.

Redis encapsulates the string with the structure sdshdr and sds, and the string-related operations are implemented in the source file sds.h/sds.c. The maximum data length that a string type Value can hold in Redis is 512m.

Binary security in Redis, which means that the type can accept data in any format, such as JPEG image data or Json object description information.

The data structure is defined as follows:

Typedefchar*sds

Structsdshdr {

Longlen

Longfree

Charbuf []

}

1.1. SET

Set key value [EX seconds] [PXmilliseconds] [NX | XX]

Associate the string value value to key. If key already holds another value, SET overrides the old value, regardless of the type. For a key that originally has a time to live (TTL), the original TTL of the key will be cleared when the SET command is successfully executed on the key.

Optional parameter

Starting with Redis version 2.6.12, the behavior of the SET command can be modified with a series of parameters:

EXsecond: sets the expiration time of the key to second seconds. The SET key value EX second effect is equivalent to SETEXkey second value.

PXmillisecond: sets the expiration time of the key to millisecond milliseconds. The SET key value PX millisecond effect is equivalent to PSETEXkey millisecond value.

NX: set the key only when the key does not exist. The SETkey value NX effect is equivalent to SETNX key value.

XX: set the key only if it already exists.

# assignment and selection

127.0.0.1 purl 6379 > set dbredis

OK

127.0.0.1 purl 6379 > get db

"redis"

# use the ex option

127.0.0.1 6379 > set dbredis ex 20

OK

127.0.0.1 purl 6379 > ttl db

(integer) 16

127.0.0.1 purl 6379 > get db

"redis"

127.0.0.1 purl 6379 > ttl db

(integer) 5

127.0.0.1 purl 6379 > get db

"redis"

127.0.0.1 purl 6379 > get db

"redis"

127.0.0.1 purl 6379 > get db

(nil)

# use the PX option

127.0.0.1 6379 > set db redis px 20000

OK

127.0.0.1 purl 6379 > PTTL db

(integer) 15674

127.0.0.1 purl 6379 > PTTL db

(integer) 8974

127.0.0.1 purl 6379 > PTTL db

(integer) 8045

127.0.0.1 purl 6379 > get db

"redis"

127.0.0.1 purl 6379 > PTTL db

(integer) 2482

127.0.0.1 purl 6379 > get db

"redis"

127.0.0.1 purl 6379 > get db

(nil)

# use the NX option

127.0.0.1 purl 6379 > set db oracle NX

OK

127.0.0.1 purl 6379 > get db

"oracle"

127.0.0.1 6379 > set db redis NX-key exists, failed

(nil)

127.0.0.1 purl 6379 > get db

"oracle"

# use the XX option

127.0.0.1 purl 6379 > exists name

(integer) 0

127.0.0.1 purl 6379 > get name

(nil)

127.0.0.1 6379 > set name hunt1574 XX-key does not exist, failed

(nil)

127.0.0.1 purl 6379 > set name redis

OK

127.0.0.1 purl 6379 > set name hunt1574 XX

OK

127.0.0.1 purl 6379 > get name

"hunt1574"

1.2. SETEX

Setex key seconds value

Associate the value value to key and set the lifetime of key to seconds (in seconds).

If key already exists, the SETEX command overrides the old value.

This command is similar to the following two commands:

SETkey value

EXPIREkey seconds # set time to Live

The difference is that SETEX is an atomic operation, and the two actions of associating values and setting time to live are done at the same time, which is very useful when Redis is used as a cache.

# key does not exist assignment

127.0.0.1 purl 6379 > EXISTS user

(integer) 0

127.0.0.1 setex user 6379 > 30 root

OK

127.0.0.1 purl 6379 > get user

"root"

127.0.0.1 purl 6379 > ttl user

(integer) 15

127.0.0.1 purl 6379 > get user

(nil)

# key exists assignment

127.0.0.1 6379 > setex user 20 admin

OK

127.0.0.1 purl 6379 > get user

"admin"

127.0.0.1 purl 6379 > ttl user

(integer) 14

127.0.0.1 purl 6379 > get user

(nil)

1.3. SETNX

SETNX key value

Set the value of key to value if and only if key does not exist.

If a given key already exists, the SETNX does not take any action.

SETNX is an abbreviation for "SET if Not eXists" (or SET if it does not exist).

# assignment and selection

127.0.0.1 purl 6379 > EXISTS www

(integer) 0

127.0.0.1 purl 6379 > setnx www code.google.com

(integer) 1

127.0.0.1 purl 6379 > setnx www redis.io

(integer) 0

127.0.0.1 purl 6379 > get www

"code.google.com"

127.0.0.1purl 6379 >

1.4. SETRANGE

SETRANGE key offset value

Overwrite (overwrite) the string value stored by the key of a given offset with the value parameter

127.0.0.1 purl 6379 > set www code.google.com

OK

127.0.0.1 purl 6379 > get www

"code.google.com"

127.0.0.1 6379 > SETRANGE www 0 mail

(integer) 15

127.0.0.1 purl 6379 > get www

"mail.google.com"

127.0.0.1 6379 > SETRANGE www 0 gmail

(integer) 15

127.0.0.1 purl 6379 > get www

"gmailgoogle.com"

1.5. MSET

MSET key value [key value...]

Set one or more key-value pairs simultaneously.

If a given key already exists, MSET will overwrite the old value with the new value, and if this is not what you want, consider using the MSETNX command: it will only set it if none of the given key exists.

MSET is an atomic operation where all given key are set at the same time. It is impossible for some given key to be updated and some given key to remain unchanged.

# assignment and selection

127.0.1 db 6379 > mset os "linux" db "redis" date "2015-03-17"

OK

127.0.0.1 purl 6379 > mget os db date

1) "linux"

2) "redis"

3) "2015-03-17"

# value override

127.0.1 db 6379 > mset os "linux" db "oracle" date "2015-03-17"

OK

127.0.0.1 purl 6379 > mget os db date

1) "linux"

2) "oracle"

3) "2015-03-17"

127.0.0.1purl 6379 >

1.6. MSETNX

MSETNX key value [key value...]

Set one or more key-value pairs at the same time if and only if none of the given key exists.

Even if only one given key already exists, MSETNX refuses to perform setup operations for all given key. MSETNX is atomic, so it can be used as a unique logical object (unique logic object) to set multiple different key to represent different fields (field), all of which are either set or not set.

# assignment and selection

127.0.0.1 purl 6379 > mget os db date

1) "linux"

2) "redis"

3) "2015-03-17"

127.0.1 db 6379 > msetnx os "linux" db "oracle" date "2015-03-17"

(integer) 0

127.0.0.1 purl 6379 > mget os db date

1) "linux"

2) "redis"

3) "2015-03-17"

127.0.0.1purl 6379 >

1.7. GET

GET key

Returns the string value associated with key.

Returns the special value nil if key does not exist.

If the value stored by key is not a string type, an error is returned because GET can only be used to handle string values.

127.0.0.1 purl 6379 > set dbname redis

OK

127.0.0.1 purl 6379 > get dbname

"redis"

127.0.0.1 purl 6379 > get dbversion

(nil)

127.0.0.1 purl 6379 > lpush db redis mysql mongodb

(integer) 3

127.0.0.1 purl 6379 > get db

(error) WRONGTYPE Operation against a key holding the wrong kind of value

1.8. MGET

MGET key [key...]

Returns all (one or more) values of a given key.

If a key does not exist in a given key, the key returns a special value, nil. Therefore, the command never fails.

# assignment and selection

127.0.0.1 6379 > set date 2015-03-17

OK

127.0.0.1 6379 > set time 10:00

OK

127.0.0.1 purl 6379 > mget date time

1) "2015-03-17"

2) "10:00"

127.0.0.1 nil 6379 > mget date time week-week does not exist. Return nil

1) "2015-03-17"

2) "10:00"

3) (nil)

1.9. GETRANGE

GETRANGE key startend

Returns a substring of the string value in key. The interception range of the string is determined by the offsets of start and end (including start and end). The negative offset indicates that the count starts at the end of the string,-1 represents the last character,-2 represents the penultimate character, and so on. The range exceeds the maximum subscript value of the string and the maximum subscript value shall prevail.

# assignment and selection

127.0.0.1 purl 6379 > set www redis.io

OK

127.0.0.1 GETRANGE www 6379 > 0 4

"redis"

127.0.0.1 6379 > GETRANGE www-2-1

"io"

127.0.0.1 GETRANGE www 6379 > 100-1

"redis.io"

1.10. GETSET

GETSET key value

Sets the value of the given key to value and returns the old value of key (old value).

Returns an error when key exists but is not a string type.

127.0.0.1 purl 6379 > getset dbname mysql

(nil)

127.0.0.1 purl 6379 > get dbname

"mysql"

127.0.0.1 purl 6379 > getset dbname redis

"mysql"

127.0.0.1 purl 6379 > get dbname

"redis"

1.11. INCR

INCR key

Increments the numerical value stored in key by one.

If key does not exist, the value of key is initialized to 0 before performing the INCR operation. If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

The value of this operation is limited to a 64-bit (bit) signed numeric representation.

127.0.0.1 purl 6379 > EXISTS pages

(integer) 0

127.0.0.1 purl 6379 > incr pages

(integer) 1

127.0.0.1 purl 6379 > get pages

"1"

127.0.0.1 set rows 6379 > 0

OK

127.0.0.1 purl 6379 > incr rows

(integer) 1

127.0.0.1 purl 6379 > incr rows

(integer) 2

127.0.0.1 purl 6379 > incr rows

(integer) 3

127.0.0.1 purl 6379 > get rows

"3"

127.0.0.1purl 6379 >

1.12. INCRBY

INCRBY keyincrement

Add the value stored by key to the incremental increment.

If key does not exist, the value of key is initialized to 0 before executing the INCRBY command.

If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

The value of this operation is limited to the 64-bit (bit) signed number representation

# key exists

127.0.0.1 purl 6379 > get rows

"3"

127.0.0.1 INCRBY rows 6379 >

(integer) 6

127.0.0.1 INCRBY rows 6379 >

(integer) 9

127.0.0.1 INCRBY rows 6379 >

(integer) 12

# key does not exist

127.0.0.1 purl 6379 > get num

(nil)

127.0.0.1 6379 > INCRBY num-2

(integer)-2

127.0.0.1 6379 > INCRBY num-2

(integer)-4

127.0.0.1 6379 > INCRBY num-2

(integer)-6

1.13. INCRBYFLOAT

INCRBYFLOAT keyincrement

Adds a floating-point increment increment to the value stored in key.

If key does not exist, INCRBYFLOAT sets the value of key to 0 before performing the addition operation.

If the command executes successfully, the value of key is updated to the new value (after the addition is performed), and the new value is returned to the caller as a string.

Either the value of key or the incremental increment can be represented by exponential symbols such as 2.0e7, 3e5, and 90e-2, but the values after the INCRBYFLOAT command are always stored in the same form, that is, they always consist of a number, an (optional) decimal point, and a decimal part of any place (e.g. 3.14,69.768, and so on). The trailing 0 of the decimal part is removed and, if necessary, the floating point number is changed to an integer (for example, 3.0 is saved as 3).

127.0.0.1 6379 > set price 45.99

OK

127.0.0.1 INCRBYFLOAT price 6379 >

"50.49"

127.0.0.1 INCRBYFLOAT price 6379 >

"54.99"

127.0.0.1 purl 6379 > get num

(nil)

127.0.0.1 6379 > INCRBYFLOAT num 5.3

"5.3"

127.0.0.1 6379 > INCRBYFLOAT num 5.3

"10.6"

1.14. DECR

DECR key

Subtracts the numeric value stored in key by one.

If key does not exist, the value of key is initialized to 0 before performing the DECR operation.

If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

127.0.0.1 6379 > set price 398.99

OK

127.0.0.1 purl 6379 > decr price

(error) ERR value is not an integer or out of range

127.0.0.1 set pages 6379 >

OK

127.0.0.1 purl 6379 > decr pages

(integer) 97

127.0.0.1 purl 6379 > decr pages

(integer) 96

1.15. DECRBY

DECRBY keydecrement

Subtract the value stored in key from the subtractive decrement.

If key does not exist, the value of key is initialized to 0 before performing the DECRBY operation.

If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

127.0.0.1 purl 6379 > get pages

"96"

127.0.0.1 6379 > decrby pages 5

(integer) 91

127.0.0.1 6379 > decrby pages 5

(integer) 86

127.0.0.1 6379 > decrby pages-5

(integer) 91

127.0.0.1 6379 > decrby pages-5

(integer) 96

127.0.0.1purl 6379 >

1.16. APPEND

APPEND key value

If key already exists and is a string, the APPEND command appends value to the end of the original value of key.

If key does not exist, APPEND simply sets the given key to value, just as it does SET key value.

127.0.0.1 purl 6379 > get db

(nil)

127.0.0.1 purl 6379 > APPENDdb redis

(integer) 5

127.0.0.1 6379 > APPENDdb .io

(integer) 8

127.0.0.1 purl 6379 > get db

"redis.io"

127.0.0.1purl 6379 >

1.17. STRLEN

STRLEN key

Returns the length of the string value stored by key.

An error is returned when key does not store a string value.

127.0.0.1 purl 6379 > getpages

"96"

127.0.0.1 purl 6379 > STRLENpages

(integer) 2

127.0.0.1 purl 6379 > get db

"redis.io"

127.0.0.1 purl 6379 > STRLENdb

(integer) 8

127.0.0.1purl 6379 >

At this point, I believe you have a deeper understanding of the "detailed introduction of the redis data type strings". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Database

Wechat

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

12
Report