REDIS02 uses Redis database (string type) to fully parse _redis

Source: Internet
Author: User
Tags redis

A String type

First use the Start server process:

Redis-server.exe

1. Set

The value of the SET key corresponds to the string type.

Example: Inserts a record of a data type string into the Redis database.

To enter a command on the client:

C:\software\redis\64bit>redis-cli.exe-h 127.0.0.1-p 6379
redis 127.0.0.1:6379> set foo test
OK
Redis 127.0.0.1:6379> get foo

2.setnx

Setting the key corresponds to the value of string type, and if the key already exists, returning 0,NX is not the meaning of exist. The command is simply understood to be: set a key and update the value if the value of 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

3. Setex

The value for the key is set to string, and the validity period for this key value is established.

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

4 SetRange

Sets the substring of value for the specified key

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

Redis 127.0.0.1:6379> set name abcde@126.com
OK
redis 127.0.0.1:6379> setrange name 6 gmail.com
( Integer)
redis 127.0.0.1:6379> get Name

5.mset

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

Redis 127.0.0.1:6379> mset key1 wangwu key2
' Lisi OK redis 127.0.0.1:6379> get
key1
'
Wangwu ' Redis 127.0.0.1:6379> Get Key2

6.msetnx

Set the value of multiple keys at once, and return OK successfully, indicating that all values are set and the failure returns 0, indicating that no value is set, but no existing key is overwritten.

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

Redis 127.0.0.1:6379> msetnx key1 AAA key2 BBB key3 CCC
(integer) 0
redis 127.0.0.1:6379> get key1
"wan GWU '
redis 127.0.0.1:6379> get key2
' Lisi ' Redis 127.0.0.1:6379> get
key3
(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 obtains can correspond to string value, if key does not exist return nil.

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

GetRange

Gets a substring of the value value for the key.

GetRange Name 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) "Wangwu"
2) "Lisi"
3) "AAA"
4) "BBB"
5) "CCC"

8. incr

Does a Gaga operation on the value of the key and returns a new value.

Incrby

Similar to INCR, plus the specified value, key does not exist when the key is set back, and that 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 operations.

Decrby

Similar to DECR, minus the specified value.

9. Append

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

Redis 127.0.0.1:6379> Get Key6
"1"
redis 127.0.0.1:6379> append key6 @163.com
(integer) 9
Redis 127.0.0.1:6379> Get Key6
1@163.com

The above is a small set to introduce the REDIS02 use of Redis database (string type) comprehensive analysis, hope to help everyone, if you want to know more content, please pay attention to cloud habitat community!

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.