Redis common commands (2) - String

APPEND

Format: APPEND key value

Function: Append value after the key value of key. If the key does not exist, create the key and store the value.

Return value: The length of the string after appending value.

Example:

192.168.1.100:6379> set testkey hello

OK

192.168.1.100:6379> append testkey " word"

(integer) 10

192.168.1.100:6379> get testkey

"hello word"

 

BITCOUNT

Format: BITCOUNT key [start] [end]

Function: Calculate the number of bits 1 in the value of the key. The start and end positions of the statistics can be specified with start and end.

Return value: the number of bit 1s. Returns 0 if the key does not exist.

Example:

192.168.1.100:6379> set testkey 5

OK

192.168.1.100:6379> bitcount testkey

(integer) 4

192.168.1.100:6379> bitcount testkey 0 1

(integer) 4

192.168.1.100:6379> bitcount testkey 0 2

(integer) 4

192.168.1.100:6379> bitcount testkey 1 2

(integer) 0

 

BITOP

IT : BITOP operation destkey key [key ...]

Function: perform bit operation on one or more string keys, and save the operation result in destkey. There are four types of operations

AND performs a bitwise AND operation;

OR performs a bitwise OR operation;

XOR performs a bitwise exclusive OR operation;

NOT performs a bitwise negation operation.

If one of the keys has a shorter string length, the missing part is treated as 0.

Return value: the length of the destkey string.

Example:

192.168.1.100:6379> set testkey1 E

OK

192.168.1.100:6379> set testkey2 F

OK

192.168.1.100:6379> bitop and testkey3 testkey1 testkey2

(integer) 1

192.168.1.100:6379> get testkey3

"D"

192.168.1.100:6379> bitop or testkey3 testkey1 testkey2

(integer) 1

192.168.1.100:6379> get testkey3

"G"

192.168.1.100:6379> bitop xor testkey3 testkey1 testkey2

(integer) 1

192.168.1.100:6379> get testkey3

"\x03"

192.168.1.100:6379> bitop not testkey3 testkey1

(integer) 1

192.168.1.100:6379> get testkey3

"\xba"

 

DECR

Format: DECR key

Function: Decrement the given key by one. If the key does not exist, the key is created first, initialized to 0, and then decremented by one.

Return value: The value after subtracting one. Returns an error if the value of key cannot be represented as a number.

Example:

192.168.1.100:6379> set testkey 9

OK

192.168.1.100:6379> decr testkey

(integer) 8

 

DECRBY

Format: DECRBY key decrement

Function: Perform a decrement operation on the given key. If the key does not exist, create the key first, initialize it to 0, and then perform the decrement operation.

Return value: the value after subtracting decrement. Returns an error if the value of key cannot be represented as a number.

Example:

192.168.1.100:6379> set testkey 9

OK

192.168.1.100:6379> decrby testkey 3

(integer) 6

 

GET

Format: GET key

Function: Get the string value of key. Returns nil if the key does not exist.

Return value: String value of key. If the value of key is not of type string, return an error.

 

GETBIT

Format: GETBIT key offset

Function: Get the value of the specified bit of the key.

Return value: the value of the specified bit of key. Returns 0 if the offset is outside the range of the key's value, or if the key does not exist.

Example: The ASCII code of 2 is 00110010

192.168.1.100:6379> set testkey 2

OK

192.168.1.100:6379> getbit testkey 0

(integer) 0

192.168.1.100:6379> getbit testkey 1

(integer) 0

192.168.1.100:6379> getbit testkey 2

(integer) 1

 

GRANGE

Format: GETRANGE key start end

Function: Get the substring of the value of the key, and set the interception range by start and end. If the offset is negative, it means to start counting from the end, you can use -1 to represent the last character. If end is outside the range of key values, it ends with the last character.

Return value: substring.

Example:

192.168.1.100:6379> set testkey "hello"

OK

192.168.1.100:6379> getrange testkey 0 1

"he"

192.168.1.100:6379> getrange testkey 2 3

"ll"

 

GETSET

Format: GETSET key value

Function: Set the value of key to value and return the old value of key.

Return value: the old value of key. Returns nil if the key does not exist.

 

INCR

Format: INCR key

Function: Add one to the given key. If the key does not exist, create the key first, initialize it to 0, and then add one.

Return value: The value after adding one. Returns an error if the value of key cannot be represented as a number.

Example:

192.168.1.100:6379> set testkey 5

OK

192.168.1.100:6379> incr testkey

(integer) 6

 

INCRBY

Format: INCRBY key increment

Function: Perform an increment operation on the given key. If the key does not exist, create the key first, initialize it to 0, and then perform the increment operation.

Return value: the value after adding increment. Returns an error if the value of key cannot be represented as a number.

Example:

192.168.1.100:6379> set testkey 5

OK

192.168.1.100:6379> incrby testkey 5

(integer) 10

 

INCRBYFLOAT

Format: INCRBYFLOAT key increment

Function: Perform an increment operation on the given key. If the key does not exist, create the key first, initialize it to 0, and then perform the increment operation. The calculation result can only represent at most the last seventeen digits of the decimal point. increment can take the exponential notation e, such as 2e5.

Return value: the value after adding increment. Returns an error if the value or increment of key cannot be represented as a float.

Example:

192.168.1.100:6379> set testkey 5

OK

192.168.1.100:6379> incrbyfloat testkey 5.23

"10.23"

 

MGET

Format: MGET key [key...]

Function: Returns the value of one or more keys. If any key does not exist, the corresponding return value is nil.

Return value: A list containing values ​​for multiple keys.

Example:

192.168.1.100:6379> mget testkey testkey2 testkey3

1) "10.23"

2) "F"

3) "\xba"

 

MSET

格式:MSET key value [key value ...]

Function: Set the value of one or more keys at the same time.

Return value: OK

Example:

192.168.1.100:6379> mset testkey 1 testkey2 2

OK

 

MSETNX

格式:MSETNX key value [key value ...]

作用:当所有key都不存在时同时设置一个或多个key的值。如果有某个key存在,则不会执行任何设置操作。原子性操作,要么都设置值,要么都不设置值。

返回值:如果所有key都设置成功,返回1。如果有key设置出错,返回0。

示例:

192.168.1.100:6379> msetnx testkey 1 testkey2 2

(integer) 1

192.168.1.100:6379> msetnx testkey 1 testkey2 2

(integer) 0

 

PSETEX

格式:PSETEX key milliseconds value

作用:设置key的值为value,过期时间为milliseconds毫秒。

返回值:成功时返回 OK

示例:psetex testkey 10000 ff

 

SET

格式:SET key value [EX seconds] [PX milliseconds] [NX|XX]

作用:设置key的值为value。

EX seconds 设置过期时间为seconds秒;

PX milliseconds 设置过期时间为milliseconds毫秒;

NX 只有key不存在时,才进行设置操作;

XX 只有key存在时,才进行设置操作。

返回值:成功返回OK

示例:

192.168.1.100:6379> set testkey ff EX 5 NX

OK

192.168.1.100:6379> get testkey

"ff"

192.168.1.100:6379> set testkey ff EX 5 XX

OK

192.168.1.100:6379> set testkey ff EX 5 NX

(nil)

192.168.1.100:6379> get testkey

(nil)

192.168.1.100:6379> set testkey ff EX 5 XX

(nil)

 

SETBIT

格式:SETBIT key offset value

作用:设置key的值的offset比特位为value。如果offset超出了key的值的范围,则中间位置补零。

返回值:offset比特位的旧值。

示例:

192.168.1.100:6379> set testkey 5

OK

192.168.1.100:6379> setbit testkey 7 0

(integer) 1

192.168.1.100:6379> get testkey

"4"

192.168.1.100:6379> setbit testkey 7 1

(integer) 0

192.168.1.100:6379> get testkey

"5"

 

SETEX

格式:SETEX key seconds value

作用:设置key的值为value,过期时间为 seconds 秒。

返回值:成功时返回 OK

示例:setex testkey 10 ff

 

SETNX

格式:SETNX key value

作用:如果key不存在,则创建key值为value。如果key存在,则不操作。

返回值:成功返回1,失败返回0。

示例:

192.168.1.100:6379> setnx testkey ff

(integer) 1

192.168.1.100:6379> setnx testkey ff

(integer) 0

192.168.1.100:6379> del testkey

(integer) 1

192.168.1.100:6379> setnx testkey ff

(integer) 1

 

SETRANGE

格式:SETRANGE key offset value

作用:从key的值的offset偏移开始使用value覆盖字符串。如果offset超出了key的值的范围,则中间位置补零。

返回值:修改后的字符串的长度。

示例:

192.168.1.100:6379> set testkey hellohello

OK

192.168.1.100:6379> setrange testkey 5 word

(integer) 10

192.168.1.100:6379> get testkey

"hellowordo"

 

STRLEN

格式:STRLEN key

作用:返回字符串的长度。

返回值:字符串的长度。如果key不存在返回0;如果key不是字符串类型,返回错误。

示例:

192.168.1.100:6379> get testkey

"hellowordo"

192.168.1.100:6379> strlen testkey

(integer) 10

 

原文地址:http://caiguoqing.org/post/104

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724531&siteId=291194637