Five types of data presentation redis

redis five data types

1. string (string)

Features:

  • A bond can store 512MB
  • The string type is binary safe and can store any data, such as jpg image or serialized objects

format:

存--set key value [EX seconds] [PX milliseconds] [NX|XX]

EX seconds: The key expiration time set to secondsseconds. Performing SET key value EX secondseffect equivalent to execution SETEX key seconds value.

PX milliseconds: The key expiration time is set to millisecondsmilliseconds. Performing SET key value PX millisecondseffect equivalent to execution PSETEX key milliseconds value.

NX: Only when the bond is absent, fishes key set operation. Performing SET key value NXeffect equivalent to performingSETNX key value

XX : Only when key already exists, fishes key set operation.

取--get key

Basic operation:

127.0.0.1:6379> set name panlifu
OK
127.0.0.1:6379> get name
"panlifu"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)

2. hash (hash)

Features:

  • Is a key (key -> value) pairs.
  • Each hash key 232-1 may be stored on (over 40 billion).
  • Field is a string type and the value of the mapping table, hash particularly suited for storing objects

format:

存--HMGET key field [field ...]
取--HMGET key field [field ...]

Basic Operations

# 存 myhash 类似一个hash值,field1类似一个键,hello就是对应值,field2类似第2个键,world就是对应的值
127.0.0.1:6379> HMSET myhash field1 "hello" field2 "world"
OK
# 取  必须有hash值  加  键才能取到值
127.0.0.1:6379> HMGET myhash field1
1) "hello"
127.0.0.1:6379> HMGET myhash field2
1) "world"
# 删除
127.0.0.1:6379> del myhash
(integer) 1
127.0.0.1:6379> HMGET myhash field2
1) (nil)
127.0.0.1:6379> HMGET myhash field1
1) (nil)

3. list (list)

Feature

  • Insertion order sort

format

存---LPUSH key value [value ...]
取--LRANGE key start stop

Basic Operations

127.0.0.1:6379> lpush runoob redis
(integer) 1
127.0.0.1:6379> lpush runoob mongodb
(integer) 2
127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
127.0.0.1:6379> LRANGE runoob 1 3
1) "mongodb"
2) "redis"
127.0.0.1:6379> LRANGE runoob 0 3
1) "rabitmq"
2) "mongodb"
3) "redis"
127.0.0.1:6379> LRANGE runoob 0 4
1) "rabitmq"
2) "mongodb"
3) "redis"

4. set (collection)

Feature

  • Disorderly
  • Collection is implemented by hash, so add, delete, search complexity is O (1)

format

存--sadd key member [member ...]
取--SMEMBERS key

Basic Operations

127.0.0.1:6379> sadd runoob redis
(integer) 1
127.0.0.1:6379> SADD runoob mongodb
(integer) 1
127.0.0.1:6379> SADD runoob rabitmq
(integer) 1
127.0.0.1:6379> SADD runoob rabitmq
(integer) 0
127.0.0.1:6379> SMEMBERS runoob
1) "mongodb"
2) "redis"
3) "rabitmq"

注意:以上实例中 rabitmq 添加了两次,但根据集合内元素的唯一性,第二次插入的元素将被忽略。

集合中最大的成员数为 232 - 1(4294967295, 每个集合可存储40多亿个成员)。

5. zset (sorted set: ordered collection)

Feature

  • And also set the same set of string type elements, and does not allow duplicate members
  • Each element will be associated with a type of score double. It is to redis from small to large order of member of the set by scores
  • Zset member is unique, but it can be repeated scores

format

存--ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  • XX : only update existing members, not add new members.
  • NX : do not update the members present. Only add new members.
  • CH : modify the return value of total membership changes occur, the original is returned to the total number of newly added member (CH are changed meaning). The changed elements are newly added members , existing members of the updated scores . So specified in the command members have the same score will not be counted. Note: Under normal circumstances, ZADDthe return value only counts the number of newly added members.
  • INCR : When ZADDthis option is specified, the operation member is equivalent ZINCRBY command, fractional increments members operate.
取--ZRANGE key start stop [WITHSCORES]

Basic Operations

127.0.0.1:6379> zadd runoob 0 redis
(integer) 1
127.0.0.1:6379> zadd runoob 0 mongodb
(integer) 1
127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 1
127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 0
127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabitmq"
3) "redis"

Each data type scenarios

Types of Brief introduction characteristic Scenes
String (String) Binary Security May contain any data, such as jpg image or a sequence of objects, a maximum bond can store 512M ---
Hash (dictionary) A collection of key-value pairs, namely the type of programming language Map Suitable for storing objects, and may update the database as an attribute, as in a modification only one attribute value (in the Memcached need to remove the entire string deserialized into objects and then finished modified sequence of memory back) Store, read, modify user attributes
List (list) List (doubly linked list) Deletions fast, a certain period of operation of the API elements 1, updates ranking functions (such as circle of friends timeline) 2, message queue
Set (collection) Hash table implementation, the elements do not repeat 1, add, delete, search complexity is O (1) 2, provided for the intersection of the collection, and union, difference and other operations 1, 2 mutual friend, the use of unique, independent ip statistics all access to the site 3, when a friend recommended, according to the intersection of tag, is greater than a certain threshold can recommend
Sorted Set (ordered set) Set a weight increase of weight parameters score elements, the elements ordered by score When data is inserted into the collection, sorting has natural 1, 2 charts, message queues with weights

Several major features of redis

  1. Supports multiple databases, each database data is isolated and can not be shared, based on the features of stand-alone only if there is no database of the cluster concept

  2. redis storage server is a dictionary structure, it is actually provided for storing a plurality of dictionary data. The client can specify which data is stored in the dictionary. This is our well-known example of a relationship in the database can be created similar to multiple databases, so it can be one of each dictionary are understood as a separate database.

  3. Each database is a named from outside incrementing number starting at 0, Redis default supports 16 database (more can be supported by the configuration file, no limit), this number can be modified by configuring databases. Redis establish client and will automatically select the database connection is 0, but you can always use the SELECT command to replace the database, the database To select No.1:

    redis> SELECT 1
    OK
    redis [1] > GET foo
    (nil)
  4. However, these figures named in a database and differ with our understanding of the database. First name Redis does not support custom databases, each database named with numbers, which the developer must own records database which stores the data.

  5. In addition Redis does not support setting a different password for each database access, so a client can either access the entire database, or even a database did not have permission to access.

  6. The most important thing is not completely isolated between multiple databases, such as FLUSHALL command to clear all data in one instance Redis database. Taken together, these databases is more of a namespace, is not suitable for storing data of different applications. Such data number 0 may be used an application database stores production environment, using the data stored in the database No. 1 in the test environment, but not suitable for using the data stored in a database application No. 0 A and B using the database application data # 1, except applications should use different Redis instance store data. Since Redis very lightweight, occupying an empty Redis instance intrinsic only about 1M, so do not worry multiple instances extra Redis take up a lot of memory.

Guess you like

Origin www.cnblogs.com/plf-Jack/p/11025105.html