Redis Key-Value data types and basic operations

Source: Internet
Author: User
Tags add numbers

The key value data types supported by Redis so far are as follows:

Strings (String)

Hash (MAP)

Listing (list)

Collection (sets)

Ordered set (sorted sets)

1. string type

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

The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.

The string type is the most basic data type of Redis, and a key can store up to 512MB.

String command:

Assign and value set key Value/get key

>set name Zhangzhi

Ok

>get Name

"Zhangzhi"

The INCR command increments the key value when the string value is an integer
>set a 1

Ok
>INCR A

(integer) 2

The default key value is 0 when the key does not exist
>INCR b

(integer) 1

Incrby and incr are similar, but you can specify an increased value once
>incrby C 10

(integer) 10
DECR and Decrby reduce integers, incrbyfloat increments the specified floating-point number, append appends a value to the tail, and the value is specified if the key does not exist
>set D Hello

Ok
>append D World

(integer) # append command return string length "HelloWorld" Length of 10

Strlen key Gets the string length
>strlen D

(integer) 10
simultaneous Acquisition (Mget) /Set (mget) multiple key values

>mset a Valuea b valueb

>mget a B

2. Hashes Hash

The key value of a hash is also a dictionary structure that stores the mappings of fields and field values, special societies, and storage objects. But the field value can only be a string, and the hash type cannot nest other data types.

Example:

>hset user:1 name Zhang #赋值

>hget user:1 name #取值

>hmset user:1 name Zhangzhi age address Beijng #赋多值

>hmget user:1 name Age #根据keys获取多个值

>hgetall user:1 #获取全部值

>hexists user:1 Address #判断字段是否存在

>hsetnx user:1 Address Beijing # Assignment When a field does not exist

>hdel user:1 Age #删除字段

>hincrby user:1 Age 1 # Add numbers, like Incrby, hash no incr

>hkeys user:1 #获取字段名

>hvals user:1 #获取字段值

>hlen User:1 # Get the number of fields

3. List (Lists)
The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head of the list (to the left) or to the tail (to the right).
The Lpush command inserts a new element into the list header, while the Rpush command inserts a new element into the end of the list. When you execute one of these commands against an empty key, a new table is created. The list can store up to 2 of 32 parties – 1 elements (4294967295, each of which can store more than 4 billion).
Example:
>lpush list:1 a #创建列表list: 1 and insert a

>rpush list:1 b #右边插入b

>lrange list:1 0 10 # Gets the List:1 index from 0 to 10 of the meta

>lpop list:1 #左边弹出元素

>rpop list:1 #右边弹出元素

>llen list:1 # Number of elements in the list

>lrem list:1 2 A #删除列表中指定的值 2 for quantity

>linde list:1 0 # Gets the element value for the specified index

>lset list:1 0 2 #设置指定索引的元素值

>ltrim List1 2 4 #删除指定索引范围之外的所有元素

>linsert List1 before 4 #Linsert commands are used to insert elements before or after the elements of a list. When the specified element does not exist in the list, no action is taken. When the list does not exist, it is treated as an empty list, and no action is taken. If key is not a list type, an error is returned.
>rpoplpush list1 list2 #rpoplpush command is used to remove the last element of the list and add the element to another list and return.

4. Set (SET)

Set is an unordered collection of type string. The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1).
Example:
>sadd set:1 1 2 #已经存在集合中的元素忽略

>srem set:1 2 3

>smembers set:1

>sismember set:1 4 #判断元素是否在集合中

>sdiff set:1 Set:2 #集合间差集

>sinter set:1 Set:2 # set intersection

>sunion set:1 set:2 # Set Merge Set

5. Ordered set (Sorted sets)

The Redis ordered collection is similar to the Redis collection, where the members of each ordered set are associated with a score that is used to rank the members of an ordered set by the lowest score.
Example:
>zadd zset:1 0 Redis

>zadd zset:1 1 Monogdb

>zadd zset:1 2 MySQL

>zrangebyscore zset:1 0 1000

>zscore zset:1 MySQL #获取元素分数

>zrange zset:1 0-1 Withscores

Redis Key-Value data types and basic 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.