Browse Source

mem cache

ls 3 years ago
parent
commit
150ab906f6
5 changed files with 23 additions and 12 deletions
  1. 5 5
      wechat/client.go
  2. 3 3
      wechat/mini.go
  3. 1 1
      wechat/response.go
  4. 7 3
      wechat/server.go
  5. 7 0
      wechat/structure.go

+ 5 - 5
wechat/client.go

@@ -21,11 +21,11 @@ func keyToken(appid string) string {
 // NewClient new client
 func NewClient(appID, appSecret, token, encodingAESKey string) *Client {
 	key := key(appID)
-	if v, ok := cache.Load(key); ok {
+	if v, ok := memcache.Load(key); ok {
 		return v.(*Client)
 	}
 	c := &Client{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey}
-	cache.Store(key, c)
+	memcache.Store(key, c)
 	return c
 }
 
@@ -56,9 +56,9 @@ func (wc *Client) getToken() (token string, err error) {
 		wc.AccessToken = accessToken
 		token = wc.AccessToken
 		key := key(wc.AppID)
-		cache.Store(key, wc)
+		memcache.Store(key, wc)
 		key = keyToken(wc.AppID)
-		cache.Store(key, accessToken)
+		//cache.Store(key, accessToken)
 	}
 	return
 }
@@ -84,7 +84,7 @@ func (wc *Client) getTicket(now int64) (ticket string, err error) {
 		wc.Ticket = ticket
 
 		key := key(wc.AppID)
-		cache.Store(key, wc)
+		memcache.Store(key, wc)
 	}
 
 	return

+ 3 - 3
wechat/mini.go

@@ -14,11 +14,11 @@ func keyMini(appid string) string {
 // NewMiniClient new mini client
 func NewMiniClient(appID, appSecret string) *MiniClient {
 	key := keyMini(appID)
-	if v, ok := cache.Load(key); ok {
+	if v, ok := memcache.Load(key); ok {
 		return v.(*MiniClient)
 	}
 	c := &MiniClient{AppID: appID, AppSecret: appSecret}
-	cache.Store(key, c)
+	memcache.Store(key, c)
 	return c
 }
 
@@ -63,7 +63,7 @@ func (mc *MiniClient) getToken() (token string, err error) {
 		mc.AccessToken, err = jq.String(`access_token`)
 		token = mc.AccessToken
 		key := keyMini(mc.AppID)
-		cache.Store(key, mc)
+		memcache.Store(key, mc)
 	}
 	return
 }

+ 1 - 1
wechat/response.go

@@ -8,7 +8,7 @@ import (
 )
 
 var (
-	cache sync.Map
+	memcache sync.Map
 )
 
 // ToXML reply message to xml

+ 7 - 3
wechat/server.go

@@ -8,14 +8,18 @@ type Server struct {
 	EncodingAESKey string `json:"encodingaeskey"`
 }
 
+func keyServer(appid string) string {
+	return "wechat:server:" + appid
+}
+
 // NewServer new Server
 func NewServer(appID, appSecret, token, encodingAESKey string) *Server {
-	key := "wechat:server:" + appID
-	if v, ok := cache.Load(key); ok {
+	key := keyServer(appID)
+	if v, ok := memcache.Load(key); ok {
 		return v.(*Server)
 	}
 	s := &Server{AppID: appID, AppSecret: appSecret, Token: token, EncodingAESKey: encodingAESKey}
-	cache.Store(key, s)
+	memcache.Store(key, s)
 	return s
 }
 

+ 7 - 0
wechat/structure.go

@@ -21,6 +21,13 @@ type Client struct {
 	LastTicketTime int64
 }
 
+// ClientToken wechat client token
+type ClientToken struct {
+	AppID         string `json:"appid"`
+	AccessToken   string `json:"access_token"`
+	LastTokenTime int64  `json:"last_token_time"`
+}
+
 // MiniClient wechat mini
 type MiniClient struct {
 	AppID     string `json:"appid"`