【微信开发】 getAccessToken 和 getJsapi_ticket缓存支持


AccessToken   JsapiTicket 获取这个两个参数的接口每天都有上线,所以我们应该使用缓存把它们保存起来,减少对这两个接口的调用


示例: 

/**
	 * 获取access_token
	 * 
	 * @updateDate 2015年8月3日23:22:39 增加对缓存的处理
	 * @return
	 * @throws Exception
	 */
	public static String getAccessToken(String appId, String appSecret)
			throws Exception {
		String accessToken = (String) CacheUtils.get("wxCache", "accessToken");
		if (StringUtils.isBlank(accessToken)
				|| StringUtils.isEmpty(accessToken)) {
			String jsonStr = HttpKit.get(ACCESSTOKEN_URL.concat("&appid=")
					+ appId + "&secret=" + appSecret);
			Map<String, Object> map = JSONObject.parseObject(jsonStr);
			accessToken = map.get("access_token").toString();
			CacheUtils.put("wxCache", "accessToken", accessToken);
		}
		return accessToken;
	}

 

/**
	 * 获得jsapi_ticket(有效期7200秒)
	 * 
	 * @updateDate 2015年8月4日00:00:46 z 增加对缓存的
	 * @param accessToken
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @throws IOException
	 * @throws NoSuchProviderException
	 */
	public static JSONObject getTicket(String accessToken)
			throws InterruptedException, ExecutionException,
			NoSuchAlgorithmException, KeyManagementException, IOException,
			NoSuchProviderException {
		String jsonStr = (String) CacheUtils.get("wxCache", "jsapi_ticket");
		if (StringUtils.isBlank(jsonStr) || StringUtils.isEmpty(jsonStr)) {
			jsonStr = HttpKit.get(JSAPI_TICKET.concat(accessToken));
			CacheUtils.put("wxCache", "jsapi_ticket", jsonStr);
		}
		return JSONObject.parseObject(jsonStr);
	}

 ehcache.xml

<cache name="wxCache" 
maxElementsInMemory="100" 
eternal="false" 
timeToLiveSeconds="6000" 
overflowToDisk="true"/>


转载于:https://my.oschina.net/giegie/blog/487318

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值