REDIS02 using Redis Database (String type)

Source: Internet
Author: User

A String type

First use the Start server process:

Redis-server.exe

1. Set

Sets the value of the key corresponding to string type.

Example: Inserting a record with a data type of string into a Redis database.
In the Client input command:

127.0. 0.1 6379  127.0. 0.1:6379set127.0. 0.1:6379get  foo"test"

2.setnx

Sets the value of the key corresponding to string type value, if key already exists, return 0,nx is not exist meaning. The command is simply understood as: Set a key and update the value if the value corresponding to the key does not exist.

Redis 127.0.0.1:6379> setnx foo1 AAA
(integer) 1
Redis 127.0.0.1:6379> Get Foo1
"AAA"
Redis 127.0.0.1:6379> setnx foo1 BBB
(integer) 0

3. Setex

Set the value of the key to a string type of value, and set the validity period for this key value.

For example: Add a haircolor=red key-value pair and specify a validity period of 10 seconds.

Redis 127.0.0.1:6379> Setex Haircolor Red
Ok
Redis 127.0.0.1:6379> Get Haircolor
"Red"
Redis 127.0.0.1:6379> Get Haircolor
(nil)

4 SetRange

Sets the substring of the value of the specified key

For example: We want to replace the XPXIAOWU 126 mailbox with a Gmail mailbox.

Redis 127.0.0.1:6379> set name [email protected]
Ok
Redis 127.0.0.1:6379> setrange Name 6 gmail.com
(integer) 15
Redis 127.0.0.1:6379> Get Name
"[Email protected]"

5.mset

Setting the value of multiple keys at once, successfully returning OK means that all values are set, and a failure return of 0 means that no value is set.

127.0. 0.1:6379>127.0. 0.1:6379get  key1"Wangwu"   127.0. 0.1:6379get  key2"Lisi"

6.msetnx

Setting the value of multiple keys at once, successfully returning OK, indicating that all values are set, failure returns 0, indicating that no value is set, but does not overwrite the existing key.

If one key is not set successfully, no other key will be set successfully.

Redis127.0.0.1:6379>msetnx key1 AAA key2 BBB key3 CCC (integer)0Redis127.0.0.1:6379>GetKey1"Wangwu"Redis127.0.0.1:6379>GetKey2"Lisi"Redis127.0.0.1:6379>GetKey3 (Nil)

Redis 127.0.0.1:6379> msetnx Key3 AAA key4 BBB key5 CCC
(integer) 1
Redis 127.0.0.1:6379> Get Key1
"Wangwu"
Redis 127.0.0.1:6379> Get Dkey2
(nil)
Redis 127.0.0.1:6379> Get Key3
"AAA"
Redis 127.0.0.1:6379> Get Key4
"BBB"
Redis 127.0.0.1:6379> Get Key5
"CCC"

7. Get get can correspond to a string value if key does not exist return nil.

Getset sets the value of key and returns the old value of key.

GetRange

Gets the substring of the value corresponding to the key.

0 5

Mget

Gets the value of more than one key at a time and returns nil if the corresponding key does not exist.

 redis 127.0 . 0.1 : 6379  >  Mget key1 key2 Key3 key4 key5  1 )  "   2 )  " lisi   " 3 )  " aaa   4 )  bbb  "  )  CCC   

8. incr

Perform a good operation on the value of key and return the new value.

Incrby

Similar to INCR, plus the specified value, key does not exist when you set key back, and think the original value is 0.

Redis 127.0.0.1:6379> Set Key6 0
Ok
Redis 127.0.0.1:6379> Get Key6
"0"
Redis 127.0.0.1:6379> incr Key6
(integer) 1

Redis 127.0.0.1:6379> Set Key7 1
Ok
Redis 127.0.0.1:6379> Get Key7
"1"
Redis 127.0.0.1:6379> Incrby Key7 5
(integer) 6
Redis 127.0.0.1:6379> Get Key7
"6"

Decr

The value of the key is reduced by doing the subtraction operation.

Decrby

Similar to DECR, minus the specified value.

9. Append

Appends value to the string of the specified key, returning the length of the new string value.

Redis127.0.0.1:6379>GetKey6"1"Redis127.0.0.1:6379> Append key6 @163. com (integer)9Redis127.0.0.1:6379>GetKey6"[email protected]"

REDIS02 using Redis Database (String type)

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.