Redis Detailed: Strings data types and operations

Source: Internet
Author: User

Salvatore Sanfilippo, the Antirez author of Redis, has published an article called Redis Manifesto, which lists the seven principles of Redis to clarify the idea of Redis.

1. Redis is a language tool that operates data structures that provides TCP-based protocols to manipulate rich data structures. In Redis, the meaning of the term data structure not only represents the operation on some data structure, but also includes the structure itself and the complexity of the time space of these operations.

2, Redis is located in a memory database, it is because of the fast access features of memory, so that Redis can have such a high performance, so that Redis can easily handle a large number of complex data structures, Redis will try other storage options, But it will never change the role of a memory database.

3. Redis uses basic API operations based data structure, Redis API and data structure, are some of the most basic elements, you can almost any information interactively use this API format representation. The authors joked that if there were other non-human intelligent organisms, they would understand the Redis API. Because it's such a foundation.

4, Redis has a beautiful poetic code, often have some people who do not know much about Redis some would suggest that Redis adopt some other people's code to achieve some of the features that Redis does not realize, but this is like for us to give the "dream of Red mansions" after 40 back.

5, Redis always avoid complications, we think that the essence of designing a system is to combat complications. We don't add thousands of lines of code to the source for a small feature, and the way to solve complex problems is to never ask complex questions.

6. Redis supports two layers of APIs, the first layer contains some operations API, but it supports redis for distributed environments. The second level of the API supports more complex multi-key operations. They have their own strengths, but we will not launch APIs that both support, but we want to be able to provide commands for data migration between instances and perform multi-key operations.

7, we optimize the code for music, we believe that coding is a hard work, the only thing worthy of this hard is to enjoy it. If we lose the fun in coding, the best solution is to stop. We will never choose a development pattern that makes redis less fun.

Antirez, the author of Redis, laughs at Redis as a data structure server (data Structures server), which I think is a very accurate statement, and all of Redis's functions are to store data in several of its inherent structures, and provide the interface to the user to manipulate these kinds of structures. This article describes the various data types supported by Redis and their operating interfaces.

Strings Type and operation

String is the simplest type, you can understand that it is the same type as memcached, a key corresponds to a value, and the operation supported on it is similar to the operation of Memcached. But it's more versatile.

The string type is binary safe. This means that a Redis string can contain any data, such as a JPG image or a serialized object. From an internal implementation, a string can be treated as a byte array with a maximum limit of 1G bytes, and the following is the definition of a string type:

structSDSHDR {
Long Len;
Long free ;
Char buf[];
};

Len is the length of the BUF array.

Free is the remaining number of bytes available in the array, which allows you to understand why the string type is binary safe because it is essentially a byte array, and of course it can contain any data.

BUF is a char array used to store the actual string content, in fact Char and C # byte is equivalent, is a byte.

In addition, the string type can be handled by a partial command by Int. For example, INCR commands, if only string type, Redis can be considered as a memcached with the persistence feature. Of course, Redis has much more to do with string types than memcached, as follows:

  1. Set

Sets the value of the key corresponding to string type.

For example, we add a name= Hongwan key-value pair, which can be done:

Redis127.0.0.1:6379> Set name Hongwan
Ok
Redis 127.0.0.1:6379>

  2, Setnx

Sets the value of the key corresponding to string type. If key already exists, returning 0,nx is the meaning of not exist.

For example, we add a name= hongwan_new key-value pair, which can be done:

Redis127.0.0.1:6379> Get name
"Hongwan"
Redis 127.0.0.1:6379> setnx name Hongwan_new
(integer) 0
Redis 127.0.0.1:6379> get name
"Hongwan"
Redis 127.0.0.1:6379>

Because the original name has a corresponding value, so this modification does not take effect, and the return code is 0.

  3, Setex

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

For example, we add a haircolor= red key-value pair and specify that it is valid for 10 seconds, and you can do this:

Redis127.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)
Redis 127.0.0.1:6379>

It is visible that the last call is 10 seconds later, so the value of the key corresponding to the Haicolor is not taken.

  4, SetRange

Sets the substring of the value of the specified key.

For example, we want to replace the Hongwan 126 mailbox with a Gmail mailbox, so we can do this:

Redis127.0.0.1:6379> Get name
"[Email protected]"
Redis 127.0.0.1:6379> setrange name 8 gmail.com
(integer) 17
Redis 127.0.0.1:6379> get name
"[Email protected]"
Redis 127.0.0.1:6379>

8 of these are the beginning of the substitution of characters from subscript 8 (containing 8)

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.

Redis127.0.0.1:6379> mset key1 HongWan1 key2 HongWan2
Ok
Redis 127.0.0.1:6379> get key1
"HongWan1"
Redis 127.0.0.1:6379> get Key2
"HongWan2"
Redis 127.0.0.1:6379>

  6, Msetnx

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, but does not overwrite the existing key.

Redis127.0.0.1:6379>Get Key1
"HongWan1"
Redis 127.0.0.1:6379> get Key2
"HongWan2"
Redis 127.0.0.1:6379> msetnx key2 hongwan2_new Key3 HongWan3
(integer) 0
Redis 127.0.0.1:6379> get Key2
"HongWan2"
Redis 127.0.0.1:6379> get Key3
(nil)

It can be seen that if this command returns 0, the inside operation will be rolled back and will not be executed.

 7. Get

Gets the string value corresponding to key, if key does not exist return nil.

For example, we get a key that exists in a library name and can quickly get its corresponding value

Redis127.0.0.1:6379> Get name
"Hongwan"
Redis 127.0.0.1:6379>

We get a key name1 that does not exist in the library, then it returns a nil to the table without this key value pair

Redis127.0.0.1:6379> Get name1
(nil)
Redis 127.0.0.1:6379>

  8, Getset

Sets the value of key and returns the old value of key.

Redis127.0.0.1:6379> Get name
"Hongwan"
Redis 127.0.0.1:6379> getset name Hongwan_new
"Hongwan"
Redis 127.0.0.1:6379> get name
"Hongwan_new"
Redis 127.0.0.1:6379>

Let's take a look at what will happen if key doesn't exist?

Redis127.0.0.1:6379> Getset name1 AAA
(nil)
Redis 127.0.0.1:6379>

Visible, if key does not exist, then nil will be returned

 9, GetRange

Gets a substring of the value of the specified key.

The specific example is as follows:

Redis127.0.0.1:6379> Get name
"[Email protected]"
Redis 127.0.0.1:6379> getrange name 0 6
"Hongwan"
Redis 127.0.0.1:6379>

The index to the left of the string starts at 0.

Redis127.0.0.1:6379> getrange name -7 -1
"126.com"
Redis 127.0.0.1:6379>

The subscript to the right of the string starts at-1.

Redis127.0.0.1:6379> getrange name 7 100
"@126.com"
Redis 127.0.0.1:6379>

When the current label exceeds the string length, it is assumed to be the largest subscript in the same direction

 10, Mget

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

The specific example is as follows:

Redis127.0.0.1:6379> mget key1 key2 Key3
1) "HongWan1"
2) "HongWan2"
3) (nil)
Redis 127.0.0.1:6379>

Key3 returns nil because it does not have this key definition.

  11, INCR

Make a Gaga operation on the value of key and return the new value. Note INCR A value that is not an int returns an error incr a nonexistent key, setting key to 1

Redis127.0.0.1:6379> Set age 20
Ok
Redis 127.0.0.1:6379> incr Age
(integer) 21
Redis 127.0.0.1:6379> get Age
"21"
Redis 127.0.0.1:6379>

  12, Incrby

Similar to INCR, plus the specified value, key does not exist when the key is set, and the original value is considered to be 0

Redis127.0.0.1:6379> Get Age
"21"
Redis 127.0.0.1:6379> Incrby age 5
(integer) 26
Redis 127.0.0.1:6379> get name
"[Email protected]"
Redis 127.0.0.1:6379> get Age
"26"
Redis 127.0.0.1:6379>

 13, DECR

The value of the key is a decrement operation, decr a key does not exist, then set key to 1

Redis127.0.0.1:6379> Get Age
"26"
Redis 127.0.0.1:6379> decr Age
(integer) 25
Redis 127.0.0.1:6379> get Age
"25"
Redis 127.0.0.1:6379>

 14, Decrby

With DECR, minus the specified value.

Redis127.0.0.1:6379> Get Age
"25"
Redis 127.0.0.1:6379> Decrby age 5
(integer) 20
Redis 127.0.0.1:6379> get Age
"20"
Redis 127.0.0.1:6379>

Decrby is purely for readability, we can achieve the same effect by Incrby a negative value, and vice versa.

Redis127.0.0.1:6379> Get Age
"20"
Redis 127.0.0.1:6379> Incrby age -5
(integer) 15
Redis 127.0.0.1:6379> get Age
"15"
Redis 127.0.0.1:6379>

  15, append

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

For example, if we append a @126.com string to the value of name, we can do this:

Redis127.0.0.1:6379> Append name @126.com
(integer) 15
Redis 127.0.0.1:6379> get name
"[Email protected]"
Redis 127.0.0.1:6379>

  16, Strlen

Takes the length of the value of the specified key.

Redis127.0.0.1:6379> Get name
"Hongwan_new"
Redis 127.0.0.1:6379> strlen Name
(integer) 11
Redis 127.0.0.1:6379> get Age
"15"
Redis 127.0.0.1:6379> strlen Age
(integer) 2
Redis 127.0.0.1:6379>

Redis Detailed: Strings data types and operations

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.