mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 02:28:25 +08:00 
			
		
		
		
	Fix issue #335: 重构WxMpInRedisConfigStorage,改用连接池
This commit is contained in:
		 nickwongwong
					nickwongwong
				
			
				
					committed by
					
						 Binary Wang
						Binary Wang
					
				
			
			
				
	
			
			
			 Binary Wang
						Binary Wang
					
				
			
						parent
						
							3875635a8a
						
					
				
				
					commit
					0e580a05ff
				
			| @ -1,11 +1,12 @@ | ||||
| package me.chanjar.weixin.mp.api; | ||||
|  | ||||
| import redis.clients.jedis.Jedis; | ||||
| import redis.clients.jedis.JedisPool; | ||||
|  | ||||
| /** | ||||
|  * 基于Redis的微信配置provider | ||||
|  * | ||||
|  * @author lly835 | ||||
|  * @author nickwong | ||||
|  */ | ||||
| @SuppressWarnings("hiding") | ||||
| public class WxMpInRedisConfigStorage extends WxMpInMemoryConfigStorage { | ||||
| @ -16,75 +17,115 @@ public class WxMpInRedisConfigStorage extends WxMpInMemoryConfigStorage { | ||||
|  | ||||
|   private final static String CARDAPI_TICKET_KEY = "wechat_cardapi_ticket_"; | ||||
|  | ||||
|   protected Jedis jedis; | ||||
|   /** | ||||
|    * 使用连接池保证线程安全 | ||||
|    */ | ||||
|   protected final JedisPool jedisPool; | ||||
|  | ||||
|   private String accessTokenKey; | ||||
|  | ||||
|   private String jsapiTicketKey; | ||||
|  | ||||
|   private String cardapiTicketKey; | ||||
|  | ||||
|   public WxMpInRedisConfigStorage(JedisPool jedisPool) { | ||||
|     this.jedisPool = jedisPool; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 每个公众号生成独有的存储key | ||||
|    * | ||||
|    * @param appId | ||||
|    */ | ||||
|   @Override | ||||
|   public void setAppId(String appId) { | ||||
|     super.setAppId(appId); | ||||
|     this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId); | ||||
|     this.jsapiTicketKey = JSAPI_TICKET_KEY.concat(appId); | ||||
|     this.cardapiTicketKey = CARDAPI_TICKET_KEY.concat(appId); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String getAccessToken() { | ||||
|     return jedis.get(ACCESS_TOKEN_KEY.concat(appId)); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.get(accessTokenKey); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean isAccessTokenExpired() { | ||||
|     return jedis.ttl(ACCESS_TOKEN_KEY.concat(appId)) < 2; | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.ttl(accessTokenKey) < 2; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | ||||
|     jedis.set(ACCESS_TOKEN_KEY.concat(appId), accessToken); | ||||
|     jedis.expire(ACCESS_TOKEN_KEY.concat(appId), expiresInSeconds - 200); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.setex(accessTokenKey, expiresInSeconds - 200, accessToken); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void expireAccessToken() { | ||||
|     jedis.expire(ACCESS_TOKEN_KEY.concat(appId), 0); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.expire(accessTokenKey, 0); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public String getJsapiTicket() { | ||||
|     return jedis.get(JSAPI_TICKET_KEY.concat(appId)); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.get(jsapiTicketKey); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean isJsapiTicketExpired() { | ||||
|     return jedis.ttl(JSAPI_TICKET_KEY.concat(appId)) < 2; | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.ttl(jsapiTicketKey) < 2; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | ||||
|     jedis.set(JSAPI_TICKET_KEY.concat(appId), jsapiTicket); | ||||
|     jedis.expire(JSAPI_TICKET_KEY.concat(appId), expiresInSeconds - 200); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.setex(jsapiTicketKey, expiresInSeconds - 200, jsapiTicket); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void expireJsapiTicket() { | ||||
|     jedis.expire(JSAPI_TICKET_KEY.concat(appId), 0); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.expire(jsapiTicketKey, 0); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 卡券api_ticket | ||||
|    */ | ||||
|   @Override | ||||
|   public String getCardApiTicket() { | ||||
|     return jedis.get(CARDAPI_TICKET_KEY.concat(appId)); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.get(cardapiTicketKey); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean isCardApiTicketExpired() { | ||||
|     return jedis.ttl(CARDAPI_TICKET_KEY.concat(appId)) < 2; | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       return jedis.ttl(cardapiTicketKey) < 2; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public synchronized void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) { | ||||
|     jedis.set(CARDAPI_TICKET_KEY.concat(appId), cardApiTicket); | ||||
|     jedis.expire(CARDAPI_TICKET_KEY.concat(appId), expiresInSeconds - 200); | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.setex(cardapiTicketKey, expiresInSeconds - 200, cardApiTicket); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void expireCardApiTicket() { | ||||
|     jedis.expire(CARDAPI_TICKET_KEY.concat(appId), 0); | ||||
|   } | ||||
|  | ||||
|   public void setJedis(Jedis jedis) { | ||||
|     this.jedis = jedis; | ||||
|     try (Jedis jedis = this.jedisPool.getResource()) { | ||||
|       jedis.expire(cardapiTicketKey, 0); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user