Redis常用命令,redis命令

來源:互聯網
上載者:User

Redis常用命令,redis命令

1. SELECT命令

Redis有0 ~ 15個資料庫, 使用SELECT 6則選擇在資料庫六下操作。



2. SET : 賦值命令

SET server:name "fido" 設定Key為server:name, Value為"fido"



3. GET : 取值命令

GET server:name 將得到"fido"



4. DEL : 刪除命令

DEL server:name 將刪除server:name的Key以及和它的Value



5. INCR: 數值自增長命令(這是個安全執行緒的計數命令, 在多線程下計數不會出錯)

SET ten 10

INCR ten  結果是 (integer)11



6. EXPIRE : 到期命令

SET timeout "120s"

EXPIRE timeout 60    60秒後到期



7. TTL(Time To Live) : 查看還剩多少時間到期

TTL timeout  

結果: (integer) 55     還剩55秒存活時間

逾時後:(integer) -2   意味著將不存在(如果是-1的話說明這個將不會expire)



8.  List 資料結構(按一定順序排列)

RPUSH print "!"            在list的右邊插入"!"

LPUSH print "World"    在list的左邊插入"World"

LPUSH print "Hello"      在list的左邊插入"Hello"

LRANGE print 0 -1        得到list子集合0為初始位, -1是最終位

列印:

1) "Hello"
2) "World"
3) "!"

LLEN print                     得到list長度 (integer) 3

LPOP print                    從list左邊彈出一個資料

RPOP print                  從list右邊邊彈出一個資料

觀察結果: LRANGE print 0 -1

"World"



9.  Set資料結構(與List類似, 但是無序且每個元素僅出現一次)

SADD setTest "fucking"               向Set中增加"fucking"元素

SADD setTest "day"                     向Set中增加"day"元素

SREM setTest "fucking"               將Set中"fucking"元素刪除

SISMEMBER setTest "fucking"     查看"fucking"在不再set裡面, 返回1表示存在, 0表示不存在。

SMEMBERS setTest                      查看set中所有元素   結果:"day"

SUNION setTest setTest2             串連2個set



10. Sorted Sets (有序Set)

ZADD sortedSetTest 1 "A"             插入元素"A"以數字1為排序指標

ZADD sortedSetTest 2 "B"             插入元素"B"以數字2為排序指標

ZADD sortedSetTest 3 "C"             插入元素"C"以數字3為排序指標

ZRANGE sortedSetTest 0 -1          

查看結果:

"A"
"B"
"C"



11. Hash資料結構

HSET user:007 name "James Bond"     設定Key=user:007, Value=name "James Bond"(這又是個Key-Value)

HSET user:007 gender "male"

HGET user:007 name                            查詢user:007下name的Value

結果:

"James Bond"

HGETALL user:007                                查詢user:007下所有Key-Value

結果:

1) "name"
2) "James Bond"
3) "gender"
4) "male"

HMSET user:008 name "Xingchi_Zhou" gender "male"  直接設定多個Key-Value效果和上面多句賦值相同

HSET user:007 bulletCount 1                設定數值:子彈數"1"

HINCRBY user:007 bulletCount 100      增加數值:子彈數(integer) 101

HDEL user:007 bulletCount                   刪除數值:子彈數(nil)



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.