string類型 set/get 最常見也是最簡單的操作.key value 索引值對;
[plain] view plain copy 192.168.159.130:7000> set num 1 OK 192.168.159.130:7000> get num "1" 192.168.159.130:7000> get age (nil)
setnx 同set 區別在於 如果key值存在,不覆蓋返回0, 如果key值不存在,設定值.並且返回1(nx not exist)
[plain] view plain copy 192.168.159.130:7000> setnx num 1 (integer) 0 192.168.159.130:7000> get num "1" 192.168.159.130:7000> setnx age 1 (integer) 1 192.168.159.130:7000> get age "1"
setex 同set 區別在於可以給資料加一個失效時間. set key 時間 value
[plain] view plain copy 192.168.159.130:7000> setex num 5 1 OK 192.168.159.130:7000> get num "1" 192.168.159.130:7000> get num "1" 192.168.159.130:7000> get num (nil)
setrange 同set 可以替換value值 setrange key 要替換的開始索引 替換的值 替換的長度取決於替換值的長度.長度不夠,原值補充
[plain] view plain copy 192.168.159.130:7001> set name helloworld OK 192.168.159.130:7001> SETRANGE name 1 t (integer) 10 192.168.159.130:7001> get name "htlloworld" 192.168.159.130:7001> setrange name 0 helloworld (integer) 10 192.168.159.130:7001> get name "helloworld"
mset 同set 一次設定多個值(特點要麼全部成功,要不全部失敗) set key1 value1 key2 value2 ... 這裡不做示範(因為我的環境是redis叢集,設定多個值的時候,redis有可能會報CROSSSLOT Keys in request don't hash to the same slot 異常.原因請看下一篇關於redis的文章) getset 同set 只不過會返回key的原值 沒有返回nil
[plain] view plain copy 192.168.159.130:7000> getset age 1 "2" 192.168.159.130:7000> getset age 2 "1" 192.168.159.130:7000> getset take 2 (nil) 192.168.159.130:7000> getset take 1 "2"
getrange 擷取值的一部分 getrange key 開始索引 結束索引
[plain] view plain copy 192.168.159.130:7001> GETRANGE name 0 (error) ERR wrong number of arguments for 'getrange' command 192.168.159.130:7001> GETRANGE name 0 4 "hello" 192.168.159.130:7001> GETRANGE name 0 18 "helloworld"
mget 同 get 特點是一次後去多個 key 鎖對應的值. mget key1 key2 ... 這裡不做示範.原因同mset incr 增加對應key 的value值 incr key 返回增加後的value值
[plain] view plain copy 192.168.159.130:7001> get name "helloworld" 192.168.159.130:7001> INCR name (error) ERR value is not an integer or out of range 192.168.159.130:7001> set name 1 OK 192.168.159.130:7001> INCR name (integer) 2
incrby 同incr 特點是可以根據你給定的值 增加key對應的value值.如果key值不存在直接設定key值(incr 也相同)
[plain] view plain copy 192.168.159.130:7003> get student (nil) 192.168.159.130:7001> INCRBY student 10 (integer) 10 192.168.159.130:7001> get student "10" 192.168.159.130:7001> INCRBY student 2 (integer) 12
decr 和decrby 這裡不做介紹.同incr append 相當於java中的 stringbuffer 裡的append方法 .向後追加字串
[plain] view plain copy 192.168.159.130:7001> set name aa OK 192.168.159.130:7001> get name "aa" 192.168.159.130:7001> APPEND name bb (integer) 4 192.168.159.130:7001> get name "aabb"
strlen 擷取對象key的value值長度
[plain] view plain copy 192.168.159.130:7001> get name "aabb" 192.168.159.130:7001> strlen name (integer) 4 192.168.159.130:7001> set name 1 OK 192.168.159.130:7001> STRLEN name (integer)
2.HASH字典類型 hset key field value(設定或者新增對象的一個屬性),hget key field (擷取對象的單個屬性值),hsetnx(同string類型的setnx 設定屬性值 如果有值則不執行)
[plain] view plain copy 192.168.159.130:7000> hset person age 10 (integer) 1 192.168.159.130:7000> hget person age "10" 192.168.159.130:7000> hsetnx person age 5 (integer) 0 192.168.159.130:7000> hsetnx person sex 1 (integer) 1 192.168.159.130:7000> hget person sex "1"
hmset key field1 value1 field2 value2...(大量設定對象屬性),hmget key field1 field2..(批量擷取對象屬性值),hgetall key(擷取對象所有的屬性以及屬性值)
[plain] view plain copy 192.168.159.130:7001> hmset person name xiaoming age 5 sex 1 OK 192.168.159.130:7000> hmget person name age sex 1) "xiaoming" 2) "5" 3) "1" 192.168.159.130:7000> hgetall person 1) "name" 2) "xiaoming" 3) "age" 4) "5" 5) "sex" 6) "1"
hexists key field(判斷是否屬性是否存在),hlen key(返回對象屬性條數),hdel key field( 刪除指定屬性),hkeys key (返回對象所有屬性名稱),hvals key(返回對象所有屬性值)
[plain] view plain copy 192.168.159.130:7000> HINCRBY person sex 1 (integer) 2 192.168.159.130:7000> HEXISTS person hobby (integer) 0 192.168.159.130:7000> hlen person (integer) 3 192.168.159.130:7000> hkeys (error) ERR wrong number of arguments for 'hkeys' command 192.168.159.130:7000> hkeys person 1) "name" 2) "age" 3) "sex" 192.168.159.130:7000> hvals person 1) "xiaoming" 2) "5" 3) "2" 192.168.159.130:7000> hdel person sex (integer) 1 192.168.159.130:7000> hget person sex (nil)
3.LIST 隊列資料類型 lpush key value1 value2 ...(從頭部依次插入資料),rpush key value1,value2..(從尾部依次插入資料),lrange key 開始索引 結束索引 (查詢指定索引範圍內的元素 0 -1標示全部)
[plain] view plain copy 192.168.159.130:7000> LPUSH mylist a -> Redirected to slot [5282] located at 192.168.159.130:7001 (integer) 1 192.168.159.130:7001> lpush mylist b (integer) 2 192.168.159.130:7001> LRANGE 0 -1 (error) ERR wrong number of arguments for 'lrange' command 192.168.159.130:7001> LRANGE mylist 0 -1 1) "b" 2) "a" 192.168.159.130:7001> rpush mylist c (integer) 3 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "a" 3) "c" linsert key value before/after 待插入值 (在指定值的前後插入值)
[plain] view plain copy 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "a" 3) "c" 192.168.159.130:7001> linsert mylist before a d (integer) 4 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "d" 3) "a" 4) "c" 192.168.159.130:7001> linsert mylist after a e (integer) 5 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "d" 3) "a" 4) "e" 5) "c" lset key 位置 value(替換自定位子的值)
[plain] view plain copy 192.168.159.130:7000> lset mylist 1 f OK 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "f" 3) "a" 4) "e" 5) "c"
lrem key 個數 value(刪除隊列中值為value的元素,個數為正數從頭開始刪除,個數為負數從尾部開始刪除,個數為0全部刪除
[plain] view plain copy 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "a" 3) "b" 4) "a" 5) "a" 6) "b" 7) "f" 8) "a" 9) "e" 10) "c" 192.168.159.130:7001> lrem mylist 1 a (integer) 1 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "b" 3) "a" 4) "a" 5) "b" 6) "f" 7) "a" 8) "e" 9) "c" 192.168.159.130:7001> lrem mylist -1 a (integer) 1 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "b" 3) "a" 4) "a" 5) "b" 6) "f" 7) "e" 8) "c" 192.168.159.130:7001> lrem mylist 0 b (integer) 3 192.168.159.130:7001> lrange mylist 0 -1 1) "a" 2) "a" 3) "f" 4) "e" 5) "c" ltrim 保留指定範圍內資料 成功返回ok
[plain] view plain copy 192.168.159.130:7001> lrange mylist 0 -1 1) "a" 2) "a" 3) "f" 4) "e" 5) "c" 192.168.159.130:7001> ltim mylist 1 2 (error) ERR unknown command 'ltim' 192.168.159.130:7001> ltrim mylist 1 2 OK 192.168.159.130:7001> lrange mylist 0 -1 1) "a" 2) "f"
llen 返回list個數
[plain] view plain copy 192.168.159.130:7001> ltrim mylist 3 7 OK 192.168.159.130:7001> lrange mylist 0 -1 (empty list or set) 192.168.159.130:7001> llen mylist (integer) 0 lpop 和rpop 前者是從頭部彈出一個資料 後者是從尾部彈出一個資料 [plain] view plain copy 192.168.159.130:7001> lpop mylist (nil) 192.168.159.130:7001> lpush mylist a (integer) 1 192.168.159.130:7001> lpush mylist b (integer) 2 192.168.159.130:7001> lrange mylist 0 -1 1) "b" 2) "a" 192.168.159.130:7001> rpop mylist "a" lindex (返回指定索引的資料) rpoplpush(彈出前一個list尾部資料 壓入後一個list頭部,這個方法如果叢集也不適合)