Redis supports 5 types of data

Source: Internet
Author: User
Tags new set ranges time in milliseconds

Redis currently supports 5 types of data, namely

String (String)

List (lists)

Hash (dictionary)

Set (SET)

Sorted set (ordered set)



Redis data types

1. String type

Set key value Setting Key=value

The value corresponding to the GET key or key

GETRANGE key start end gets the substring of the string stored in a key

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

Getbit key offset returns the offsets of the string values stored in the keyed value

MGET Key1 [Key2 ...] Get all the values of the given key

Setbit key offset value Sets or clears the bit at which the string value offsets stored in the key

Setex Key seconds value key expires when setting values

The value of SETNX key value sets the key only if the key does not exist

SETRANGE key offset value overrides part of the string from the specified key

STRLEN key Gets the length of the value stored in the key

MSET key value [key value ...] Set multiple keys and multiple values

Msetnx key value [key value ...] sets multiple keys multiple values, only when there are no keystrokes

Psetex key milliseconds value sets the millisecond and expiry time of the key

INCR key increments the integer value of the key one time

Incrby key increment the integer value of the key increment by the given number

Incrbyfloat key increment a floating-point value that increments the key by a given number

DECR key decrements the key once the integer value

Decrby key Decrement The integer value of the key descending by a given number

APPEND key value append value to a key



Operations Management Commands

DEL key If a delete key exists

DUMP Key returns the serialized version of the value stored in the specified key

EXISTS Key This command checks if the key exists

EXPIRE key seconds the expiry time of the specified key

Expireat key timestamp The specified key expiration time. Here, time is in the UNIX timestamp format

Pexpire key milliseconds setting key expires in milliseconds

Pexpireat key Milliseconds-timestamp set key in Unix timestamp specified as milliseconds expires

Keys pattern finds all keys that match the specified pattern

Move key DB moves key to another database

PERSIST key to remove expired keys

Pttl key gets the expiration key for the remaining time in milliseconds.

The TTL key gets the remaining time for the key to expire.

Randomkey returning random keys from Redis

RENAME key Newkey Change the name of the key

Renamenx key Newkey Rename key if the new key does not exist

Type key returns the value of the data type stored in the key.



Operation Use Example

Redis 127.0.0.1:6379> set Baidu Http://www.baidu

Ok

Redis 127.0.0.1:6379> append Baidu. com

(integer) 20

Redis 127.0.0.1:6379> get Baidu

"Http://www.baidu.com"

Redis 127.0.0.1:6379> Set Visitors 0

Ok

Redis 127.0.0.1:6379> incr Visitors

(integer) 1

Redis 127.0.0.1:6379> incr Visitors

(integer) 2

Redis 127.0.0.1:6379> Get Visitors

"2"

Redis 127.0.0.1:6379> Incrby Visitors 100

(integer) 102

Redis 127.0.0.1:6379> Get Visitors

"102"

Redis 127.0.0.1:6379> type Baidu

String

Redis 127.0.0.1:6379> Type Visitors

String

Redis 127.0.0.1:6379> ttl Baidu

(integer)-1

Redis 127.0.0.1:6379> rename Baidu Baidu-site

Ok

Redis 127.0.0.1:6379> get Baidu

(nil)

Redis 127.0.0.1:6379> Get Baidu-site

"Http://www.baidu.com"






2. List (lists)

The Redis list is a simple list of strings that can be likened to the std::list in C + +, simply a chain list or a queue. You can add elements to the Redis list from the head or tail. The maximum length of the list is 2^32-1, which means that more than 4 billion elements are supported per list.


The implementation of Redis list is a doubly linked list, which can support reverse lookup and traversal, but it is more convenient to operate, but it brings some additional memory overhead, and many implementations within Redis, including sending buffer queues, are also used in this data structure.


Application Scenarios

Redis list has many applications and is one of the most important data structures for Redis, such as Twitter's watchlist, fan list, etc., which can be implemented using the REDIS list structure. For example, some applications use the Redis list type to implement a simple lightweight message queue, producer push, consumer Pop/bpop.


Related commands

Blpop

Blpop key1 [Key2] timeout fetches and gets the first element in the list, or blocks until there are available

Brpop

Brpop key1 [Key2] timeout fetches and gets the last element in the list, or blocks until there are available

Brpoplpush

Brpoplpush Source Destination timeout pops a value from the list, pushes it to another list and returns it, or blocks until there is a usable

LINDEX

LINDEX key index gets the corresponding element from a list of its indexes

Linsert

Linsert Key before| After pivot value inserts an element after or before other elements in the list

Llen

Llen key Gets the length of the list

Lpop

Lpop key gets and fetches the first element in the list

Lpush

Lpush key value1 [value2] preceded by a list of one or more values

Lpushx

Lpushx key value is preceded by a list of values, only if the list exists

Lrange

Lrange key start stop gets various elements from a list

Lrem

Lrem Key count value removes an element from the list

LSET

LSET Key index value sets the values of an element in a list of indexes

LTRIM

LTRIM key start Stop trim list to specified range

Rpop

Rpop key takes out and gets the last element in the list

Rpoplpush

Rpoplpush source destination Deletes the list of the last element, attaches it to another list, and returns it

Rpush

Rpush key value1 [value2] Add one or more values to the list

Rpushx

Rpushx key value Adds a list of values only if the list exists



Using the example


Redis 127.0.0.1:6379> Lpush List1 Redis

(integer) 1

Redis 127.0.0.1:6379> Lpush List1 Hello

(integer) 2

Redis 127.0.0.1:6379> Rpush List1 World

(integer) 3

Redis 127.0.0.1:6379> Llen List1

(integer) 3

Redis 127.0.0.1:6379> lrange list1 0 3

1) "Hello"

2) "Redis"

3) "World"

Redis 127.0.0.1:6379> Lpop List1

"Hello"

Redis 127.0.0.1:6379> Rpop List1

"World"

Redis 127.0.0.1:6379> lrange list1 0 3

1) "Redis"




3. Hash (dictionary, hash table)

Similar to the dict type in C # or the Hash_map type in C + +.


Redis hash corresponds to value inside the actual is a hashmap, actually there will be 2 different implementations, this hash of the members relatively young Redis in order to save memory will be similar to a one-dimensional array of compact storage, rather than the real hashmap structure, The encoding of the corresponding value Redisobject is Zipmap, and when the number of members increases, it automatically turns into a true hashmap, at which time encoding is HT.


Application Scenarios

Assume that there are multiple users and corresponding user information, can be used to store the user ID as key, the user information serialized into such as JSON format as value to save.






Related commands

Hdel

Hdel key Field[field ...] Delete one or more domains of an object, the non-existent property will be ignored

Hexists

Hexists key field to see if the object exists in this domain

Hget

Hget key field gets the value of the field attribute in the object

Hgetall

Hgetall key gets all domains and values of the object

Hincrby

Hincrby key field value increments the values of the specified field in the object by the given value, the atomic increment operation, which can only be an integer property value.

Hincrbyfloat

Hincrbyfloat key field increment adds the value of the specified field in the object to the given floating-point number

Hkeys

Hkeys Key gets all the property fields of the object

Hvals

Hvals Key gets all the property values of an object

Hlen

Hlen Key Gets the total number of all property fields for an object

Hmget

Hmget key Field[field ...] Gets the value of one or more specified fields of an object

Hset

Hset key field value sets the values of the specified fields for the object

Hmset

Hmset key field value [field value ...] Sets the value of one or more fields in an object at the same time


The HSETNX:HSETNX command is used to store the hash of the key values in the field, only if the field does not exist.

If the key does not exist, the new key is created by the hash. If the field already exists, the operation has no effect.

Hsetnx key field value sets the values of fields only if the specified field does not exist for the object

HSCAN

HSCAN key cursor [MATCH pattern] [count count] similar to scan command



Using the example

127.0.0.1:6379> Hset Person name Jack

(integer) 1

127.0.0.1:6379> Hset Person Age 20

(integer) 1

127.0.0.1:6379> hset person Sex famale

(integer) 1

127.0.0.1:6379> hget Person Name

"Jack"

127.0.0.1:6379> Hgetall Person

1) "Name"

2) "Jack"

3) "Age"

4) "20"

5) "Sex"

6) "Famale"

127.0.0.1:6379> Hkeys Person

1) "Name"

2) "Age"

3) "Sex"

127.0.0.1:6379> hvals Person

1) "Jack"

2) "20"

3) "Famale"




4. Set (SET)

Can be understood as a list of values that are not duplicated, similar to the set concept in the field of mathematics, and Redis also provides operations such as intersection, set, and difference sets for a set.

The internal implementation of set is a value that is always null hashmap, which is actually calculated by hashing the way to fast weight, which is also set to provide a judge whether a member is within the cause of the collection.


Application Scenarios

The functionality provided by Redis set externally is a list-like feature, except that set is automatically weight-saving, and set is a good choice when you need to store a list of data and you don't want duplicate data. and set provides an important interface to determine whether a member is within a set set, which is not available in list.


Or in the microblogging application, each user concerned about the existence of a collection, it is easy to achieve the two people with a common friend function.


Related commands

Sadd

Sadd key member [member ...] add one or more elements to the collection (set)

Sacrd

SCard Key gets the number of elements inside the collection

Sdiff

Sdiff key [key ...] gets the element for which the queue does not exist

Sdiffstore

Sdiffstore destination key [key ...] gets the element that the queue does not exist and stores it in a key result set

SINTER

SINTER key [key ...] gets the intersection of two sets

Sinterstore

Sinterstore destination key [key ...] gets the intersection of two sets and stores it in a collection

Sismember

Sismember Key member determines that a given value is a member of a collection

Smembers

Smembers Key gets all the keys inside the collection

Smove

Smove Source Destination member move a key inside the collection to another collection

SPOP

SPOP key [Count] gets and deletes an element inside a collection

Srandmember

Srandmember key [Count] Gets an element randomly from the collection

Srem

Srem Key member [member ...] removes one or more elements from the collection and the nonexistent elements are ignored

Sunion

Sunion key [key ...] add multiple set elements

Sunionstore

Sunionstore destination key [key ...] merges the set element and stores the result in a new set

Sscan

Sscan key cursor [MATCH pattern] [count count] element inside iteration Set



Using the example

Redis> sadd MySet "Hello"

(integer) 1

Redis> sadd MySet "World"

(integer) 1

Redis> smembers MySet

1) "World"

2) "Hello"

Redis> sadd MySet "one"

(integer) 1

Redis> sismember MySet "one"

(integer) 1

Redis> Sismember MySet "the other"

(integer) 0




A typical use case for using a collection data structure is the implementation of a friend list:

Redis 127.0.0.1:6379> sadd Friends:leto Ghanima Paul Chani Jessica

(integer) 4

Redis 127.0.0.1:6379> Sadd Friends:duncan Paul Jessica Alia

(integer) 3

Redis 127.0.0.1:6379> sismember Friends:leto Jessica

(integer) 1 #不管一个用户有多少个朋友, we are able to efficiently (O (1) Time complexity) identify user X is a friend of user Y

Redis 127.0.0.1:6379> Sismember friends:leto Vladimir

(integer) 0

Redis 127.0.0.1:6379> sinter Friends:leto friends:duncan #我们可以查看两个或更多的人是不是有共同的朋友

1) "Paul"

2) "Jessica"

Redis 127.0.0.1:6379> sinterstore friends:leto_duncan friends:leto Friends:duncan # can store results in a new keyword

(integer) 2


5. Sorted set (ordered set)

The Redis ordered set is similar to a Redis collection, and the difference is that it adds a function that the collection is ordered. Each member of an ordered collection has fractions that are used for sorting.


The time complexity of adding, deleting, and testing Redis ordered collections is O (1) (fixed time, regardless of the number of elements contained within it). The maximum length of the list is 2^32-1 elements (4294967295, more than 4 billion of each element's collection).


Redis sorted set internal use HashMap and jump Table (skiplist) to ensure the storage and ordering of data, HashMap in the member to score mapping, and the jumping table is all the members, sorted by HashMap in the score , the use of the structure of the jumping table can obtain a relatively high efficiency of finding, and it is relatively simple to implement.


Usage Scenarios

The usage scenario for Redis sorted set is similar to set, except that the set is not automatically ordered, and the sorted set can be ordered by the user with an additional priority (score) parameter, and is inserted in an orderly, automatic sort. When you need an ordered and non-repeating collection list, you can choose sorted set data structures, such as the public Timeline of Twitter, which can be stored as score in the publication time, which is automatically sorted by time.


Another example is the user's point leaderboard needs can be achieved through an ordered set. There are also the use of the list to implement lightweight Message Queuing, in fact, can also be implemented through the sorted set of priority or by weight of the queue.


Related commands

Zadd

Zadd key Score1 member1 [Score2 member2] Add one or more members to an ordered collection, or if it already exists update its score

Zcard

Zcard Key gets the number of ordered collection members

Zcount

Zcount Key min Max calculates the fraction of an ordered set member with a given value range

Zincrby

Zincrby key increment member increase the member's score in an ordered set

Zinterstore

Zinterstore destination Numkeys key [key ...] multiple cross-sorted collection, and stores a new ordered collection of keys.

Zlexcount

Zlexcount Key min Max calculates the number of ordered collection members between a given dictionary range

Zrange

Zrange key start stop [Withscores] returns an ordered collection of member ranges (from low to high) by index

Zrangebylex

Zrangebylex key min Max [LIMIT offset Count] Returns an ordered collection of member scopes (by dictionary scope)

Zrangebyscore

Zrangebyscore key min Max [withscores] [LIMIT] returns the ordered set key, where all score values are between Min and Max (including equals min or Max), and the ordered set members are incremented by the score value (from small to large) Order

Zrank

Zrank Key member determines the ordered collection of members in the index

Zrem

Zrem key member [member ...] removes one or more members from an ordered collection, the nonexistent member is ignored

Zremrangebylex

Zremrangebylex Key min Max deletes an ordered collection of all members between a given dictionary range

Zremrangebyrank

Zremrangebyrank key Start stop removes an ordered collection of all members within the given index

Zremrangebyscore

Zremrangebyscore Key min Max deletes an ordered set of all members within a given fraction

Zrevrange

Zrevrange key start stop [Withscores] returns an ordered collection of member ranges by index, sorted by fractions, from high score to low score

Zrevrangebyscore

Zrevrangebyscore key Max min [withscores] Returns an ordered collection of member scopes to socre sort from high to low

Zrevrank

Zrevrank Key member determines the index of an ordered set member, sorted by fractions, from high to low

Zscore

Zscore Key member gets the associated fraction of a given member in an ordered set

Zunionstore

Zunionstore destination Numkeys key [key ...] add more than one set sort, the resulting sorted collection is stored in a new key

Zscan

Zscan key cursor [MATCH pattern] [count Count] increment iterates the sorted element set and related fractions



Using the example

Redis 127.0.0.1:6379> Zadd DBS

(integer) 1

Redis 127.0.0.1:6379> zadd DBS 98 memcached

(integer) 1

Redis 127.0.0.1:6379> zadd dbs in MongoDB

(integer) 1

Redis 127.0.0.1:6379> Zadd DBS Leveldb

(integer) 1

Redis 127.0.0.1:6379> Zcard DBS

(integer) 4

Redis 127.0.0.1:6379> Zcount DBS 10 99

(integer) 3

Redis 127.0.0.1:6379> Zrank DBS Leveldb

(integer) 1

Redis 127.0.0.1:6379> Zrank DBS Other

(nil)

Redis 127.0.0.1:6379> zrangebyscore DBS 98 100

1) "Memcached"

2) "Leveldb"

3) "MongoDB"

4) "Redis"





This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1868979

Redis supports 5 types of data

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.