11package me .chanjar .weixin .mp .api ;
22
3- import me .chanjar .weixin .common .bean .WxAccessToken ;
4- import me .chanjar .weixin .common .util .ToStringUtils ;
5- import me .chanjar .weixin .common .util .http .ApacheHttpClientBuilder ;
63import redis .clients .jedis .Jedis ;
74
8- import javax .net .ssl .SSLContext ;
9- import java .io .File ;
10- import java .util .concurrent .locks .Lock ;
11- import java .util .concurrent .locks .ReentrantLock ;
12-
135/**
146 * 基于Redis的微信配置provider
157 *
168 * @author lly835
179 */
18- public class WxMpInRedisConfigStorage implements WxMpConfigStorage {
10+ @ SuppressWarnings ("hiding" )
11+ public class WxMpInRedisConfigStorage extends WxMpInMemoryConfigStorage {
1912
2013 private final static String ACCESS_TOKEN_KEY = "wechat_access_token_" ;
2114
2215 private final static String JSAPI_TICKET_KEY = "wechat_jsapi_ticket_" ;
2316
2417 private final static String CARDAPI_TICKET_KEY = "wechat_cardapi_ticket_" ;
2518
26- protected volatile String appId ;
27- protected volatile String secret ;
28- protected volatile String partnerId ;
29- protected volatile String partnerKey ;
30- protected volatile String notifyURL ;
31- protected volatile String tradeType ;
32- protected volatile String token ;
33- protected volatile String accessToken ;
34- protected volatile String aesKey ;
35- protected volatile long expiresTime ;
36-
37- protected volatile String oauth2redirectUri ;
38-
39- protected volatile String httpProxyHost ;
40- protected volatile int httpProxyPort ;
41- protected volatile String httpProxyUsername ;
42- protected volatile String httpProxyPassword ;
43-
44- protected volatile String jsapiTicket ;
45- protected volatile long jsapiTicketExpiresTime ;
46-
47- protected volatile String cardApiTicket ;
48- protected volatile long cardApiTicketExpiresTime ;
49-
50- protected Lock accessTokenLock = new ReentrantLock ();
51- protected Lock jsapiTicketLock = new ReentrantLock ();
52- protected Lock cardApiTicketLock = new ReentrantLock ();
53-
54- /**
55- * 临时文件目录
56- */
57- protected volatile File tmpDirFile ;
58-
59- protected volatile SSLContext sslContext ;
60-
61- protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder ;
62-
6319 protected Jedis jedis ;
6420
6521 @ Override
6622 public String getAccessToken () {
6723 return jedis .get (ACCESS_TOKEN_KEY .concat (appId ));
6824 }
6925
70- @ Override
71- public Lock getAccessTokenLock () {
72- return this .accessTokenLock ;
73- }
74-
7526 @ Override
7627 public boolean isAccessTokenExpired () {
7728 return getAccessToken () == null ? true : false ;
7829 }
7930
80- @ Override
81- public synchronized void updateAccessToken (WxAccessToken accessToken ) {
82- updateAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
83- }
84-
85- @ Override
31+ @ Override
8632 public synchronized void updateAccessToken (String accessToken , int expiresInSeconds ) {
87- this .accessToken = accessToken ;
88- this .expiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
8933 jedis .set (ACCESS_TOKEN_KEY .concat (appId ), accessToken );
9034 jedis .expire (ACCESS_TOKEN_KEY .concat (appId ), expiresInSeconds - 200 );
9135 }
9236
9337 @ Override
9438 public void expireAccessToken () {
95- this . expiresTime = 0 ;
39+ jedis . expire ( ACCESS_TOKEN_KEY . concat ( appId ), 0 ) ;
9640 }
9741
9842 @ Override
9943 public String getJsapiTicket () {
100- return this .jsapiTicket ;
101- }
102-
103- public void setJsapiTicket (String jsapiTicket ) {
104- this .jsapiTicket = jsapiTicket ;
105- }
106-
107- @ Override
108- public Lock getJsapiTicketLock () {
109- return this .jsapiTicketLock ;
44+ return jedis .get (JSAPI_TICKET_KEY .concat (appId ));
11045 }
11146
11247 @ Override
@@ -116,194 +51,40 @@ public boolean isJsapiTicketExpired() {
11651
11752 @ Override
11853 public synchronized void updateJsapiTicket (String jsapiTicket , int expiresInSeconds ) {
119- this .jsapiTicket = jsapiTicket ;
120- // 预留200秒的时间
121- this .jsapiTicketExpiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
122- jedis .set (JSAPI_TICKET_KEY .concat (appId ), accessToken );
54+ jedis .set (JSAPI_TICKET_KEY .concat (appId ), jsapiTicket );
12355 jedis .expire (JSAPI_TICKET_KEY .concat (appId ), expiresInSeconds - 200 );
12456 }
12557
12658 @ Override
12759 public void expireJsapiTicket () {
128- this . jsapiTicketExpiresTime = 0 ;
60+ jedis . expire ( JSAPI_TICKET_KEY . concat ( appId ), 0 ) ;
12961 }
13062
13163 /**
13264 * 卡券api_ticket
13365 */
13466 @ Override
13567 public String getCardApiTicket () {
136- return this .cardApiTicket ;
137- }
138-
139- @ Override
140- public Lock getCardApiTicketLock () {
141- return this .cardApiTicketLock ;
68+ return jedis .get (CARDAPI_TICKET_KEY .concat (appId ));
14269 }
14370
14471 @ Override
14572 public boolean isCardApiTicketExpired () {
146- return System . currentTimeMillis () > this . cardApiTicketExpiresTime ;
73+ return getCardApiTicket () == null ? true : false ;
14774 }
14875
14976 @ Override
15077 public synchronized void updateCardApiTicket (String cardApiTicket , int expiresInSeconds ) {
151- this .cardApiTicket = cardApiTicket ;
152- // 预留200秒的时间
153- this .cardApiTicketExpiresTime = System .currentTimeMillis () + (expiresInSeconds - 200 ) * 1000L ;
154- jedis .set (CARDAPI_TICKET_KEY .concat (appId ), accessToken );
78+ jedis .set (CARDAPI_TICKET_KEY .concat (appId ), cardApiTicket );
15579 jedis .expire (CARDAPI_TICKET_KEY .concat (appId ), expiresInSeconds - 200 );
15680 }
15781
15882 @ Override
15983 public void expireCardApiTicket () {
160- this .cardApiTicketExpiresTime = 0 ;
161- }
162-
163- @ Override
164- public String getAppId () {
165- return this .appId ;
166- }
167-
168- public void setAppId (String appId ) {
169- this .appId = appId ;
170- }
171-
172- @ Override
173- public String getSecret () {
174- return this .secret ;
175- }
176-
177- public void setSecret (String secret ) {
178- this .secret = secret ;
179- }
180-
181- @ Override
182- public String getToken () {
183- return this .token ;
184- }
185-
186- public void setToken (String token ) {
187- this .token = token ;
188- }
189-
190- @ Override
191- public long getExpiresTime () {
192- return this .expiresTime ;
193- }
194-
195- public void setExpiresTime (long expiresTime ) {
196- this .expiresTime = expiresTime ;
197- }
198-
199- @ Override
200- public String getAesKey () {
201- return this .aesKey ;
202- }
203-
204- public void setAesKey (String aesKey ) {
205- this .aesKey = aesKey ;
206- }
207-
208- @ Override
209- public String getOauth2redirectUri () {
210- return this .oauth2redirectUri ;
211- }
212-
213- public void setOauth2redirectUri (String oauth2redirectUri ) {
214- this .oauth2redirectUri = oauth2redirectUri ;
215- }
216-
217- @ Override
218- public String getHttpProxyHost () {
219- return this .httpProxyHost ;
220- }
221-
222- public void setHttpProxyHost (String httpProxyHost ) {
223- this .httpProxyHost = httpProxyHost ;
224- }
225-
226- @ Override
227- public int getHttpProxyPort () {
228- return this .httpProxyPort ;
229- }
230-
231- public void setHttpProxyPort (int httpProxyPort ) {
232- this .httpProxyPort = httpProxyPort ;
233- }
234-
235- @ Override
236- public String getHttpProxyUsername () {
237- return this .httpProxyUsername ;
238- }
239-
240- public void setHttpProxyUsername (String httpProxyUsername ) {
241- this .httpProxyUsername = httpProxyUsername ;
242- }
243-
244- @ Override
245- public String getHttpProxyPassword () {
246- return this .httpProxyPassword ;
247- }
248-
249- public void setHttpProxyPassword (String httpProxyPassword ) {
250- this .httpProxyPassword = httpProxyPassword ;
251- }
252-
253- @ Override
254- public String toString () {
255- return ToStringUtils .toSimpleString (this );
256- }
257-
258- public void setPartnerId (String partnerId ) {
259- this .partnerId = partnerId ;
260- }
261-
262- public void setPartnerKey (String partnerKey ) {
263- this .partnerKey = partnerKey ;
264- }
265-
266-
267- public String getNotifyURL () {
268- return notifyURL ;
269- }
270-
271- public void setNotifyURL (String notifyURL ) {
272- this .notifyURL = notifyURL ;
273- }
274-
275- public String getTradeType () {
276- return tradeType ;
277- }
278-
279- public void setTradeType (String tradeType ) {
280- this .tradeType = tradeType ;
281- }
282-
283- @ Override
284- public File getTmpDirFile () {
285- return this .tmpDirFile ;
286- }
287-
288- public void setTmpDirFile (File tmpDirFile ) {
289- this .tmpDirFile = tmpDirFile ;
290- }
291-
292- @ Override
293- public ApacheHttpClientBuilder getApacheHttpClientBuilder () {
294- return this .apacheHttpClientBuilder ;
295- }
296-
297- public void setApacheHttpClientBuilder (ApacheHttpClientBuilder apacheHttpClientBuilder ) {
298- this .apacheHttpClientBuilder = apacheHttpClientBuilder ;
299- }
300-
301- @ Override
302- public boolean autoRefreshToken () {
303- return true ;
84+ jedis .expire (CARDAPI_TICKET_KEY .concat (appId ), 0 );
30485 }
30586
30687 public void setJedis (Jedis jedis ) {
307- this .jedis = jedis ;
308- }
309- }
88+ this .jedis = jedis ;
89+ }
90+ }
0 commit comments