2018-05-01隨筆

這周都在考試,只有最后兩天老翁上課了,當(dāng)然,這個試考的很難受,多虧老翁才能沒末班,這周沒學(xué)啥,寫點(diǎn)老翁教的redis基本操作吧

redis 鍵和hash

啟動redis

redis-server /etc/redis/redis.conf

打開

redis-cli

設(shè)置鍵值:?

set key value? ? ? ? ? ? ?例如:set 'test1' 'hello'

設(shè)置帶過期時間的鍵值,以秒為單位

setex key seconds value? ? ?例如:setex 'test2'? 20 'hello2'?設(shè)置一個20秒后過期的鍵值

修改過期時間

expire key?時間(就是數(shù)字,以秒為單位)

expire test1

查看有效時間

ttL test1

設(shè)置多個鍵值

mset key value key value? ? 例如:mset 'test3' 'hello3' 'test4' 'hello4'

獲取單個(類似查詢)

get key? ? ? 根據(jù)鍵獲取,不存在此鍵則返回nil

例子:get test1

獲取多個

mget key key

例子: mget test1 test2

運(yùn)算

要求值是數(shù)字

incr(默認(rèn)加1)

例子: incr test1

incrby(加整數(shù))

例子:incrby test 6? ? 就是加6

decr(默認(rèn)減1)

例子:decr test5

decrby(減整數(shù))

decrby test5 6?? ?? ? 就是減6

追加

append key value

例子:append test1 world

獲取長度

strlen key

strlen test1

查找鍵(支持正則表達(dá)式)

查找全部: keys *

正則: keys 'test0'

判斷是否存在

exists key ...

例子: exists test1

查看鍵對應(yīng)的value類型

type key

刪除鍵值

del key

Hash

用于儲存對象,對象的格式為鍵值對

設(shè)置單個屬性

hset run10 'name' 'zhang'? ? ? ?就是相當(dāng)于在字典里再套個字典?run10的值就是?name?和zhang

設(shè)置多個屬性

hmset run10 'name' 'zhang' 'name1' 'zhao'? ?

獲取一個屬性的值

hget run10 name

獲取多個屬性的值

hmget run10 name name1

獲取所有屬性和值

hgetall key

獲取所有屬性

hkeys key

返回包含屬性的個數(shù)

hlen key

獲取所有值

hvals key

判斷屬性是否存在

hexists key name

刪除屬性和值

hdel run10 name

返回值得字符串長度

hstrlen key? name


redis list set zset


list

按照插入順序排序,在頭部或者末尾添加?元素

在頭部插入數(shù)據(jù)

lpush key value ...

在尾部插入數(shù)據(jù)

rpush run 100? ? ??

在一個元素前或者后插入新元素

linsert run8 before heng zhang? ? ? 在heng前面添加zhang

linsert run8 after heng zhao? ? ? ? ? ?在heng后面添加zhao

在指定索引添加或者修改數(shù)據(jù)

lset key index value

lset run8 2 heng? ? 在run8第二位索引添加heng或者修改heng

獲取(查看)

移除并且返回key相對應(yīng)的第一個元素

Lpop key?

移除并返回key相對應(yīng)的最后一個元素

rpop key

查看指定范圍的數(shù)據(jù)

lrange key start stop?

lrange run8 0 -1? ? 這是查看所有這個范圍的數(shù)據(jù)

裁剪

ltrim key start stop? ? ?裁剪這個范圍的數(shù)據(jù)? ?(不知道有啥作用)

返回儲存在key里的list長度

llen key

返回列表里索引對應(yīng)的元素

lindex key index

lindex run8 0

set

無序集合,元素具有唯一性,不重復(fù),元素類型為string

添加元素

sadd run1 heng

返回key集合所有的元素

smembers key?

返回集合元素個數(shù)

scard key

求多個集合交集(就是相同的元素)

sinter run run1 run2

求某幾個與其他集合的差集(就是不一樣的元素)

sdiff run run1

求多個集合的合集(就是全部)

sunion run1 run2??

判斷元素是否在集合中

sismember run2 heng

zset

sorted set?有序集合,元素為string類型,元素唯一

每個元素都會關(guān)聯(lián)一個double類型的score,表示權(quán)重,通過權(quán)重將元素從小到大排序,score可以重復(fù)

添加

zadd key score? member

zadd run8 2 zhang

返回指定范圍內(nèi)的元素

zrange run8 0 -1

返回元素個數(shù)

zcard run2

返回有序集key中,score值在min和max之間的成員個數(shù)

zcount run8 5? 20

返回有序集key中,成員member的score值

zscore run8 heng

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容