Home >Database >Redis >Analysis of String data type examples in Redis

Analysis of String data type examples in Redis

WBOY
WBOYforward
2023-06-03 10:47:49896browse

Overview:

The string type is the most basic data storage type in Redis. 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, etc. The maximum capacity of string type values ​​in Redis is 512MB.

Related command list:

O(1) If the Key already exists, the APPEND command appends the data of the parameter Value to the end of the existing Value. If the Key does not exist, the APPEND command will create a new Key/Value. The length of Value after appending. O(1) Decreasing Value. O(1) The incremented Value value. O(1)The reduced Value value. O(1) The increased Value value. O(1)Get the Value of the specified Key. If the Value associated with the Key is not of string type, Redis will return an error message because the GET command can only be used to obtain string Value. Value related to the Key. If the Key does not exist, nil is returned. O(1)Set the Key to hold the specified string Value. If the Key If it already exists, its original value will be overwritten. Always returns "OK". O(1)Atomicly set the Key to the specified Value and return the Key at the same time the original value. Like the GET command, this command can only process string Value, otherwise Redis will give relevant error information. Return the original value of the Key. If the Key does not exist before, return nil. O(1)Returns the character value length of the specified Key. If Value is not of string type, Redis The execution will fail and relevant error information will be given. Returns the Value character length of the specified Key. If the Key does not exist, returns 0. O(1) Complete two operations atomically. One is to set the value of the Key to Specify a string and set the survival time (seconds) of the Key in the Redis server. This command is mainly used when Redis is used as a Cache server. O(1)If the specified Key does not exist, set The Key is determined to hold the specified string Value. At this time, its effect is equivalent to the SET command. On the contrary, if the Key already exists, the command will do nothing and return. 1 means the setting is successful, otherwise 0. O(1)SETBIT Set the BIT value on the specified Offset. This value can only be 1 or 0. After setting, this command returns the original BIT value on the Offset. If the specified Key does not exist, this command will create a new value and set the BIT value in the parameter at the specified GETBITMGETMSETMSETNXN represents the number of specified Keys. This command atomically completes all key/value setting operations in the parameters. Its specific behavior can be seen as executing the SETNX command iteratively multiple times. However, what needs to be clearly stated here is that String (string)
Command prototype Time complexity Command description Return value
##APPEND
DECR Decrement the Value of the specified Key atomically by 1. If the Key does not exist, its initial value is 0 and its value after decr is -1. If the value of Value is not of integer type, such as Hello, the operation will fail

and return the corresponding error message. Note: The value range of this operation is a 64-bit signed integer.

INCR Atomicly increment the Value of the specified Key by 1. If the Key does not exist, its initial value is 0 and its value after incr is 1. If the value of Value cannot be converted to an integer value, such as the string Hello, then the operation will fail

and return the corresponding error message. Note: The value range of this operation is a 64-bit signed integer.

DECRBY The Value of the specified Key will be atomically reduced by decrement. After decrby is executed, if the Key does not exist, its initial value is 0, and then its value becomes -decrement. If the value of Value cannot be converted to an integer value,

such as Hello, the operation will fail and the corresponding error message will be returned. Note: The value range of this operation is a 64-bit signed integer.

INCRBY Increments the Value of the specified Key atomically. If the Key does not exist, its initial value is 0, and its value after incrby is increment. If the value of Value cannot be converted to an integer value, such as Hello,

the operation will fail and the corresponding error message will be returned. Note: The value range of this operation is a 64-bit signed integer.

GET
SET
GETSET
STRLEN
SETEX
SETNX
SETRANGE Replace part of the string value of the specified Key. Starting from offset, the replacement length is the string length of the third parameter value of the command. If the value of offset is greater than the string length of the original value of the Key,

Redis will be in the string length of Value. Then pad the (offset - strlen(value)) number of 0x00, and then append the new value. If the key does not exist, this command will assume that its original value length is 0, and append offset characters

0x00 before appending the new value. Given that the maximum length of the string Value is 512M, the maximum value of offset is 536870911. The last thing to note is that if the command causes the original value of the specified Key to be lost when executed, when the length increases, Redis will reallocate enough memory to accommodate all the replaced strings, so it will be affected to a certain extent. affect performance.

The modified string Value length.
GETRANGE O(1)

If the length of the intercepted string is very short, we can The time complexity of the command is considered O(1), otherwise it is O(N), where N represents the length of the intercepted substring. When this command intercepts a substring, it will include both start (0 represents the first character) and end characters in a closed interval. If the end value exceeds the character length of Value, this command will Will just intercept all character data starting from start.

Substring
O(1)

Offset. If Offset is greater than the character length of Value, Redis will stretch the Value and set the BIT value in the parameter on the specified Offset, with the BIT value added in the middle being 0. The last thing that needs to be

is that the Offset value must be greater than 0.

The original value of the BIT at the specified Offset.
O(1) Returns the value of the BIT at the specified Offset, 0 or 1. This command will return 0 if Offset exceeds the length of the string value, so it always returns 0 for an empty string. The BIT value at the specified Offset.
O(N) N represents the number of keys obtained. Returns the Values ​​of all specified Keys. If one of the Keys does not exist or its value is not of string type, the Value of the Key will return nil. Returns a list of Values ​​for a set of specified Keys.
O(N) N represents the number of specified Keys. This command atomically completes all key/value setting operations in the parameters. Its specific behavior can be seen as executing the SET command iteratively multiple times. This command will not fail and always returns OK.
O(N)

If any Key in this batch of Keys already exists, then the operation will all be rolled back, that is, all modifications will not take effect.


append key value Add value to the original value, if If the key does not exist, create a new key and value instead of appending

substr key start len ​​intercepts the key, starting from start, intercepts the length of len

strlen key obtains the length of the key

incr key increases by 1

decr key decreases by 1

incrby key num increases key, increases num

decrby key num decreases key, decreases num

getrange key start end intercepts the character key [start, end] and the header also contains the tail

setrange key offset value replaces the data at the offset position with value (offset is the index of the key)

setex key seconds value sets the expiration time of the key

If the key does not exist, executing the setnx command will set the key and value; but if the key already exists, the setnx command will fail and the value cannot be added again

mset key1 value key2 value Set multiple keys and values ​​at one time

mget key1 key2 Get the value of multiple keys at one time

msetnx key1 value key2 value Set multiple keys and values ​​at one time value If one of the keys exists, all creations will fail (atomicity)

getset key value If it does not exist, get nil, and then set the value. If it refers to the value before getting it, set the subsequent value (update operation) )

###############################################
127.0.0.1:6379> set key1 v
OK
127.0.0.1:6379> get key1
"v"
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> append key1 v1
(integer) 3
127.0.0.1:6379> get key1
"vv1"
127.0.0.1:6379> substr key1 0 3
"vv1"
127.0.0.1:6379> strlen key1
(integer) 3
127.0.0.1:6379> append key1 "hello1"
(integer) 9
127.0.0.1:6379> substr key1 1 2
"v1"
127.0.0.1:6379> substr key1 1 1
"v"
127.0.0.1:6379> append key2 "lisi"
(integer) 4
127.0.0.1:6379> get key2
"lisi"
###############################################
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> incrby views 10
(integer) 10
127.0.0.1:6379> decrby views 5
(integer) 5
####################################
127.0.0.1:6379> set key1 "hello,world"
OK
127.0.0.1:6379> GETRANGE key1 0 3
"hell"
127.0.0.1:6379> GETRANGE key1 0 -1
"hello,world"
127.0.0.1:6379> SETRANGE key2 3 2
(integer) 7
127.0.0.1:6379> get key2
"abc2efg"
########################################
127.0.0.1:6379> setex key3 30 hello
OK
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> ttl key3
(integer) 24
127.0.0.1:6379> SETNX mykey redis
(integer) 1
127.0.0.1:6379> keys *
1) "mykey"
2) "key2"
3) "key1"
127.0.0.1:6379> SETNX mykey "MongoDB"
(integer) 0
127.0.0.1:6379> get mykey
"redis"
####################################
127.0.0.1:6379> mset k1 v1 k2 v2
OK
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
127.0.0.1:6379> MSETNX k1 v1 k3 v3
(integer) 0
# 对象
# 这里的key是一个巧妙的设计 user:{id}:{filed} 
127.0.0.1:6379> msetnx user:1:name "zhangsan" user:1:age 2
(integer) 1
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
127.0.0.1:6379> set article:101:views 0
OK
127.0.0.1:6379> incr article:101:views
(integer) 1
127.0.0.1:6379> get article:101:views
"1"
###########################################################
127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db 10
"redis"

String Similar scenario: value can be our number in addition to our string!

    Counter
  • Count the number of multiple units uid:1923:follow 0
  • Number of fans
  • Object cache storage

The above is the detailed content of Analysis of String data type examples in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete