天天看点

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

<a target="_blank"></a>

<a></a>

通过set key value来存储,通过get key来获取值

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

expire key 多少秒:设置多少秒后过期;  ttl key:Time To Live,查看还可以存活多久,-2表示key不存在;-1表示定时任务消失,永久存储。

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

     -&gt;

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

-&gt;

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

现在学习list:

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

-》

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

和java中list与set的区别一样。这里的set无序且值唯一。

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

和set一样,唯一。但z多了个score用来排序。所以命令又像list一样:

可以存储对象,比如人,编号,姓名,年龄等

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]
redis学习教程之一基本命令 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 IDs of objects stored at object_1, object_2, object_3 and object_4, while the keys weight_1, weight_2, weight_3 and weight_4 can contain weights we want to use to sort our list of objects identifiers. We can use the following command: 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 IDs. Often it is needed to get the actual objects sorted (object_1, ..., object_4 in the example). We can do it with the following command: 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 &gt;= 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.

复制一个sort的用法:http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135921.html

返回或保存给定列表、集合、有序集合key中经过排序的元素。排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较。
最简单的sort使用方法是sort key和sort key desc。 sort key:返回键值从小到大排序的结果。 sort key desc:返回键值从大到小排序的结果。

假设price列表保存了今日的物品价格, 那么可以用sort命令对它进行排序:

# 开销金额列表

# 排序

# 逆序排序

因为sort命令默认排序对象为数字,当需要对字符串进行排序时,需要显式地在sort命令之后添加alpha修饰符。

# 网址

# 默认(按数字)排序

排序之后返回元素的数量可以通过limit修饰符进行限制,修饰符接受offset和count两个参数。offset:指定要跳过的元素数量,即起始位置。count:指定跳过offset个指定的元素之后,要返回多少个对象。

以下例子返回排序结果的前5个对象(offset为0表示没有元素被跳过)。

# 添加测试数据,列表值为1~10

# 返回列表中最小的5个值

可以使用外部 key 的数据作为权重,代替默认的直接对比键值的方式来进行排序。

假设现在有用户数据如下:

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

以下代码将数据输入到redis中:

 View Code

默认情况下, sort uid直接按uid中的值排序:
通过使用by选项,可以让uid按其他键的元素来排序。

比如说, 以下代码让uid键按照user_level_{uid}的大小来排序:

user_level_*是一个占位符,它先取出uid中的值,然后再用这个值来查找相应的键。

比如在对uid列表进行排序时,程序就会先取出uid的值1、2、3、4,然后使用user_level_1、user_level_2、user_level_3和 user_level_4的值作为排序uid的权重。

使用get选项,可以根据排序的结果来取出相应的键值。

比如说,以下代码先排序uid,再取出键user_name_{uid}的值:

现在的排序结果要比只使用 sort uid by user_level_* 要直观得多。

可以同时使用多个get选项,获取多个外部键的值。

以下代码就按 uid 分别获取 user_level_{uid} 和 user_name_{uid} :

get有一个额外的参数规则,那就是可以用#获取被排序键的值。

以下代码就将 uid 的值、及其相应的 user_level_* 和 user_name_* 都返回为结果:

通过将一个不存在的键作为参数传给 by 选项, 可以让 sort 跳过排序操作,直接返回结果:

这种用法在单独使用时,没什么实际用处。

不过,通过将这种用法和get选项配合,就可以在不排序的情况下,获取多个外部键,相当于执行一个整合的获取操作(类似于 sql数据库的join关键字)。

以下代码演示了,如何在不引起排序的情况下,使用sort、by和get获取多个外部键:

除了可以将字符串键之外, 哈希表也可以作为 get 或 by 选项的参数来使用。

比如说,对于前面给出的用户信息表:

redis学习教程之一基本命令 SORT [BY] [LIMIT] [GET] [ASC|DESC] [ALPHA] [STORE ]

我们可以不将用户的名字和级别保存在 user_name_{uid} 和 user_level_{uid} 两个字符串键中, 而是用一个带有 name 域和 level 域的哈希表 user_info_{uid} 来保存用户的名字和级别信息:

之后, by 和 get 选项都可以用 key-&gt;field 的格式来获取哈希表中的域的值, 其中 key 表示哈希表键, 而 field 则表示哈希表的域:

默认情况下, sort 操作只是简单地返回排序结果,并不进行任何保存操作。 通过给 store 选项指定一个 key 参数,可以将排序结果保存到给定的键上。 如果被指定的 key 已存在,那么原有的值将被排序结果覆盖。

# 测试数据

没有使用 store 参数,返回列表形式的排序结果。

使用 store 参数,返回排序结果的元素数量。

redis内置了很多原子操作的命令,比如incr,getset等,但实际中我们希望将一组命令原子的执行,这时候就需要用到事物。做法如下:

使用关键字multi 输入你想要的命令组合 输入exec来执行,或discard来放弃

 本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/redis.html,如需转载请自行联系原作者

继续阅读