Jelajahi Sumber

添加cache接口

huangrf 6 tahun lalu
induk
melakukan
b7ea2cd2d8
1 mengubah file dengan 34 tambahan dan 24 penghapusan
  1. 34 24
      third/wx/global.go

+ 34 - 24
third/wx/global.go

@@ -3,12 +3,12 @@ package wx
 import (
 	"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
 	"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
-	"strconv"
+	"sync"
 	"time"
 
 	"github.com/silenceper/wechat"
-	"github.com/silenceper/wechat/oauth"
 	"github.com/silenceper/wechat/cache"
+	"github.com/silenceper/wechat/oauth"
 )
 
 var wc *wechat.Wechat
@@ -29,7 +29,7 @@ var wxConfig struct {
 }
 
 func init() {
-	c := &LocalCache{}
+	c := getLocalCache()
 	wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
 	wxConfig.AppId = config.AppConfig.GetKey("appid")					// 公众号appid
 	wxConfig.AppSecret = config.AppConfig.GetKey("appsecret")			// 公众号appsecret
@@ -50,37 +50,47 @@ func init() {
 /*********************local cache*************************/
 type LocalCache struct {
 	cache.Cache
-	m map[string]string
+	m map[string]interface{}
+	mutex sync.Mutex
 }
 
-func (this *LocalCache) Get(key string) interface{} {
-	if this.m == nil {
-		return nil
-	}
-
-	tokenTime := this.m["timeout"]
-	if tokenTime == "" {
-		return nil
-	}
+func getLocalCache()*LocalCache{
+	lc := &LocalCache{}
+	lc.m = make(map[string]interface{})
 
-	tInt64, _ := strconv.ParseInt(tokenTime, 10, 64)
+	return lc
+}
+func (lc *LocalCache)Get(key string) interface{}{
+	lc.mutex.Lock()
+	defer lc.mutex.Unlock()
 
-	if time.Now().After(time.Unix(tInt64, 0)) {
+	if o, e := lc.m[key]; !e{
 		return nil
+	}else{
+		return o
 	}
-
-	return this.m[key]
 }
+func (lc *LocalCache)Set(key string, val interface{}, timeout time.Duration) error{
+	lc.mutex.Lock()
+	defer lc.mutex.Unlock()
 
-func (this *LocalCache) Set(key string, val interface{}, timeout time.Duration) error {
-	if this.m == nil {
-		this.m = make(map[string]string)
-	}
+	lc.m[key] = val
+	return nil
+}
+func (lc *LocalCache)IsExist(key string) bool{
+	lc.mutex.Lock()
+	defer lc.mutex.Unlock()
 
-	t := time.Now().Add(timeout).Unix()
+	_, e := lc.m[key]
+	return e
+}
+func (lc *LocalCache)Delete(key string) error {
+	lc.mutex.Lock()
+	defer lc.mutex.Unlock()
 
-	this.m["timeout"] = strconv.FormatInt(t, 10)
-	this.m[key] = val.(string)
+	if _, e := lc.m[key]; e {
+		delete(lc.m, key)
+	}
 	return nil
 }
 /*********************************************************/