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

NoSQL----Redis 2.4--String

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

First, introduction: string: string type is the most basic data storage type in Redis, and it is binary safe in Redis, which means that this type can accept data in any format, such as JPEG image data or Json object description information. The maximum data length that a string type Value can hold in Redis is 512m.

2. Application scenarios:

Record the daily PV and UV of the website in real time

The number of people followed by each account on Sina Weibo in real time.

Third, common operations:

1. Set method:

Set key value

Set the value of key to value.

If key already exists, set overwrites the old value.

For example: redis > set color red

Redis > get color

"red"

Redis > set color black

Redis > get color

"black"

2. Setnx method:

Setnx key value

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

If key already exists, SETNX does not overwrite the old value, that is, it maintains the original value.

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

For example:

Redis > SETNX job "programmer"

(integer) 1 job is set successfully

Redis > SETNX job "code-farmer"

(integer) 0 job setting failed

Redis > GET job

"programmer" is not overwritten

3. Setex method:

SETEX key seconds value

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

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

Note: this command is similar to the following two commands:

SET key value

EXPIRE key 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.

4. Setrange method:

Setrange key offset value

Overrides the string value stored in the given key with the value parameter, starting with the subscript offset.

Redis > set email @163.com

Redis > get email

Haha@163.com

Redis > setrange email 5 google.com

Redis > get email

Haha@google.com

Redis > setrange email 5 sina.cn

Redis > get email

Haha@sina.cncom

5. Mset method:

MSET key value [key value...]

Set one or more key-value pairs simultaneously.

When a key with the same name is found, MSET will overwrite the old value with the new value. If you do not want to overwrite the key with the same name, use the MSETNX command.

MSET is an atomic operation in which all given key are set at the same time, and it is impossible for some given key to be updated while others to remain unchanged.

Redis > MSET date "November 11, 2015" time "9.09a.m." Weather "sunny" OK

6. Append method:

APPEND key value

If key already exists and is a string, the APPEND command appends value to 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.

# case 1: execute APPENDredis > EXISTS myphone # on the nonexistent key to ensure that the myphone does not exist (integer) 0redis > APPEND myphone "mi" # APPEND the nonexistent key, equivalent to SET myphone "mi" (integer) 5 # character length # case 2: APPENDredis > APPEND myphone "- 1110" (integer) 12 # length from 5 characters to 12 characters redis > GET myphone # View the entire string "mi-1110"

7. Get method:

Get key

Returns the string value associated with key.

Returns a special value of 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.

Redis > GET animate (nil) redis > SET animate "anohana" OKredis > GET animate "anohana"

8. Mget method:

MGET key [key...]

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

If a specified key does not exist, a special value nil is returned. Therefore, the command never fails.

9. Getrange method:

GETRANGE key start end

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.

GETRANGE handles range requests that are out of range by ensuring that the range of the substring does not exceed the range of the actual string.

Note: in SET greeting "hello, my friend" OKredis > GETRANGE greeting 0 4 # returns the characters of index 0-4, including 4. " Hello "redis > GETRANGE greeting-1-5 # does not support rewind operation. You must intercept"redis > GETRANGE greeting-3-1 # negative index" end "redis > GETRANGE greeting 0-1 # from the first to the last" hello, my friend "redis > GETRANGE greeting 0-1 # range does not exceed the actual string, the excess part is automatically abbreviated" hello, my friend "GETSET key value sets the value of the given key to value, and returns the old value of key. Returns an error when key exists but is not a string type. Redis > GETSET mail xxx@google.com # because mail does not exist before, there is no old value, return nil (nil) redis > GETSET mail xxx@yahoo.com # mail is updated, the old value is returned "xxx@google.com" strlen key returns the length of the string value stored by key. An error is returned when key does not store a string value. Returns 0 when key does not exist. Redis > SET mykey "Hello world" OKredis > STRLEN mykey (integer) 11redis > STRLEN nonexisting # non-existent key length is regarded as 0 (integer) 0

12. Incr method:

INCR key

Add + 1 to the numeric value stored in key.

If key does not exist, take 0 as the initial value of key, and then perform 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.

Redis > SET page_view 20OKredis > INCR page_view (integer) 21redis > GET page_view # numeric values save "21" as strings in Redis

13. Incrby method:

INCRBY key increment

Add the value stored by key to the incremental increment.

If key does not exist, take 0 as the initial value of key, and then execute 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 a 64-bit (bit) signed numeric representation.

For more information about increment / decrement operations, see the INCR command.

# case where 1:key exists and is numeric value redis > SET rank 5 setting rank to 50OKredis > INCRBY rank 20 # add 20 (integer) 70redis > GET rank "70" redis > INCRBY rank-20 # to rank 2:key does not exist redis > EXISTS counter (integer) 0redis > INCRBY counter 30 (integer) 30redis > GET counter "30" # case 3:key is not a numeric value redis > SET book "long long ago..." OKredis > INCRBY book 200 (error) the numeric value-1 stored in ERR value is not an integer or out of rangekey. If key does not exist, take 0 as the initial value of key, and then perform 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. The value of this operation is limited to a 64-bit (bit) signed numeric representation. For more information about increment / decrement operations, see the INCR command. Case 1: DECRredis > SET failure_times 10OKredis > DECR failure_times (integer) values for the existing numeric value key case 2: DECRredis > EXISTS count (integer) 0redis > DECR count (integer)-values for non-existent key values case 3: DECRredis > SET company YOUR_CODE_SUCKS.LLCOKredis > DECR company (error) ERR value is not an integer or out of range15, decrby method for key that exists but is not numeric:

DECRBY key decrement

Subtract the value stored in key from the subtractive decrement.

If key does not exist, take 0 as the initial value of key, and then perform 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.

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

For more information about increment / decrement operations, see the INCR command

# case 1: DECRBYredis > SET count 100OKredis > DECRBY count 20 (integer) 8 for existing key case 2: DECRBYredis > EXISTS pages (integer) 0redis > DECRBY pages 10 (integer)-10redis > DECRBY pages-20 (integer) 10 for non-existent key

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