<a target="_blank"></a>
<a></a>
通過set key value來存儲,通過get key來擷取值

expire key 多少秒:設定多少秒後過期; ttl key:Time To Live,檢視還可以存活多久,-2表示key不存在;-1表示定時任務消失,永久存儲。
->
->
現在學習list:
-》
和java中list與set的差別一樣。這裡的set無序且值唯一。
和set一樣,唯一。但z多了個score用來排序。是以指令又像list一樣:
可以存儲對象,比如人,編号,姓名,年齡等
文檔如下:
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 >= 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中:
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 選項的參數來使用。
比如說,對于前面給出的使用者資訊表:
我們可以不将使用者的名字和級别儲存在 user_name_{uid} 和 user_level_{uid} 兩個字元串鍵中, 而是用一個帶有 name 域和 level 域的哈希表 user_info_{uid} 來儲存使用者的名字和級别資訊:
之後, by 和 get 選項都可以用 key->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,如需轉載請自行聯系原作者