Redis data types

Five data types of Redis:
 
    a.string (string)
 
String is the most basic type of redis, you can understand it as the exact same type as Memcached, a key corresponds to a value
        1. Single value single value
        2. set/get/del/append/strlen
        3.Incr/decr/incrby/decrby, must be a number to add or subtract
        4. getrange/setrange, getrange: Get the value within the specified range, similar to the relationship between...and, from zero to negative one means all
        5. setex (set with expire) key second value/setnx (set if not exist), setex: set the key with expiration time, dynamically set. setex key second value real value
        6. mset/mget/msetnx, mset: set one or more key-value pairs at the same time. mget: Get all (one or more) values ​​for the given key. msetnx: Simultaneously set one or more key-value pairs if and only if none of the given keys exist.
        7. getset (get first and then set), getset: set the value of the given key as value, and return the old value of the key. In simple words, get first and then set immediately
 
    2. Redis list (List)
 
Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list. Its bottom layer is actually a linked list
 
        1. Single value multi value
        2. lpush/rpush/lrange
        3. lpop/rpop
        4. lindex, get elements according to the index subscript (from top to bottom), get the elements in the list by index lindex key index
        5. llen
        6. lrem key deletes N values. * Delete 2 elements whose value is equal to v1 from left to right, and the returned value is the actual number of deletions * LREM list3 0 value, which means to delete all the given values. zero is all
        7. ltrim key starts the index and ends the index, intercepts the value of the specified range and then assigns it to the key. ltrim: Intercept the elements of the specified index range, the format is the key start index end index of the ltrim list
        8. rpoplpush source list destination list. Remove the last element of a list and add that element to another list and return
        9. lset key index value
        10. linsert key before/after value 1 value 2. Add a specific value before and after an existing value in the list
        11. Performance summary.
It is a string linked list, both left and right can be inserted and added;
If the key does not exist, create a new linked list;
If the key already exists, add the content;
If all values ​​are removed, the corresponding keys disappear.
The operation of the linked list is very efficient both at the head and at the tail, but if it is to operate on the middle elements, the efficiency is very bleak.
 
3. Redis collection (Set).
Redis's Set is an unordered collection of string type. It is implemented through HashTable
 
    1. sadd / smembers / sismember
    2. scard, get the number of elements in the set.
    3. srem key value deletes elements in the collection
    4. srandmember key An integer (several random numbers). * Randomly take 2 pieces from the set collection * If the maximum number is exceeded, all are taken out, * If the written value is a negative number, such as -3, it means that 3 pieces need to be taken out, but there may be duplicate values.
    5. The spop key is randomly popped from the stack.
    6. smove key1 key2 a value in key1 The function is to assign a value in key1 to key2.
    7. Mathematical set class: difference set: sdiff (items in the first set but not in any of the following sets). Intersection: sinter. Union: sunion.
 
4. Redis hash (Hash).
A Redis hash is a collection of key-value pairs. Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.
 
    1. The KV mode is unchanged, but V is a key-value pair
    2.  hset/hget/hmset/hmget/hgetall/hdel
    3. hlen
    4. hexists key The key of a certain value in the key
    5. hkeys / whale
    6. hincrby/hincrbyfloat
    7. There is no assignment in hsetnx, and it is invalid.
       
5. Redis ordered set Zset (sorted set).
 
Redis zset, like set, is also a collection of elements of type string, and does not allow duplicate members. The difference is that each element is associated with a fraction of type double. Redis sorts the members of the set from small to large through scores. The members of zset are unique, but the score can be repeated.
 
    1. zadd/zrange。withscores
    2. zrangebyscore key start score end score
 withscores
 (does not include
The role of limit is to return the limit
 limit how many steps to start subscripting
 
    3. zrem key The corresponding value value under a score, the function is to delete the element
Delete elements, the format is the value of the key item of zrem zset, the value of the item can be multiple
A corresponding value of zrem key score, which can be multiple values
 
    4. zcard/zcount key score interval/zrank key values ​​value, the function is to obtain the corresponding value of the subscript value/zscore key to obtain the score
zcard : Get the number of elements in the collection
zcount : Get the number of elements in the score interval, zcount key starts the score interval and ends the score interval
zrank: Get the subscript position of value in zset
zscore: get the corresponding score according to the value
 
    5. The value of zrevrank key values, the function is to obtain the subscript value in reverse order. Get the subscript index value in positive and reverse order
    6.zrevrange
    7. zrevrangebyscore key end score start score
zrevrangebyscore zset1 90 60 withscores Scores are reversed
 
    
Six. Redis key (key)
    1. keys *
    2. The name of the exists key, to determine whether a key exists
    3. move key db ---> the current library is gone and removed
    4. expire key seconds: set the expiration time for the given key
    5. ttl key to see how many seconds to expire, -1 means never expired, -2 means expired
    6. type key to see what type your key is

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027741&siteId=291194637