[Transfer] Introduction to five data types of edis

Overview:

Redis keys can use species data types: strings, hash tables, lists, sets, ordered sets . This article describes the use of these five data types in detail. The command introduction part of this article only lists the basic commands. For specific usage examples, you can refer to the official Redis documentation: Redis Commands

 

1. String type

 

String is the most basic data type in Redis, it can store any type of string, including binary data. Can be used to store mailboxes, JSONized objects, or even an image, with a maximum storage capacity of 512MB for a string . Strings are the basis of the other four types, and the difference from the other types is essentially just the way strings are organized.

basic commands

 

String manipulation

  1. SET assignment, usage:SET key value
  2. GET value, usage:GET key
  3. INCR increments numbers, only useful for keys of numeric type, equivalent to Java's i++ operation, usage:INCR key
  4. INCRBY increases the specified number, which is only useful for numeric keys, equivalent to Java's i+=3, usage: INCRBY key increment, which means that the key is incremented, and increment can be a negative number, which means decrease.
  5. DECR decrements numbers, only useful for numeric keys, equivalent to Java's i–, usage:DECR key
  6. DECRBY reduces the specified number, which is only useful for numeric keys, equivalent to Java's i-=3, usage: DECRBY key decrement, which means that the key is decrement, and decrement can be a positive number, which means increase.
  7. INCRBYFLOAT adds the specified floating point number, which is only useful for numeric keys. Usage:INCRBYFLOAT key increment
  8. APPEND appends a value to the tail, equivalent to "hello".append("world") in Java, usage:APPEND key value
  9. STRLEN gets the length of the string, usage:STRLEN key
  10. MSET sets the value of multiple keys at the same time, usage:MSET key1 value1 [key2 value2 ...]
  11. MGET gets the values ​​of multiple keys at the same time, usage:MGET key1 [key2 ...]

bit manipulation

  1. GETBIT gets the value (0/1) of the specified position of the binary bit of a key value, usage:GETBIT key offset
  2. SETBIT sets the value (0/1) of the specified position of the binary bit of a key value. Usage:SETBIT key offset value
  3. BITCOUNT gets the number of 1's in a range of binary representations of a key value, usage:BITCOUNT key [start end]
  4. BITOP This command can perform bit operation on multiple string type keys and store the result in the specified key. The operations supported by BITOP include: OR, AND, XOR, NOT , usage:BITOP OP desKey key1 key2
  5. BITPOS gets the position where the first bit value of the specified key is 0 or 1. Usage:BITPOS key 0/1 [start, end]

2. Hash type

 

The hash type is equivalent to HashMap in Java. Its value is a dictionary, which stores many key and value pairs. The value of each pair of keys and values ​​is a string type. In other words, the hash type cannot be nested with other types. type of data. A hash type key can contain up to 2 to the power of 32 - 1 fields.

 

basic commands

  1. HSET assignment, usage:HSET key field value
  2. HMSET assigns multiple fields at a time, usage:HMSET key field1 value1 [field2 values]
  3. HGET value, usage:HSET key field
  4. HMGET takes the values ​​of multiple fields at a time, usage:HMSET key field1 [field2]
  5. HGETALL takes the values ​​of all fields at once, usage:HGETALL key
  6. HEXISTS判断字段是否存在,用法:HEXISTS key field
  7. HSETNX当字段不存在时赋值,用法:HSETNX key field value
  8. HINCRBY增加数字,仅对数字类型的值有用,用法:HINCRBY key field increment
  9. HDEL删除字段,用法:HDEL key field
  10. HKEYS获取所有字段名,用法:HKEYS key
  11. HVALS获取所有字段值,用法:HVALS key
  12. HLEN获取字段数量,用法:HLEN key

3.列表类型

 

列表类型(list)用于存储一个有序的字符串列表,常用的操作是向队列两端添加元素或者获得列表的某一片段。列表内部使用的是双向链表(double linked list)实现的,所以向列表两端添加元素的时间复杂度是O(1),获取越接近列表两端的元素的速度越快。但是缺点是使用列表通过索引访问元素的效率太低(需要从端点开始遍历元素)。所以列表的使用场景一般如:朋友圈新鲜事,只关心最新的一些内容。借助列表类型,Redis还可以作为消息队列使用。

基本命令

  1. LPUSH向列表左端添加元素,用法:LPUSH key value
  2. RPUSH向列表右端添加元素,用法:RPUSH key value
  3. LPOP从列表左端弹出元素,用法:LPOP key
  4. RPOP从列表右端弹出元素,用法:RPOP key
  5. LLEN获取列表中元素个数,用法:LLEN key
  6. LRANGE获取列表中某一片段的元素,用法:LRANGE key start stop,index从0开始,-1表示最后一个元素
  7. LREM删除列表中指定的值,用法:LREM key count value,删除列表中前count个值为value的元素,当count>0时从左边开始数,count<0时从右边开始数,count=0时会删除所有值为value的元素
  8. LINDEX获取指定索引的元素值,用法:LINDEX key index
  9. LSET设置指定索引的元素值,用法:LSET key index value
  10. LTRIM只保留列表指定片段,用法:LTRIM key start stop,包含start和stop
  11. LINSERT像列表中插入元素,用法:LINSERT key BEFORE|AFTER privot value,从左边开始寻找值为privot的第一个元素,然后根据第二个参数是BEFORE还是AFTER决定在该元素的前面还是后面插入value
  12. RPOPLPUSH将元素从一个列表转义到另一个列表,用法:RPOPLPUSH source destination

4.集合类型

集合在概念在高中课本就学过,集合中每个元素都是不同的,集合中的元素个数最多为2的32次方-1个,集合中的元素师没有顺序的。

基本命令

  1. SADD添加元素,用法:SADD key value1 [value2 value3 ...]
  2. SREM删除元素,用法:SREM key value2 [value2 value3 ...]
  3. SMEMBERS获得集合中所有元素,用法:SMEMBERS key
  4. SISMEMBER判断元素是否在集合中,用法:SISMEMBER key value
  5. SDIFF对集合做差集运算,用法:SDIFF key1 key2 [key3 ...],先计算key1和key2的差集,然后再用结果与key3做差集
  6. SINTER对集合做交集运算,用法:SINTER key1 key2 [key3 ...]
  7. SUNION对集合做并集运算,用法:SUNION key1 key2 [key3 ...]
  8. SCARD获得集合中元素的个数,用法:SCARD key
  9. SDIFFSTORE对集合做差集并将结果存储,用法:SDIFFSTORE destination key1 key2 [key3 ...]
  10. SINTERSTORE对集合做交集运算并将结果存储,用法:SINTERSTORE destination key1 key2 [key3 ...]
  11. SUNIONSTORE对集合做并集运算并将结果存储,用法:SUNIONSTORE destination key1 key2 [key3 ...]
  12. SRANDMEMBER随机获取集合中的元素,用法:SRANDMEMBER key [count],当count>0时,会随机中集合中获取count个不重复的元素,当count<0时,随机中集合中获取|count|和可能重复的元素。
  13. SPOP从集合中随机弹出一个元素,用法:SPOP key

5.有序集合类型

有序集合类型与集合类型的区别就是他是有序的。有序集合是在集合的基础上为每一个元素关联一个分数,这就让有序集合不仅支持插入,删除,判断元素是否存在等操作外,还支持获取分数最高/最低的前N个元素。有序集合中的每个元素是不同的,但是分数却可以相同。有序集合使用散列表和跳跃表实现,即使读取位于中间部分的数据也很快,时间复杂度为O(log(N)),有序集合比列表更费内存。

基本命令

  1. ZADD添加元素,用法:ZADD key score1 value1 [score2 value2 score3 value3 ...]
  2. ZSCORE获取元素的分数,用法:ZSCORE key value
  3. ZRANGE获取排名在某个范围的元素,用法:ZRANGE key start stop [WITHSCORE],按照元素从小到大的顺序排序,从0开始编号,包含start和stop对应的元素,WITHSCORE选项表示是否返回元素分数
  4. ZREVRANGE获取排名在某个范围的元素,用法:ZREVRANGE key start stop [WITHSCORE],和上一个命令用法一样,只是这个倒序排序的。
  5. ZRANGEBYSCORE获取指定分数范围内的元素,用法:ZRANGEBYSCORE key min max,包含min和max,(min表示不包含min,(max表示不包含max,+inf表示无穷大
  6. ZINCRBY增加某个元素的分数,用法:ZINCRBY key increment value
  7. ZCARD获取集合中元素的个数,用法:ZCARD key
  8. ZCOUNT获取指定分数范围内的元素个数,用法:ZCOUNT key min max,min和max的用法和5中的一样
  9. ZREM删除一个或多个元素,用法:ZREM key value1 [value2 ...]
  10. ZREMRANGEBYRANK按照排名范围删除元素,用法:ZREMRANGEBYRANK key start stop
  11. ZREMRANGEBYSCORE按照分数范围删除元素,用法:ZREMRANGEBYSCORE key min max,min和max的用法和4中的一样
  12. ZRANK获取正序排序的元素的排名,用法:ZRANK key value
  13. ZREVRANK获取逆序排序的元素的排名,用法:ZREVRANK key value
  14. ZINTERSTORE计算有序集合的交集并存储结果,用法:ZINTERSTORE destination numbers key1 key2 [key3 key4 ...] WEIGHTS weight1 weight2 [weight3 weight4 ...] AGGREGATE SUM | MIN | MAX,numbers表示参加运算的集合个数,weight表示权重,aggregate表示结果取值
  15. ZUNIONSTORE计算有序几个的并集并存储结果,用法和14一样,不再赘述。

声明:原创文章,转载请注明出处,本文链接:http://qifuguang.me/2015/09/29/Redis五种数据类型介绍/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644629&siteId=291194637