golang-redis之string型別簡單操作
阿新 • • 發佈:2019-02-09
1:安裝redigo
go get github.com/garyburd/redigo/redis
2:連線redis(通過ip埠)
c, err := redis.Dial("tcp", "192.168.2.225:6379")
if err != nil {
fmt.Println("connect to redis err", err.Error())
return
}
defer c.Close()
3:命令執行
n,err := c.Do("SET","key","value") //寫
result,err := redis.String(c.Do ("GET","key"))//讀
4:string型別常見操作
set(key, value):給資料庫中名稱為key的string賦予值value
get(key):返回資料庫中名稱為key的string的value
getset(key, value):給名稱為key的string賦予上一次的value
mget(key1, key2,…, key N):返回庫中多個string的value
setnx(key, value):新增string,名稱為key,值為value
setex(key, time, value):向庫中新增string,設定過期時間time
mset(key N, value N):批量設定多個string的值
msetnx(key N, value N):如果所有名稱為key i的string都不存在
incr(key):名稱為key的string增1操作
incrby(key, integer):名稱為key的string增加integer
decr(key):名稱為key的string減1操作
decrby(key, integer):名稱為key的string減少integer
append(key, value):名稱為key的string的值附加value
substr(key, start, end):返回名稱為key的string的value 的子串
5:示例
5.1:SET-GET示例
設定key-value(“myname”,”Alice”)
_,err = c.Do("set","myname","Alice")
if err != nil{
fmt.Println("redis set failed",err.Error())
}
myname,err := redis.String(c.Do("GET","myname"))
if err!= nil{
fmt.Println("redis get failed",err.Error())
}else{
fmt.Println("GET myname",myname)
}
5.2:GETSET示例
呼叫GETSET時,返回的是key值原來的value值,呼叫之後,該值被修改為現有的值
mydb,err = redis.String(c.Do("GETSET","mydb","mysql"))
if err != nil{
fmt.Println("getset failed",err.Error())
}
fmt.Println("get before",mydb)
mydb,err = redis.String(c.Do("get","mydb"))
if err == nil{
fmt.Println("mydb:",mydb)
}
5.3:SETNX示例
_,err = c.Do("setnx","foo","bluegogo")
if err != nil{
fmt.Println("setnx failed",err.Error())
}
foo,err := redis.String(c.Do("get","foo"))
if err == nil{
fmt.Println("setnx value",foo)
}
5.4:MSETNX-MGET示例
MSETNX設定不存在的key的值,如果該值存在,則值不會修改
_,err=c.Do("msetnx","bike1","ofo","bike2","bluegogo","bike3","foo")
if err != nil{
fmt.Println("setnx failed")
}
var tmpbike1 string
var tmpbike2 string
var tmpbike3 string
array,err =redis.Values( c.Do("MGET","bike1","bike2","bike3"))
if err != nil{
fmt.Println("mget failed",err.Error())
}else{
if _,err=redis.Scan(array,&tmpbike1,&tmpbike2,&tmpbike3);err == nil{
fmt.Println("mget :",tmpbike1,tmpbike2,tmpbike3)
}
}
5.5:MSET-MGET示例
_,err=c.Do("mset","bike1","mongo","bike2","redis","bike3","memcached")
if err != nil{
fmt.Println("mset error",err.Error())
}
array,err =redis.Values( c.Do("MGET","bike1","bike2","bike3"))
if err != nil{
fmt.Println("mget failed",err.Error())
}else{
if _,err=redis.Scan(array,&tmpbike1,&tmpbike2,&tmpbike3);err== nil{
fmt.Println("mget :",tmpbike1,tmpbike2,tmpbike3)
}
}
5.6:SETEX-SET(定時)示例
_,err = c.Do("setex","ofo",10,"xiaoming")
if err != nil{
fmt.Println("setex failed ",err.Error())
}
ofo,err := redis.String(c.Do("get","ofo"))
if err == nil{
fmt.Println("setex ofo",ofo)
}
time.Sleep(11 *time.Second)
ofo,err = redis.String(c.Do("get","ofo"))
if err == nil{
fmt.Println("setex ofo",ofo)
}else{
fmt.Println("time out get nil")
}
_,err = c.Do("SET","myage",10,"EX","10")
if nil != err{
fmt.Println("redis set failed",err.Error())
}
isExist,err := redis.Bool(c.Do("EXISTS","myage"))
if err != nil{
fmt.Println("error",err.Error())
}else{
fmt.Println("exists or not ",isExist)
}
time.Sleep(11*time.Second)
myage,err := redis.String(c.Do("GET","myage"))
if err != nil{
fmt.Println("redis get failed",err.Error())
}else{
fmt.Println("get myage",myage)
}
isExist,err = redis.Bool(c.Do("EXISTS","myage"))
if err != nil{
fmt.Println("error",err.Error())
}else{
fmt.Println("10s after exists or not ",isExist)
}
5.7:INCR-INCRBY示例
_,err = c.Do("SET","myage",10)
if err != nil{
fmt.Println("set myage failed",err.Error())
}
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("before myage",myage)
}
_,err=c.Do("INCR","myage")
if err != nil{
fmt.Println("incr error",err.Error())
}
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("after myage incr 1",myage)
}
_,err=c.Do("INCRBY","myage",10)
if err != nil{
fmt.Println("incrby error",err.Error())
}
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("after myage incr 10",myage)
}
5.8:DECR-DECRBY示例
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("before myage",myage)
}
_,err=c.Do("DECR","myage")
if err != nil{
fmt.Println("incr error",err.Error())
}
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("after myage DECR 1",myage)
}
_,err=c.Do("DECRBY","myage",10)
if err != nil{
fmt.Println("incrby error",err.Error())
}
myage,err = redis.String(c.Do("GET","myage"))
if err == nil{
fmt.Println("after myage DECR 10",myage)
}
5.9:APPEND示例
_,err = c.Do("APPEND","bike1","xiaomingbike")
if err != nil{
fmt.Println("append error",err.Error())
}
bike1,err := redis.String(c.Do("GET","bike1"))
if err == nil{
fmt.Println("get bike is",bike1)
}
5.10:SUBSTR示例
str,err := redis.String(c.Do("substr","bike1",1,4))
if err != nil{
fmt.Println("substr get err",err.Error())
}
fmt.Println("substr is",str)
5.11:DEL示例
_,err = c.Do("DEL","myname")
if err != nil{
fmt.Println("redis delete failed",err.Error())
}
myname,err = redis.String(c.Do("GET","myname"))
if err != nil{
fmt.Printf("redis get failed %s delete success\n",err.Error())
}else{
fmt.Println("delete failed")
}