Data types and storage structures in Redis

Source: Internet
Author: User
Tags hash numeric numeric value redis redis server

Redis supports V data types: string (string), hash (hash), list, set (set), and Zset (sortedset: Ordered collection).

Redis defines a rich primitive command that can interact directly with a Redis server. In practice, we do not use these primitive commands directly, and Redis provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, In most cases, we operate redis through a variety of clients. However, the client in any language is actually the encapsulation of the Redis primitive commands, and understanding the primitive commands helps to understand the design principles of the client and knows why.


3.1. String

The string is the most basic data type of redis, and the structure corresponds to one value for a key.

The string type is binary safe, meaning that it can contain any data, such as a JPG image or a serialized object.

The maximum capacity of a string type is 512M.

Unlike Linux, which has so many imaginative commands, it also likes to take a couple of inexplicable arguments. Redis's primitive command is simple, and there are rules to follow, in a nutshell, is clean crisp.

For example, we want to set up a user name to Redis and store it in String type:

127.0.0.1:6379> SET name Chenlongfei

Ok

"OK" is the response returned by Redis, representing the success of the setup.

Remove the value of this name:

127.0.0.1:6379> GET name

"Chenlongfei"

To change the value of name to "CLF", re-set again, overwriting the original value:

127.0.0.1:6379> SET name CLF

Ok

127.0.0.1:6379> GET name

"CLF"

To delete this piece of data:

127.0.0.1:6379> DEL name

(integer) 1--The total number of records that the number represents affects

127.0.0.1:6379> GET name

(nil)--nil representative is empty, the object does not exist

Adding and deleting the command a minute to learn, want to forget all difficult, mother no longer worry about my study.

command Format

Description

SET Key value

Sets the value of the specified key

GET Key

Gets the value of the specified key

setnx Key value

(set if Not Exist) Sets the value of key only if key does not exist

SETRANGE Key offset value

Use the value parameter to write to the string value stored by the key, starting with offset offsets

GETRANGE Key Start end

Returns the substring of a string value in key

getset Key value

Sets the value of the given key to value and returns the old value of key

MSET key value [key value ...]

(Multi set) simultaneously sets one or more key-value pairs

MGET key1 [Key2 ...]

Gets the value of all (one or more) given key

APPEND Key value

If key already exists and is a string, the APPEND command appends value to the end of the original value of the key

Setex Key seconds value

(set Expire) associates the value of values to the key and sets the expiration time of the key to seconds (in seconds)

Psetex Key milliseconds value

(Precise set Expire) This command is similar to the Setex command, but it sets the lifetime of the key in milliseconds, rather than as the Setex command, in seconds

STRLEN Key

Returns the length of the string value stored by key

INCR Key

Increment the numeric value stored in key by one, provided that value is a number

Incrby Key increment

Adds the value stored by key to the given increment value, provided that value is a number

incrbyfloat Key increment

Adds the value stored by key to the given floating-point increment value if value is a number

DECR Key

Subtract the numeric value stored in key by one, provided that value is a number

Decrby Key Decrement

The value stored by key minus the given decrement value, provided that value is a number


3.2. Hash

A Redis hash is a mapping between field and value, a collection of key-value pairs, so it is particularly well-suited for storing objects.

Each hash in Redis can store up to 232-1 key-value pairs (more than 4 billion).

For example, we want to store a user's information in Redis, including the user ID, username, and three fields of the email address:

127.0.0.1:6379>hmset user_1 userId 123 userName CLF Email chenlongfei@163.com

Ok

127.0.0.1:6379> Hgetall user_1

1) "UserId"

2) "123"

3) "UserName"

4) "CLF"

5) "Email"

6) "Chenlongfei@163.com"

command Format

Description

hmset key field1 value1 [Field2 value2 ...]

(Hash Multi Set) simultaneously sets multiple field-value pairs to the hash table key

hmget key field1 [Field2 ...]

Gets the value of all given fields

hset key field value

Sets the value of the field in the hash table key to

hget key field

Gets the value stored in the specified field in the hash table

Hgetall Key

Get all fields and values that specify key in the hash table

Hdel Key field2 [Field2]

Delete one or more hash table fields

hsetnx key field value

Set the value of the hash table field only if the field is not present

Hkeys Key

Gets the fields from all the hash tables

hvals Key

Get all values in the hash table

hexists key field

View Hash table key, whether the specified field exists

Hlen Key

Gets the number of fields in the hash table

hincrby key field increment

Adds an increment to the integer value of the specified field in the hash table key

hincrbyfloat key field increment

Adds an increment to the floating point value of the specified field in the hash table key


3.3. List

The Redis list is a simple list of strings, sorted by insertion order. Support for adding an element to the list header (left) or tail (right) operation.

A list can contain up to 232-1, or more than 4 billion elements.

For example, we would like to use a list named "Continents" to put the names of five continents:

127.0.0.1:6379> lpush Continents Asia Africa America Oceania Antarctica

(integer) 5

127.0.0.1:6379> Lrange Continents 0 4 --Get the element labeled 0~4

1) "Antarctica"

2) "Oceania"

3) "America"

4) "Africa"

5) "Asia"

Although the Redis list is named as a list, in fact it is more like a stack in terms of features, with the most recently placed elements as the head, with the oldest element in the tail, so the subscript of the Redis list is in reverse order. In the above example, the five elements are put in sequence: Asia, Africa, America, Oceania, Antarctica, subscript 4, 3, 2, 1, 0 respectively. This is completely different from the concept of list in Java and requires special attention.

Similar to the stack, when performing a pop operation, the Redis list pops up the newest element, similar to the top element of the stack.

The Redis list also supports a blocking operation such as BLPOP (the abbreviation forBlockd List Pop ), moving out and getting the first element of the list, If the list does not have an element (or the list does not exist) it blocks the list until it waits for a timeout or discovers that it can eject the element.

For example, we execute the blpop command on a nonexistent list "myList":

Blpopmylist 20--pops the first element of the MyList list, if not, blocks for 20 seconds

The client goes into a blocking state, and if the list is stored in the element within 20 seconds, it pops up:

27.0.0.1:6379> blpop myList ----if no element is blocked, the time limit is 20 seconds.

1) "MyList"

2) "Hello"

(6.20s)

If the element is still not waiting after the timeout, the end is blocked and nil is returned:

127.0.0.1:6379> Blpop myList

(nil)

(20.07s)

command Format

Description

lpush key value1 [value2 ...]

Insert one or more values into the list header

Lpop Key

Move out and get the first element of a list

lpushx Key value

(List Push If exist) inserts one or more values into the existing list header

LINDEX Key index

Get the elements in a list by index

Lrange key Start stop

Gets the element within the specified range of the list

LSET Key index value

To set the value of a list element by index

LTRIM key Start stop

Only elements within the specified interval are preserved, and elements that are not within the specified range are deleted

Rpop Key

(Rear Pop) Remove and get the last element of the list

rpush key value1 [value2 ...]

Insert one or more values into the tail of the list

rpushx Key value

Inserts one or more values at the end of a list that already exists

Lrem Key Count value

Removes the element with the value of the field from the list, removes count of the absolute value after the end, Count > 0 is removed from the table header, count < 0 is removed from the footer; count=0 all deleted

rpoplpush Source destination

Removes the last element of the list and adds the element to another list and returns

blpop key1 [key2 ...] timeout

Moves out and gets the first element of the list, if the list has no elements that block the list until the wait time-out or the popup element is found, and if timeout is 0, it waits forever.

brpop key1 [key2 ...] timeout

Moves out and gets the last element of the list, if the list has no elements that block the list until the wait time-out or the popup element is found, and if timeout is 0, it waits forever.

Linsert key before | after pivot value

Look for pivot in the key list and insert value before pivot value |

Llen Key

Get list length


3.4. Collection

A Redis collection is an unordered collection of type string. A collection member is unique , which means that duplicate data cannot appear in the collection.

Redis collections are implemented by hash tables, so the complexity of adding, deleting, and finding is O (1).

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.