Redis data types and operations

"" Java technician "Growth Manual", contains frames, storage, search, optimization, distributed and other essential knowledge, are collected in GitHub JavaEgg , N line Internet development skills necessary arsenal, welcomed the guidance

Redis Introduction

Redis: REMOTE DICTIONARY Server (remote dictionary server).

Redis is a full free open source (BSD license), the data structure stored in system memory, which can be used as a database, caching, and messaging middleware . As a general high-performance (key / value) distributed in-memory database, based on memory to run and support the persistence of NoSQL databases, it is one of the most popular NoSql current database, also known as a data structure server


Redis Introduction

redis is an open source, written in C, support network interactions, can also be based on the persistence of memory Key-Value database.

redis is a key-value storage system. And Memcached Similarly, it supports relatively more stored value type, comprising a string (string), List (list), SET (set), zset (sorted set - ordered set) and hash (hash type) . These data types are supported push / pop, add / remove and on the intersection and union, and difference richer operation, and these operations are atomic. On this basis, redis support a variety of different ways of sorting. Like with memcached, in order to ensure efficiency, the data is cached in memory. Redis difference is periodically updated in the data written to disk or to modify the operation of writing additional log file, and on this basis realize the master-slave (master-slave) synchronization, it may also be seen as Redis a server data structures.

Redis is a key-value high-performance database . redis appears, to a large extent compensate for the lack of such memcached key / value store, it can play a very good complement to relational database in some situations. It provides Java, C / C ++, C #, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients, very easy to use.

Redis supports master-slave synchronization. Data can be synchronized from the primary server to an arbitrary number from the server, the server may be associated with the primary server from another server. This allows the tree Redis single executable copy. Intentionally or unintentionally, can save the data write operation. Since the full implementation of the publish / subscribe mechanism, so that the database synchronization from a tree anywhere, can subscribe to a channel and receive the full message master server publishing record. Synchronization scalability and redundancy data useful for a read operation.

redis official website address is very easy to remember, is redis.io. (Domain name suffix io ​​domain name owned by the state, is the british Indian Ocean territory, namely the British Indian Ocean Territory) Currently, Vmware in financing the development and maintenance projects redis

All Redis data are stored in memory, and then from time to time saved to disk (this is known as "semi-persistent mode") by asynchronously; can also change each time data is written to a append only file (aof) inside (this is known as "full-persistent mode"). This is two kinds redis provide persistent way, RDB (Redis DataBase) and AOF (Append Only File).


Redis Features

Redis is an open source, advanced key-value store, and used to build high-performance, the perfect solution for scalable Web applications.

Redis inherited from its many competitive three main features to come:

  • Redis databases entirely in memory, using disk only for persistence.

  • Compared to many key data storage, Redis has a wealth of data types.

  • Redis can copy the data from the server to an arbitrary number.


Redis advantage

  • Exceptionally fast: Redis very fast, able to perform a collection of about 110,000 per second, about 81000+ records per second. SET operation 110,000 times per second, GET operation 81000 times per second, sites generally use Redis as a caching server .
  • Support for rich data types : Redis supports most developers already know, like lists, sets, ordered set, hash data type. This makes it very easy to solve a variety of problems, because we know what issues can be dealt with by its data type better.
  • Operations are atomic : all Redis operations are atomic, which guarantees the value of the Redis server if two clients simultaneously access will be updated.
  • MultiUtility tool: Redis is a versatile utility that can be many such as: any Web application sessions, web page hits such as: caching, messaging queue using (Redis native support publish / subscribe), in the application, such as transient data;


redis usage scenarios

  • N latest data fetch operations of
  • Application of charts, operations take TOP N
  • Need to accurately set the expiration time of application
  • Timers, counters application
  • Uniq operation, obtaining a certain time all the data duplication value
  • Real-time systems, anti-spam system
  • Pub / Sub build real-time messaging system
  • Construction of the queue system
  • Cache


installation

$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6
$ make

The new version of the compiled file in the src (before the bin directory) to start the server

$ src/redis-server

Start the client

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

Redis data types Manual

String (String)

redis string is the most basic type, you can be understood as type of exactly the same with Memcached, a key corresponding to a value.

The string type is binary safe. Redis meaning of string can contain any data. Such as jpg image or serialized object.

Redis dynamic string is a string, the string can be modified, similar to the internal structure of the ArrayList Java implemented, using the redundant pre-allocated space in a manner to reduce the frequency allocation of memory, as shown, the inner current string actually allocated space capacity generally higher than the actual string length len. When the string length is less than 1M, the conventional expansion space is doubled, if more than 1M, when only one expansion space 1M plurality of expansion. Note that the maximum length of the string 512M.

redis-string.jpg

Hash (dictionary)

Redis hash is a collection of key-value pairs. KV usual path, but V is a key-value pair.

Redis hash field is a string type and the value of the mapping table, hash is particularly suited for storing objects.

Redis dictionary equivalent Java language inside the HashMap, it is disorderly dictionary, the internal implementation of Java HashMap with the structure is the same, the same two-dimensional array + linked list structure. When the position of the first array dimension hash collision, the collision of the elements will be connected in series using a linked list.

redis-hash.jpg

List (list)

Redis list is a simple list of strings, sort insertion order. You can add a head guide elements of the list (on the left) or tails (to the right).

It is actually a bottom chain,

Redis list of equivalent Java language inside the LinkedList, note that it is a linked list instead of an array. This means that the list of insert and delete operations are very fast, the time complexity is O (1), but the index position is very slow, time complexity is O (n)

Redis list queue structure often used for asynchronous use. The required structure snooze task serialized into a string Redis list, another thread to process the data from the polling list

To the right into the left: queue
> rpush books python java golang 
(integer) 3 
\> llen books
 (integer) 3 
\> lpop books
 "python" 
\> lpop books
 "java" 
\> lpop books 
"golang" 
\> lpop books 
(nil)
Into the right side of the right: Stack
> rpush books python java golang
 (integer) 3
 \> rpop books 
"golang" 
\> rpop books 
"java" 
\> rpop books 
"python"
 \> rpop books
 (nil)

Set (collection)

Redis is a string Set the type of unordered collection. It is achieved through HashTable, the equivalent Java language inside the HashSet, it is the only internal key-value pairs of unordered. Its internal implementation is equivalent to a special dictionary, a dictionary of all the value is a value NULL.

zset (sorted set: an ordered collection)

Redis zset and is set as a collection of string type elements, and does not allow duplicate members. The difference is that a double score will be associated with each type of element.

It is to redis from small to large order of collection of member passing score. Zset member is unique, but the score (score) it can be repeated.

It is similar to Java's SortedSet and HashMap combination of, on the one hand it is a set, to ensure the uniqueness of the internal value, on the other hand it can assign a value to each score, the representative of the value of the ranking weight. With its internal implementation is called "jump list" data structures.

redis common data types found at:

Key (Key) commonly used commands

command usage description Examples
OF THE DEL key [key ...] Delete given one or more key. Non-existent key will be ignored
DUMP DUMP key Sequence of a given key, and return the serialized value, this value using the RESTORE command may be deserialized into key Redis
EXISTS EXISTS key Check whether there is a given key
EXPIRE EXPIRE key seconds Set survival time for the given key, when the key expires (survival time is 0), it will be automatically deleted
PERSIST PERSIST key Removes the key expiration time, key will endure.
EXPIREAT EXPIREAT key timestamp And the role of EXPIREAT EXPIRE similar, are used to set the survival time is key. Except that the time parameter EXPIREAT accepted command is a UNIX timestamp (unix timestamp) EXPIREAT cache 1355292000 # The key will expire in 2012.12.12
KEYS KEYS pattern Find all key match a given pattern of pattern Within KEYS * # matching all key database
MOVE MOVE key db 将当前数据库的 key 移动到给定的数据库 db 当中如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key ,或者 key 不存在于当前数据库,那么 MOVE 没有任何效果。 因此,也可以利用这一特性,将 MOVE 当作锁(locking)原语(primitive) MOVE song 1 # 将 song 移动到数据库 1
TTL TTL key 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key 的剩余生存时间
PTTL PTTL key 以毫秒为单位返回 key 的剩余的过期时间。
TYPE TYPE key 返回 key 所储存的值的类型
RENAME RENAME key newkey 将 key 改名为 newkey 。当 key 和 newkey 相同,或者 key 不存在时,返回一个错误。当 newkey 已经存在时, RENAME 命令将覆盖旧值

String (字符串)常用命令

命令 用法 描述 示例
SET SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值 value 关联到 key 。如果 key 已经持有其他值, SET 就覆写旧值,无视类型 SET key "value"
MSET MSET key value [key value ...] 同时设置一个或多个 key-value 对。如果某个给定 key 已经存在,那么 MSET 会用新值覆盖原来的旧值,如果这不是你所希望的效果,请考虑使用 MSETNX 命令:它只会在所有给定 key 都不存在的情况下进行设置操作 MSET date "2012.3.30" time "11:00 a.m." weather "sunny"
SETNX SETNX key value 将 key 的值设为 value ,当且仅当 key 不存在。若给定的 key 已经存在,则 SETNX 不做任何动作 SETNX 是『SET if Not eXists』(如果不存在,则 SET)的简写
MSETNX MSETNX key value [key value ...] 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。即使只有一个给定 key 已存在, MSETNX 也会拒绝执行所有给定 key 的设置操作
SETRANGE SETRANGE key offset value 用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始。不存在的 key 当作空白字符串处理
SETBIT SETBIT key offset value 对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit) GETBIT bit 100 # bit 默认被初始化为 0
SETEX SETEX key seconds value 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。如果 key 已经存在, SETEX 命令将覆写旧值。
PSETEX PSETEX key milliseconds value 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位
STRLEN STRLEN key 返回 key 所储存的字符串值的长度。当 key 储存的不是字符串值时,返回一个错误
GET GET key 返回 key 所关联的字符串值。如果 key 不存在那么返回特殊值 nil
MGET MGET key [key ...] 返回所有(一个或多个)给定 key 的值。如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。因此,该命令永不失败
GETRANGE GETRANGE key start end 返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。负数偏移量表示从字符串最后开始计数, -1 表示最后一个字符, -2 表示倒数第二个,以此类推。 GETRANGE greeting 0 4
GETSET GETSET key value 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。当 key 存在但不是字符串类型时,返回一个错误。
GETBIT GETBIT key offset 对 key 所储存的字符串值,获取指定偏移量上的位(bit)。当 offset 比字符串值的长度大,或者 key 不存在时,返回 0
APPEND APPEND key value 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样
DECR DECR key 将 key 中储存的数字值减一。如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作 redis> SET failure_times 10OK redis> DECR failure_times(integer) 9
DECRBY DECRBY key decrement 将 key 所储存的值减去减量 decrement
INCR INCR key 将 key 中储存的数字值增一。如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作
INCRBY INCRBY key increment 将 key 所储存的值加上增量 increment
INCRBYFLOAT INCRBYFLOAT key increment 为 key 中所储存的值加上浮点数增量 increment INCRBYFLOAT mykey 0.1
BITCOUNT BITCOUNT key [start] [end] 计算给定字符串中,被设置为 1 的比特位的数量
BITOP BITOP operation destkey key [key ...] 对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上。
☆☆位图:

​ String命令中包含了一种特殊的操作,直接操作bit,某些特殊场景下,会节省存储空间。可以在存取bool型数据的场景使用,比如存取用户男女比例,用户某一段日期签到记录,

​ 在我们平时开发过程中,会有一些 bool 型数据需要存取,比如用户一年的签到记录,签了是 1,没签是 0,要记录 365 天。如果使用普通的 key/value,每个用户要记录 365 个,当用户上亿的时候,需要的存储空间是惊人的。

为了解决这个问题,Redis 提供了位图数据结构,这样每天的签到记录只占据一个位,365 天就是 365 个位,46 个字节 (一个稍长一点的字符串) 就可以完全容纳下,这就大大节约了存储空间。

位图不是特殊的数据结构,它的内容其实就是普通的字符串,也就是 byte 数组。我们可以使用普通的 get/set 直接获取和设置整个位图的内容,也可以使用位图操作 getbit/setbit 等将 byte 数组看成「位数组」来处理

Redis 的位数组是自动扩展,如果设置了某个偏移位置超出了现有的内容范围,就会自动将位数组进行零扩充。

接下来我们使用 redis-cli 设置第一个字符,也就是位数组的前 8 位,我们只需要设置值为 1 的位,如上图所示,h 字符只有 1/2/4 位需要设置,e 字符只有 9/10/13/15 位需要设置。值得注意的是位数组的顺序和字符的位顺序是相反的。

127.0.0.1:6379> setbit s 1 1
(integer) 0
127.0.0.1:6379> setbit s 2 1
(integer) 0
127.0.0.1:6379> setbit s 4 1
(integer) 0
127.0.0.1:6379> setbit s 9 1
(integer) 0
127.0.0.1:6379> setbit s 10 1
(integer) 0
127.0.0.1:6379> setbit s 13 1
(integer) 0
127.0.0.1:6379> setbit s 15 1
(integer) 0
127.0.0.1:6379> get s
"he"

上面这个例子可以理解为「零存整取」,同样我们还也可以「零存零取」,「整存零取」。「零存」就是使用 setbit 对位值进行逐个设置,「整存」就是使用字符串一次性填充所有位数组,覆盖掉旧值。

bitcount和bitop, bitpos,bitfield 都是操作位图的指令。

List(列表)常用命令

命令 用法 描述 示例
LPUSH LPUSH key value [value ...] 将一个或多个值 value 插入到列表 key 的表头如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表头 正着进反着出
LPUSHX LPUSHX key value 将值 value 插入到列表 key 的表头,当且仅当 key 存在并且是一个列表。和 LPUSH 命令相反,当 key 不存在时, LPUSHX 命令什么也不做
RPUSH RPUSH key value [value ...] 将一个或多个值 value 插入到列表 key 的表尾(最右边) 怎么进怎么出
RPUSHX RPUSHX key value 将值 value 插入到列表 key 的表尾,当且仅当 key 存在并且是一个列表。和 RPUSH 命令相反,当 key 不存在时, RPUSHX 命令什么也不做。
LPOP LPOP key 移除并返回列表 key 的头元素。
BLPOP BLPOP key [key ...] timeout 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
RPOP RPOP key 移除并返回列表 key 的尾元素。
BRPOP BRPOP key [key ...] timeout 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOPLPUSH BRPOPLPUSH source destination timeout 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
RPOPLPUSH RPOPLPUSH source destinationb 命令 RPOPLPUSH 在一个原子时间内,执行以下两个动作:将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素 RPOPLPUSH list01 list02
LSET LSET key index value 将列表 key 下标为 index 的元素的值设置为 value
LLEN LLEN key 返回列表 key 的长度。如果 key 不存在,则 key 被解释为一个空列表,返回 0 .如果 key 不是列表类型,返回一个错误
LINDEX LINDEX key index 返回列表 key 中,下标为 index 的元素。下标(index)参数 start 和 stop 都以 0 为底,也就是说,以 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。相当于 Java 链表的get(int index)方法,它需要对链表进行遍历,性能随着参数index增大而变差。
LRANGE LRANGE key start stop 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定
LREM LREM key count value 根据参数 count 的值,移除列表中与参数 value 相等的元素
LTRIM LTRIM key start stop 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除
LINSERT LINSERT key BEFORE|AFTER pivot value 将值 value 插入到列表 key 当中,位于值 pivot 之前或之后。当 pivot 不存在于列表 key 时,不执行任何操作。当 key 不存在时, key 被视为空列表,不执行任何操作。如果 key 不是列表类型,返回一个错误。 LINSERT list01 before c++ c#(在c++之前加上C#)

Hash(哈希表)常用命令

命令 用法 描述 示例
HSET HSET key field value 将哈希表 key 中的域 field 的值设为 value 。如果 key 不存在,一个新的哈希表被创建并进行 HSET 操作。如果域 field 已经存在于哈希表中,旧值将被覆盖。
HMSET HMSET key field value [field value ...] 同时将多个 field-value (域-值)对设置到哈希表 key 中。此命令会覆盖哈希表中已存在的域。
HSETNX HSETNX key field value 将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。若域 field 已经存在,该操作无效
HGET HGET key field 返回哈希表 key 中给定域 field 的值
HMGET HMGET key field [field ...] 返回哈希表 key 中,一个或多个给定域的值。
HGETALL HGETALL key 返回哈希表 key 中,所有的域和值。在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍
HDEL HDEL key field [field ...] 删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略
HEXISTS HEXISTS key field 查看哈希表 key 中,给定域 field 是否存在
HLEN HLEN key 返回哈希表 key 中域的数量
HKEYS HKEYS key 返回哈希表 key 中的所有域
HVALS HVALS key 返回哈希表 key 中所有域的值
HSTRLEN HSTRLEN key field 返回哈希表 key 中,与给定域 field 相关联的值的字符串长度(string length)。如果给定的键或者域不存在,那么命令返回 0
HINCRBY HINCRBY key field increment 为哈希表 key 中的域 field 的值加上增量 increment
HINCRBYFLOAT HINCRBYFLOAT key field increment 为哈希表 key 中的域 field 加上浮点数增量 increment
HSCAN HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对。

Set(集合)常用命令

命令 用法 描述 示例
SADD SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。假如 key 不存在,则创建一个只包含 member 元素作成员的集合。当 key 不是集合类型时,返回一个错误
SCARD SCARD key 返回集合 key 的基数(集合中元素的数量)。
SDIFF SDIFF key [key ...] 返回一个集合的全部成员,该集合是所有给定集合之间的差集。不存在的 key 被视为空集。 差集
SDIFFSTORE SDIFFSTORE destination key [key ...] 这个命令的作用和 SDIFF 类似,但它将结果保存到 destination 集合,而不是简单地返回结果集。如果 destination 集合已经存在,则将其覆盖。destination 可以是 key 本身。
SINTER SINTER key [key ...] 返回一个集合的全部成员,该集合是所有给定集合的交集。不存在的 key 被视为空集。当给定集合当中有一个空集时,结果也为空集(根据集合运算定律) 交集
SINTERSTORE SINTERSTORE destination key [key ...] 这个命令类似于 SINTER 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集。如果 destination 集合已经存在,则将其覆盖。destination 可以是 key 本身
SUNION SUNION key [key ...] 返回一个集合的全部成员,该集合是所有给定集合的并集。不存在的 key 被视为空集 并集
SUNIONSTORE SUNIONSTORE destination key [key ...] 这个命令类似于 SUNION 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集。如果 destination 已经存在,则将其覆盖。destination 可以是 key 本身
SMEMBERS SMEMBERS key 返回集合 key 中的所有成员。不存在的 key 被视为空集合
SRANDMEMBER SRANDMEMBER key [count] 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素
SISMEMBER SISMEMBER key member 判断 member 元素是否集合 key 的成员
SMOVE SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合。
SPOP SPOP key 移除并返回集合中的一个随机元素。如果只想获取一个随机元素,但不想该元素从集合中被移除的话,可以使用 SRANDMEMBER 命令。
SREM SREM key member [member ...] 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。当 key 不是集合类型,返回一个错误
SSCAN SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素

SortedSet(有序集合)常用命令

命令 用法 描述 示例
ZADD ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
ZCARD ZCARD key 返回有序集 key 的基数。
ZCOUNT ZCOUNT key min max 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。关于参数 min 和 max 的详细使用方法,请参考 ZRANGEBYSCORE 命令。
ZRANGE ZRANGE key start stop [WITHSCORES] 返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递增(从小到大)来排序
ZREVRANGE ZREVRANGE key start stop [WITHSCORES] 返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递减(从大到小)来排列。具有相同 score 值的成员按字典序的逆序(reverse lexicographical order)排列。除了成员按 score 值递减的次序排列这一点外, ZREVRANGE 命令的其他方面和 ZRANGE 命令一样。
ZREVRANGEBYSCORE ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] 返回有序集 key 中, score 值介于 max 和 min 之间(默认包括等于 max 或 min )的所有的成员。有序集成员按 score 值递减(从大到小)的次序排列。
ZREVRANK ZREVRANK key member 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递减(从大到小)排序。排名以 0 为底,也就是说, score 值最大的成员排名为 0 。使用 ZRANK 命令可以获得成员按 score 值递增(从小到大)排列的排名。
ZSCORE ZSCORE key member 返回有序集 key 中,成员 member 的 score 值。如果 member 元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
ZRANGEBYSCORE ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。有序集成员按 score 值递增(从小到大)次序排列。
ZRANK ZRANK key member 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列。
ZINCRBY ZINCRBY key increment member 为有序集 key 的成员 member 的 score 值加上增量 increment
ZREM ZREM key member [member ...] 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。当 key 存在但不是有序集类型时,返回一个错误。
ZREMRANGEBYRANK ZREMRANGEBYRANK key start stop 移除有序集 key 中,指定排名(rank)区间内的所有成员
ZREMRANGEBYSCORE ZREMRANGEBYSCORE key min max 移除有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。
ZUNIONSTORE ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] 计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
ZINTERSTORE ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] 计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之和.
ZSCAN ZSCAN key cursor [MATCH pattern] [COUNT count] 迭代有序集合中的元素(包括元素成员和元素分值)
ZRANGEBYLEX ZRANGEBYLEX key min max [LIMIT offset count] 当有序集合的所有成员都具有相同的分值时,有序集合的元素会根据成员的字典序(lexicographical ordering)来进行排序,而这个命令则可以返回给定的有序集合键 key 中,值介于 min 和 max 之间的成员。
ZLEXCOUNT ZLEXCOUNT key min max Scores for all members of an ordered collection of key are the same key, this command returns the set, the number of elements between the members of the min and max ranges. Like this sense min and max parameters parameter meaning and ZRANGEBYLEX commands and command parameters min max parameters
ZREMRANGEBYLEX ZREMRANGEBYLEX key min max Scores for all members of an ordered collection of key are the same key, this command will remove the set, all the elements between the members of the min and max ranges. Like this sense min and max parameters parameter meaning and ZRANGEBYLEX commands and command parameters min max parameters

HyperLogLog

In Redis 2.8.9 release adds HyperLogLog structure.

Scene: UV statistics can be used to site ...

Redis HyperLogLog base is used to make statistical algorithms HyperLogLog advantage is that, when the number of input elements or very, very large volume of space required for the calculation base is always fixed, and is very small. But there will be errors.

command usage description
pfadd [PFADD key element [element ...] Adds the specified element to the HyperLogLog
pfcount [PFCOUNT key [key ...] Returns HyperLogLog given cardinality estimates.
pfmerge [Manual PFMERGE sourcekey [sourcekey ...] Multiple HyperLogLog into one HyperLogLog
public class JedisTest {
  public static void main(String[] args) {
    Jedis jedis = new Jedis();
    for (int i = 0; i < 100000; i++) {
      jedis.pfadd("codehole", "user" + i);
    }
    long total = jedis.pfcount("codehole");
    System.out.printf("%d %d\n", 100000, total);
    jedis.close();
  }
}

HyperLogLog illustration

All instructions redis

Each command description

Guess you like

Origin www.cnblogs.com/lazyegg/p/12061385.html