val := redisClient.Get("Fan")if val ==nil{
fmt.Println("Value get error")}
3s過后
time.Sleep(3*time.Second)
fmt.Println("3s passing")
val = redisClient.Get("Fan")
fmt.Println("name ", val)
結(jié)果
第一個(gè)是在3s內(nèi),是可以打印出來的。
第二個(gè)是過了3s的,已經(jīng)是nil了。
del 操作與 get 操作類似
3.2 Incr & Incrby
3.2.1 Incr 加 1
redisClient.Set("Fan","1",3*time.Second)
val := redisClient.Get("Fan")if val ==nil{
fmt.Println("Value get error")}
fmt.Println("Incr before:", val)
num := redisClient.Incr("Fan")
fmt.Println("Incr after:", num)
結(jié)果
3.2.2 Incrby 加 val
Incrby(key, integer):名稱為key的string增加integer
redisClient.Set("One","1",3*time.Second)
val := redisClient.Get("One")if val ==nil{
fmt.Println("Value get error")}
fmt.Println("IncrBy before:", val)
num := redisClient.IncrBy("One",2)
fmt.Println("IncrBy after:", num)
結(jié)果
3.3 MSet & MGet
設(shè)置多個(gè) key value
MSet(key1, value1, key2, value2, key3, value3)
獲取多個(gè)key
MGet(key1, key2, key3)
redisClient.MSet("Fan","One","1","2","key1","value1")
val := redisClient.MGet("Fan","1","key1")if val ==nil{
fmt.Println("Value get error")}
fmt.Println(val)
val := redisClient.MGet("Fan","1","key1")if val ==nil{
fmt.Println("Value get error")}
fmt.Println(val)
res := redisClient.Del("1")
fmt.Println(res)
已經(jīng)沒有這個(gè)value了
4. List 操作
4.1 RPush
在名稱為key的list尾添加一個(gè)值為value的元素
val:=redisClient.RPush("F",[]string{"One","Two","Three","Four"})if val ==nil{
fmt.Println("Value get error")}
fmt.Println(val)
4.2 LLen & LRange
LLen:返回名稱為key的list的長(zhǎng)度
LRange:返回名稱為key的list中start至end之間的元素
val:=redisClient.RPush("F",[]string{"One","Two","Three","Four"})if val ==nil{
fmt.Println("Value get error")}
fmt.Println("F List:",val)
val = redisClient.LLen("F")
fmt.Println("len:",val)
res := redisClient.LRange("F",0,2)
fmt.Println("LRange:",res)