Redis string Command _redis

Source: Internet
Author: User
Tags numeric value redis version

The string command is the most common, simplest command in Redis.

1, set command-set the value of the specified key
The set command in Redis sets the value of the given key, overwriting the old value if the key already stores another value.
Grammar:

Set Key_name value

Version: 1.0.0 and above
return value:
In the previous version of Redis 2.6.12, the SET command always returns OK
Starting with Redis version 2.6.12, SET returns OK when the set operation completes successfully

Redis 127.0.0.1:6379> SET Key "value"
OK

redis 127.0.0.1:6379> get key
"value"
Redis 127.0.0.1:6379> SET Key "New-value"
OK

redis 127.0.0.1:6379> get key
"New-value"

2, Get command-gets the specified key value
Grammar:

Get Key_name

Version: 1.0.0 and above
Return value: Returns the value of the key and returns nil if the key does not exist. If the key is not a string type, an error is returned.

127.0.0.1:6379> get MyNumber
"M"
127.0.0.1:6379> get ABC
(NIL)
127.0.0.1:6379> get MyList
(Error) Wrongtype Operation against a key holding the wrong kind of value
127.0.0.1:6379>

3, GetRange command-returns substring of string value in key
Redis The GetRange command is used to get the substring stored in the string in the specified key. The Intercept range for a string is determined by the start and end two offsets.
Grammar:

GetRange keyname Start End

Version: 2.4.0 and above
return value:
intercepts the resulting substring. If the KeyName type is not a string, report the corresponding error

127.0.0.1:6379> getrange mynumber 1 2 "
127.0.0.1:6379> getrange ABC 1 2
" "
127.0.0.1:6379 > GetRange mylist
(Error) ERR wrong number of arguments for ' getrange ' command
127.0.0.1:6379>

4. Getset command-Sets the value of the given key to values and returns the old value of the key.
The Getset command in Redis sets the value of the specified key and returns the old value of the key.
Grammar:

Getset  KeyName Vale

Version: 1.0.0 and above
Returns a value that returns the old value of the given key and returns nil when the key does not have an old value, when the key does not exist.
Returns an error when the key exists but is not a string type.

127.0.0.1:6379> getset MyNumber "10000"
"
127.0.0.1:6379> getset" AAA
(Error) ERR wrong number of  Arguments for ' getset ' command
127.0.0.1:6379> getset mylist
(Error) ERR wrong # of arguments for ' getset ' Command
127.0.0.1:6379>

5. Getbit command-The string value stored for key gets the specified bit on the cheap.
Grammar:

Getbit KeyName Offset

Version: 2.2.0 and above
return value:
String value specifies the bit (bit) on the offset
Returns 0 when the offset is greater than the length of the string value, or if the key does not exist.

127.0.0.1:6379> getbit mynumber 3
(integer) 1
127.0.0.1:6379> getbit AAA 1
(integer) 0
127.0.0.1:6379> getbit mylist 1
(Error) Wrongtype Operation against a key holding the wrong kind of value

Because the bit of the key with string value may be 0 and 1. Does not exist is also 0, so this command is not very common

6, mget command-get roommate the value of the given key
The Mget command in Redis returns all (one or more) of the given key values. If a key does not exist in the given key, the key returns a special value nil
Grammar:

Mget Key1 [Key2 ... Keyn]

Version: 1.0.0 and above
return value:
A list of values that contain all the given key

127.0.0.1:6379> mget mynumber aaa mylist
1) "10000"
2) (nil)
3) (nil)
127.0.0.1:6379>

7, Setbit command-the string value stored on key, set or clear bit (bit) on the specified offset
Grammar:

Setbit KeyName Offset Value (0 or 1)

Version: 2.2.0 and above
return value:
Specifies the bit of the original storage for the offset.
eg

127.0.0.1:6379> setbit mynumber 2 0
(integer) 1
127.0.0.1:6379> getbit mynumber 2
(integer) 0
127.0.0.1:6379> get MyNumber
"\x110000"
127.0.0.1:6379> setbit mynumber 2 1
(integer) 0
127.0.0.1:6379> getbit mynumber 1
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> get MyNumber
"10000"

8, Setex command-Associates value values to key, set expiration time for key
Redis the Setex command sets the value and expiration time for the specified key. If the key already exists, the Setex command replaces the old value.
Grammar:

Setex KeyName Timeout value

Version: 2.0.0
Return value: Returns OK when the setting succeeds
eg

127.0.0.1:6379> Setex Testsetex "Just a test"
OK
127.0.0.1:6379> ttl testsetex
(integer)
127.0.0.1:6379> Setex mylist "Second Test"
OK
127.0.0.1:6379> get mylist
"second Test"

Note: The value of the key, regardless of its original type, is reset to the input value.

9, SetRange command-overwrites the string stored by the given key starting at the specified offset.
Grammar:

SETRANGE KeyName  Offset value

Version: 2.0.0
return value:
The length of the string being modified.
eg

Redis 127.0.0.1:6379> SET Key1 "Hello World"
OK
redis 127.0.0.1:6379> SETRANGE key1 6 "Redis"
( Integer) One
redis 127.0.0.1:6379> get key1
"Hello Redis"

Error condition:

127.0.0.1:6379> Rpush mylist "abc"
(integer) 1
127.0.0.1:6379> get mylist
(Error) Wrongtype Operation against a key holding the wrong kind of value
127.0.0.1:6379> get key1
"Hello redis"
127.0.0.1:63 79> setrange "wuzhiming"
(Error) ERR wrong number of arguments for ' setrange ' command
127.0.0.1:6379> SE Trange 2 Mylilst "wuzhiming"
(Error) ERR value is ' not ' an integer or out of range
127.0.0.1:6379>

10, strlen command-Returns the length of the string value stored by the key.
The strlen command in Redis is used to get the length of the string stored by the specified key. Returns an error when the key stores a value other than a string.
Grammar:

Strlen KeyName

Version: 2.2.0 and above
Return value: The length of the string. Returns 0 when key does not exist
eg

redis> SET MyKey "Hello World"
OK
redis> STRLEN mykey
(integer)
redis> STRLEN Nonexisting
(integer) 0

11, Mset command-set one or more key-value at the same time
The Mset command in Redis is used to set one or more key-value at the same time. Where value needs to be a string type.
Grammar:

Mset key1 value1 [key2 value2 ... Keyn Valuen]

Version: 1.0.1
return value: OK
eg

127.0.0.1:6379> mset Key2 "AAA" Key3 "BBB" MyList "CCC"
OK
127.0.0.1:6379> get mylist
"CCC"
127.0.0.1:6379>

As can be seen from the example, even if the key is originally a list type, it will be overwritten with the string type.

12, msetnx command: Set up multiple key-value at the same time. When and only if all the given keys do not exist.
Redis The MSETNX command is used to set one or more key-value pairs when none of the given keys exists.
Grammar:

Msetnx key1 value1 [key2 value2 ... Keyn Valuen]

Version: 1.0.1 and above
return value:
When all keys are set successfully, return 1. If all the given keys are set to fail (at least one key already exists), then return 0.

13. Psetex command-Sets the value of the key and sets the life time of the key in milliseconds.
Grammar:

Psetex KeyName Expiry_in_millisecond Value

Version: 2.6.0
Return value: Set to return OK successfully
As with Setex, only the corresponding units of time have changed.

14. INCR command-increases the number of atoms stored in the key by 1.
The INCR command in Redis increases the number value stored in the key by 1.
If the key does not exist, the key value 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.
It is now within a 64-bit (bit) signed digit representation.
Grammar:

INCR KeyName

Version: 1.0.0 and above
Return value: The value of the key after the INCR command is executed.
eg

127.0.0.1:6379> set TESTINCR "M"
OK
127.0.0.1:6379> incr testincr
(integer)
127.0.0.1:6379> get testincr
'
127.0.0.1:6379> incr mylist
(Error) ERR value is not ' an integer or OU T of range
127.0.0.1:6379>

15, Incrby command-the value stored by the key plus the given increment value (increment)
The Redis-incrby command adds the values stored in the key to the specified increment value.
If the key does not exist, the value of the key is initialized to 0 and then the Incrby command is executed. If the value contains the wrong type, or the value of the string type cannot be represented as a number, an error is returned, and the value of the operation is limited to the 64-bit signed digit representation.
Grammar:

Incrby KeyName Incrnumber

Version: 1.0.0 and above
return value:
Plus the specified increment value, the value of the key.
eg

127.0.0.1:6379> get testincr
"
127.0.0.1:6379> incrby testincr 2
(integer)
127.0.0.1:6379> get testincr
"127.0.0.1:6379>"
Incrby mylist 2
(Error) ERR value was not a integer o R out of range
127.0.0.1:6379> Incrby AAA
(Error) ERR wrong number of arguments for ' incrby ' command
127.0. 0.1:6379> Incrby AAA 3
(integer) 3

16. Incrbyfloat command-Adds the value stored by the key to the given floating-point increment value.
The incrbyfloat command in Redis adds the specified floating-point increment value to the value stored in the key, and if the key does not exist, then incrbyfloat sets the value of the key to 0 before performing the addition operation.
Grammar:

Increbyfloat KeyName Incrnumber

Version: 2.6.0 and above
return value:
The value of the key after the command is executed.
eg

127.0.0.1:6379> set Float "0.01"
OK
127.0.0.1:6379> get float
"0.01"
127.0.0.1:6379> incrbyfloat float "1.2"
"1.21"
127.0.0.1:6379> get float
"1.21"
127.0.0.1:6379> get TESTINCR "
127.0.0.1:6379>"
incrbyfloat testincr "0.01" "
103.01000000000001"
127.0.0.1:6379> Incrbyfloat mylist "0.01"
(Error) ERR value is not a valid float

17, DECR command-the key stored in the number of values minus 1
The DECR command in Redis minus 1 of the numeric value stored in the key. If the key does not exist, then the value of the key is initialized to 0 before performing the DECR operation.
If the value contains the wrong type, or if the value of the string cannot be represented as a number, an error is returned.
Grammar:

DECR KeyName

Version: 1.0.0 and above
Return value: The value of the key after the command is executed.
eg

# to the existence of the number value key DECR

redis> SET failure_times
OK

redis> decr failure_times
(integer) 9
  # the nonexistent key value DECR

redis> EXISTS Count
(integer) 0

redis> decr count
(integer)-1


# to save DECR redis> SET company Your_code_sucks is performed on a key that is not a numeric value

. LLC
OK

redis> DECR company
(Error) ERR value was not ' an ' or out of range

18, the Decrby command-key the stored value minus the given decrement value.
The Decrby command in Redis subtracts the value that the key stores from the specified decrement.
If the key does not exist, the value of the key is initialized to 0 and then the Decrby operation is performed. 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.
Grammar:

Decrby KeyName Number

Version: 1.0.0 and above
Return value: Minus the value of the key after the specified decrement value.
eg

# The existing key is Decrby

redis> SET count

-OK redis> decrby Count
(integer)


# for nonexistent keys Decrby

redis> EXISTS pages
(integer) 0

redis> decrby pages
(integer)-10

19. Append command-If the key already exists and is a string, the append command appends the value of the key's original value to the end.
Redis the append command is used to append a value to the specified key. If the key already exists and is a string, the append command appends value to the end of the key's original value. If the key does not exist, append simply sets the given key to value, just as it does the set key value.
Grammar:

Append KeyName newvalue

Version: 2.0.0 and above
Return value: The length of the string in the key after the specified value is appended.
eg

 # executes APPEND redis> EXISTS Myphone # to nonexistent key to ensure Myphone does not exist (integer) 0 redis> APPEND Myphone "Nokia" # APPEND the nonexistent key, equivalent to the SET myphone "Nokia" (integer) 5 # character length # for existing strings of the APP  End redis> APPEND myphone "-1110" # length from 5 characters to 12 characters (integer) redis> get Myphone "nokia-1110" 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.