1. 程式人生 > >redis介紹(一)

redis介紹(一)

字串:string

列表:list

雜湊:hash

集合:set

有序集合:zset

 

set、get、del   key  設定、獲取、刪除key

incr key 設定自增

例如:get key1  取出1,incr key1就是2,再次incr key1     就是3

decr key設定自減

可用為主鍵

incrby key 數:增加幾個數

例如:get key1 取出1,incrby key1  10就是11

 

hset user username luoyunlong

hset user password 123456789

hset user name 張三

取值:

hget user username

hget user password

hget user name

 

hash批量設定與取值(按照欄位取):

設定:

hmset user username luoyunlong password 123456789 name 張三

取值:

hmget user username password name→"luoyunlong" "123456789" "張三"

 

hash一次性取出資料:

hgetall user→"username" "luoyunlong" "password" "123456789" "name" "張三"

 

hdel刪除欄位:

例如上面user裡面有三個欄位,並且都有值

hdel user name→張三被刪除,name欄位被刪除結果是

hgetall user:"username" "luoyunlong" "password" "123456789"

 

hincrby增加數字:

hset user age 20

hincrby user age 2→hget user age→22

 

decr減少數字:

decr age→21

decr age→20

decr age 5→15

decr age 20→-5

 

向尾部增加值:

set str hello

append str "world!"

get str →"helloworld!"

 

獲取字串長度:

strlen str(如果str不存在返回0):0

get str→"helloworld!"

strlen str:11

同時設定/獲取多個鍵值:

mset k1 v1 k2 v2 k3 v3

mget k1→v1

mget k2 k3→v2 v3

 

redis持久化方案:

rdb:儲存速度快,不安全,有時會丟失一部分資料(斷電時)

aof:儲存速度慢,安全,保證資料的完整性(雞肋)