增加跨域配置,以及增加配置说明
This commit is contained in:
Vendored
+36
-38
@@ -1,18 +1,18 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
. "go.hoteas.com/hotime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheRedis struct {
|
||||
Host string
|
||||
Pwd string
|
||||
Pwd string
|
||||
Time int64
|
||||
conn redis.Conn
|
||||
tag int64
|
||||
tag int64
|
||||
Error
|
||||
}
|
||||
|
||||
@@ -25,28 +25,28 @@ func (this *CacheRedis) GetTag() int64 {
|
||||
return this.tag
|
||||
}
|
||||
|
||||
func (this *CacheRedis) reCon()bool{
|
||||
func (this *CacheRedis) reCon() bool {
|
||||
var err error
|
||||
this.conn,err=redis.Dial("tcp",this.Host)
|
||||
if err!=nil{
|
||||
this.conn=nil
|
||||
this.conn, err = redis.Dial("tcp", this.Host)
|
||||
if err != nil {
|
||||
this.conn = nil
|
||||
this.Error.SetError(err)
|
||||
return false
|
||||
}
|
||||
|
||||
_,err=this.conn.Do("AUTH",this.Pwd)
|
||||
if err!=nil{
|
||||
this.conn=nil
|
||||
_, err = this.conn.Do("AUTH", this.Pwd)
|
||||
if err != nil {
|
||||
this.conn = nil
|
||||
this.Error.SetError(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (this *CacheRedis) del(key string) {
|
||||
del:=strings.Index(key,"*")
|
||||
if del!=-1{
|
||||
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{
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
this.conn.Send("MULTI")
|
||||
@@ -54,14 +54,14 @@ func (this *CacheRedis) del(key string) {
|
||||
this.conn.Send("DEL", val[i])
|
||||
}
|
||||
this.conn.Do("EXEC")
|
||||
}else{
|
||||
_,err:=this.conn.Do("DEL",key)
|
||||
if err!=nil{
|
||||
} else {
|
||||
_, err := this.conn.Do("DEL", key)
|
||||
if err != nil {
|
||||
this.Error.SetError(err)
|
||||
_,err=this.conn.Do("PING")
|
||||
if err!=nil{
|
||||
if this.reCon(){
|
||||
_,err=this.conn.Do("DEL",key)
|
||||
_, err = this.conn.Do("PING")
|
||||
if err != nil {
|
||||
if this.reCon() {
|
||||
_, err = this.conn.Do("DEL", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,26 +70,25 @@ func (this *CacheRedis) del(key string) {
|
||||
|
||||
//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{
|
||||
_, 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))
|
||||
_, 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{}
|
||||
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
|
||||
reData.Data = nil
|
||||
if !strings.Contains(err.Error(), "nil returned") {
|
||||
this.Error.SetError(err)
|
||||
_, err = this.conn.Do("PING")
|
||||
@@ -106,17 +105,17 @@ func (this *CacheRedis) get(key string) *Obj{
|
||||
}
|
||||
|
||||
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
reData:= &Obj{}
|
||||
if this.conn==nil{
|
||||
re:=this.reCon()
|
||||
if !re{
|
||||
reData := &Obj{}
|
||||
if this.conn == nil {
|
||||
re := this.reCon()
|
||||
if !re {
|
||||
return reData
|
||||
}
|
||||
}
|
||||
//查询缓存
|
||||
if len(data) == 0 {
|
||||
|
||||
reData=this.get(key)
|
||||
reData = this.get(key)
|
||||
return reData
|
||||
|
||||
}
|
||||
@@ -147,8 +146,7 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.set(key,ObjToStr(data[0]),tim)
|
||||
this.set(key, ObjToStr(data[0]), tim)
|
||||
|
||||
return reData
|
||||
|
||||
|
||||
Reference in New Issue
Block a user