The next data structure that we'll look at is a set. A set is similar to a list, except it does not have a specific order and each element may only appear once. Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
sadd key value : 向set添加元素
srem key value :从set中移除元素
smembers key : 取出所有set元素
SISMEMBER key value: 查看value是否存在set中
SUNION key1 key2 ... keyN:将所有key合并后取出来,相同的值只取一次
scard key : 获取set中元素的个数
SRANDMEMBER key: Return a random element from a Set, without removing the element.随机取出一个
SDIFF key1 key2 ... keyN:获取第一set中不存在后面几个set里的元素。
SDIFFSTORE dstkey key1 key2 ... keyN:和sdiff相同,获取key1中不存在其他key里的元素,但要存储到dstkey中。
SINTER key1 key2 ... keyN:取出这些set的交集
SINTERSTORE dstkey key1 key2 ... keyN:取出这些key的交集并存储到dstkey
SMOVE srckey dstkey member:将元素member从srckey中转移到dstkey中,这个操作是原子的。
-》
-》
-》
7.有序集合sorted set
和set一样,唯一。但z多了个score用来排序。所以命令又像list一样:
ZADD key score member:向有序set中添加元素member,其中score为分数,默认升序; ZRANGE key start end [WITHSCORES]:获取按score从低到高索引范围内的元素,索引可以是负数,-1表示最后一个,-2表示倒数第二个,即从后往前。withscores可选,表示获取包括分数。
ZREVRANGE key start end [WITHSCORES]:同上,但score从高到低排序。
ZCOUNT key min max:获取score在min和max范围内的元素的个数
ZCARD key:获取集合中元素的个数。
ZINCRBY key increment member:根据元素,score原子增加increment.
ZREMRANGEBYSCORE key min max:清空集合内的score位于min和max之间的元素。
ZRANK key member:获取元素的索引(照score从低到高排列)。 ZREM key member:移除集合中的该元素 ZSCORE key member:获取该元素的score
8.对象存储Hashes
可以存储对象,比如人,编号,姓名,年龄等
HSET key field value:key是对象名,field是属性,value是值;
HMSET key field value [field value ...]:同时设置多个属性
HGET key field:获取该对象的该属性
HMGET key field value [field value ...]:获取多个属性值
HGETALL key:获取对象的所有信息
HKEYS key:获取对象的所有属性
HVALS key:获取对象的所有属性值
HDEL key field:删除对象的该属性
HEXISTS key field:查看对象是否存在该属性
HINCRBY key field value:原子自增操作,只能是integer的属性值可以使用;
HLEN key: Return the number of entries (fields) contained in the hash stored at key.获取属性的个数。
->
->
9.sort排序!
文档如下:
SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ] DESCRIPTION: Sort the elements contained in the List, Set, or Sorted Set value at key. By default sorting is numeric with elements being compared as double precision floating point numbers. This is the simplest form of SORT: SORT mylist
Assuming mylist contains a list of numbers, the return value will be the list of numbers ordered from the smallest to the biggest number. In order to get the sorting in reverse order use DESC: SORT mylist DESC
The ASC option is also supported but it's the default so you don't really need it. If you want to sort lexicographically use ALPHA. Note that Redis is utf-8 aware assuming you set the right value for the LC_COLLATE environment variable.
Sort is able to limit the number of returned elements using the LIMIT option: SORT mylist LIMIT 0 10
In the above example SORT will return only 10 elements, starting from the first one (start is zero-based). Almost all the sort options can be mixed together. For example the command: SORT mylist LIMIT 0 10 ALPHA DESC
Will sort mylist lexicographically, in descending order, returning only the first 10 elements.
Sometimes you want to sort elements using external keys as weights to compare instead to compare the actual List Sets or Sorted Set elements. For example the list mylist may contain the elements 1, 2, 3, 4, that are just unique> SORTING BY EXTERNAL KEYS: SORT mylist BY weight_*
the BY option takes a pattern (weight_* in our example) that is used in order to generate the key names of the weights used for sorting. Weight key names are obtained substituting the first occurrence of * with the actual value of the elements on the list (1,2,3,4 in our example).
Our previous example will return just the sorted> RETRIEVING EXTERNAL KEYS: SORT mylist BY weight* GET object*
Note that GET can be used multiple times in order to get more keys for every element of the original List, Set or Sorted Set sorted.
Since Redis >= 1.1 it's possible to also GET the list elements itself using the special # pattern: SORT mylist BY weight* GET object* GET # STORING THE RESULT OF A SORT OPERATION:
By default SORT returns the sorted elements as its return value. Using the STORE option instead to return the elements SORT will store this elements as a Redis List in the specified key. An example:SORT mylist BY weight_* STORE resultkey
An interesting pattern using SORT ... STORE consists in associating an EXPIRE timeout to the resulting key so that in applications where the result of a sort operation can be cached for some time other clients will use the cached list instead to call SORT for every request. When the key will timeout an updated version of the cache can be created using SORT ... STORE again.
Note that implementing this pattern it is important to avoid that multiple clients will try to rebuild the cached version of the cache at the same time, so some form of locking should be implemented (for instance using SETNX). RETURN VALUE: A multi bulk reply containing a list of sorted elements.