Redis practice (3) Data Type 1 Strings

Source: Internet
Author: User
Redis author SalvatoreSanfilippo once published an article titled Redis Declaration (RedisManifesto). The article lists seven principles of Redis to clarify Redis ideas ..

Redis author SalvatoreSanfilippo once published an article titled Redis Declaration (RedisManifesto). The article lists seven principles of Redis to clarify Redis ideas ..

Redis author antirez (Salvatore Sanfilippo) Once published a Redis Declaration (Redis Manifesto)
This article lists the seven principles of Redis to illustrate the ideas of Redis.


1. Redis is a language tool for operating data structures. It provides TCP-based protocols to operate on rich data structures.
In Redis, the term "Data Structure" not only indicates operations in a certain data structure, but also includes the structure itself.
And the time space complexity of these operations.


2. Redis is located in a memory database. It is precisely because of the fast access to memory that Redis can have such
High Performance enables Redis to easily process a large number of complex data structures. Redis will try other storage aspects.
But will never change it as a memory database role.


3. Redis uses basic APIs to operate on basic data structures. Redis APIs are the same as data structures and are the most basic
You can use this API format to represent almost any information interaction. The author joked that if there are other non-human
Class smart creature, they can also understand Redis API. Because it is such a foundation.


4. Redis has a poetic and elegant code. Some people who are not familiar with Redis often suggest using Redis.
Other people's code to implement some features not implemented by Redis, but this is like A Dream of Red Mansions for us.
The same is true for the last 40 times.


5. Redis always avoids complexity. We believe that the essence of designing a system is to combat complexity. We will not add thousands of lines of code to the source code for a small function. The solution to the complex problem is to leave the complex problem unmentioned.


6. Redis supports APIs in two layers. The first layer includes APIs for some operations, but it supports APIs for distributed environments.
Redis. The second layer of API supports more complex multi-key operations. They have their own strengths, but we will not push
Both APIs are supported, but we hope to provide commands for data migration between instances and execute multi-key operations.

7. We are happy to optimize the code. We believe that encoding is a hard job, and the only thing we can do is to enjoy it.
Accept it. If we lose fun in coding, the best solution is to stop. We will never choose to make Redis not a fun development model.

Redis author antirez once laughed to call Redis a data Structure server)
This is a very accurate expression. All Redis functions are to store data in several inherent structures and provide
Provides users with interfaces to operate these structures. This article describes various data types supported by Redis and their operation interfaces.

Strings type and Operation


String is the simplest type. You can think of it as the same type as Memcached. A key corresponds to
Value, which supports operations similar to those of Memcached. However, it provides more functions.
The string type is binary secure. It means that the redis string can contain any data, such as jpg images or sequences.
Column object. From the perspective of internal implementation, the string can be viewed as a byte array with a maximum of 1 GB. below is
Definition of the string type:

Struct sdshdr {long len; long free; char buf [] ;};

Len is the length of the buf array.
Free is the number of available bytes in the array, so we can understand why the string type is binary secure because it
In essence, it is a byte array. Of course, it can contain any data.
Buf is a char array used to store the actual string content. In fact, the byte in char and c # Is equivalent and is
Bytes.
In addition, some commands of the string type can be processed by int, such as incr. If only the string type is used, redis will
It can be seen as memcached with persistence features.
Of course, redis has more operations on the string type than memcached. The specific operation method is as follows:

Set:

Set the value corresponding to the key to a value of the string type.
For example, if we add a key-Value Pair named name = HongWan, we can do this:

Setnx:

Set the value corresponding to the key to a value of the string type.
If the key already exists, 0 is returned. nx indicates not exist.
For example, if we add a key-value pair with name = HongWan_new, we can do this:

Setex:

Set the value corresponding to the key to a value of the string type and specify the validity period of the key value.
For example, we add a haircolor = red key-Value Pair and specify that it is valid for 10 seconds. You can do this:

It can be seen that the last call is 10 seconds later, so the value corresponding to the haicolor key cannot be obtained.


Setrange:

Specifies the sub-string of the value of the specified key.
For example, if we want to replace HongWan's 126 mailbox with gmail mailbox, we can do this:

5 indicates replacement starting from the character with a subscript of 5 (including 5 ).

Mset:

If multiple key values are set at a time, "OK" is returned, indicating that all values are set, and "0" is returned for failure, indicating that no value is set.

Msetnx:

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.