数据库缓存更新
This commit is contained in:
Vendored
+73
-44
@@ -42,42 +42,20 @@ func (this *CacheRedis) reCon()bool{
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
reData:= &Obj{}
|
||||
var err error
|
||||
if this.conn==nil{
|
||||
re:=this.reCon()
|
||||
if !re{
|
||||
return reData
|
||||
func (this *CacheRedis) del(key string) {
|
||||
del:=strings.Index(key,"*")
|
||||
if del!=-1{
|
||||
val, err := redis.Strings(this.conn.Do("KEYS", key))
|
||||
if err!=nil{
|
||||
return
|
||||
}
|
||||
}
|
||||
//查询缓存
|
||||
if len(data) == 0 {
|
||||
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
if err != nil {
|
||||
reData.Data=nil
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
}
|
||||
}
|
||||
|
||||
return reData
|
||||
}
|
||||
this.conn.Send("MULTI")
|
||||
for i, _ := range val {
|
||||
this.conn.Send("DEL", val[i])
|
||||
}
|
||||
return reData
|
||||
|
||||
}
|
||||
tim := int64(0)
|
||||
//删除缓存
|
||||
if len(data) == 1 && data[0] == nil {
|
||||
|
||||
_,err=this.conn.Do("DEL",key)
|
||||
this.conn.Do("EXEC")
|
||||
}else{
|
||||
_,err:=this.conn.Do("DEL",key)
|
||||
if err!=nil{
|
||||
this.Error.SetError(err)
|
||||
_,err=this.conn.Do("PING")
|
||||
@@ -87,6 +65,65 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//key value ,时间为时间戳
|
||||
func (this *CacheRedis) set(key string, value string, time int64) {
|
||||
_,err:=this.conn.Do("SET",key,value,"EX",ObjToStr(time))
|
||||
if err!=nil{
|
||||
|
||||
this.Error.SetError(err)
|
||||
_,err=this.conn.Do("PING")
|
||||
if err!=nil{
|
||||
if this.reCon(){
|
||||
_,err=this.conn.Do("SET",key,value,"EX",ObjToStr(time))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (this *CacheRedis) get(key string) *Obj{
|
||||
reData:= &Obj{}
|
||||
var err error
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
if err != nil {
|
||||
reData.Data=nil
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
||||
}
|
||||
}
|
||||
|
||||
return reData
|
||||
}
|
||||
}
|
||||
return reData
|
||||
}
|
||||
|
||||
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
reData:= &Obj{}
|
||||
if this.conn==nil{
|
||||
re:=this.reCon()
|
||||
if !re{
|
||||
return reData
|
||||
}
|
||||
}
|
||||
//查询缓存
|
||||
if len(data) == 0 {
|
||||
|
||||
reData=this.get(key)
|
||||
return reData
|
||||
|
||||
}
|
||||
tim := int64(0)
|
||||
//删除缓存
|
||||
if len(data) == 1 && data[0] == nil {
|
||||
this.del(key)
|
||||
return reData
|
||||
}
|
||||
//添加缓存
|
||||
@@ -108,17 +145,9 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
}
|
||||
}
|
||||
|
||||
_,err=this.conn.Do("SET",key,ObjToStr(data[0]),"EX",ObjToStr(tim))
|
||||
if err!=nil{
|
||||
|
||||
this.Error.SetError(err)
|
||||
_,err=this.conn.Do("PING")
|
||||
if err!=nil{
|
||||
if this.reCon(){
|
||||
_,err=this.conn.Do("SET",key,ObjToStr(data[0]),"EX",ObjToStr(tim))
|
||||
}
|
||||
}
|
||||
}
|
||||
this.set(key,ObjToStr(data[0]),tim)
|
||||
|
||||
return reData
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user