Five types of data Redis articles 2-

redis support string, list, set, zset and hash five data types, but their operation is inconsistent .. The following syntax rough over again.

String type

Brief Description

  • The most basic data types, and as Memcached
  • Single-valued single value, the analogy of Java Map <String, String>.
  • Binary safe, redis of stringkey contain any data, such as jpg image content and sequence of the target can be.
  • In theory, string the value redis can be large, up to 512M.

Common Operations

  • set key value Definition / covering a string key
  • get key Gets the string value of the key
  • del key Delete key
  • appen key value Value append
  • strlen key Length value
  • exists key Determine whether there is a key
  • move key n The key is to migrate the current library n number database
  • expire key nKey expiration time is set to n seconds
    after expired get keyand keys *will not see, equivalent deleted
  • ttl kycSee how long expired target key
    -1 means never expires (the default), - 2 representatives have expired
  • type key Check the type of bond, such as string
  • incr key The values ​​are +1 only valid for the numeric value
  • decr key For values ​​-1, only valid numeric value
  • incrby key n Value of + n, only valid numeric value
  • decrby key n Value of -n, only valid numeric value
  • getrange key 0 3Gets the value of the specified range (the first four characters), substr i.e., getrange key 0 -1retrieves all
  • setrange key .. Value specified interval
  • setex key n value Defines a key value and simultaneously set the expiration time (n seconds value)
  • setnx key value If the key does not exist only to create that set if not exist, avoid covering
  • mset / mget / msetnx also define / obtain a plurality of keys
    mset k1 v1 k2 v2 k3 v3 mget k1 k2 k3 msetnx k3 v3 k4 v4 #注:k3已存在,本次失败,k4也不会创建
  • ...

List type

Brief Description

  • Single-value multi-value, the analogy of Java Map <string, List>
  • Is a string list, both sides can operate
  • If the key does not exist, create a new list, then there is an additional element
  • If the values ​​of all the plurality of value is removed, the corresponding key is automatically removed

Common Operations

  • lpush / rpush / lrange define a list and access key
    lpush l1 v4 v3 v2 v1 #从左push rpush l1 v5 v6 v7 v8 #从右push lrange l1 0 -1 #获取l1值 lrange l1 0 4 #范围获取l1值
  • lpop / rpop key popped from the left / right side (pull out and return)
  • llen key The number of list elements
  • lindex key value String indexOf function of analogy
  • ...

Set Types

Brief Description

  • List and is not very different, single-value multi-value, the analogy of Java Map <String, Set>
  • No repeat disorder, the bottom is Hashtable

Common Operations

  • sadd key values ​​.. Set key definitions
  • smembers key traversal key set
  • sismember key value determines whether there is a certain value
    madd s1 1 2 3 2 3 smembers s1 sismember s1 1 sismember s1 x
  • scard key set number of sets of elements
  • srem key value Remove a value
  • spop key A random pop
  • srandmember key n A stack of n random
  • ...

Zset (ordered set) type

Brief Description

  • That Sorted Set, and Set as disorderly no duplication, analogy Java's Map <String, ScoreSet>
  • Each element is associated will give a double type of score Score
  • zset member elements can not be repeated, and this score can be repeated
  • That sort by score value of each member from small to large row
  • Some scenes are used for statistics

Common Operations

  • zadd/zrange
zadd z01 60 C 70 B 80 A 90 S
zrange z01 0 -1
zrange z01 0 -1 withscores
  • ...

Hash type

Brief Description

  • Single-value multi-value, analog in JavaMap<String,<String, Object>
  • It can be defined as an object in the redis

Common Operations

  • hset / hget / hmset / hmget / hgetall / hdel definition, get, delete
hset user0 id 1 nane hwc1
hget user0 id
hgetall user0
hdel user0 id
  • hlen key The number of "object attribute"
  • hexists key0 key1 The existence of a "property"
  • hkeys/hvals key0 Traversing the "object" attribute / value
  • ...

Guess you like

Origin www.cnblogs.com/noodlerkun/p/11487651.html