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 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
maxElementsInMemory="100"
eternal="false"
timeToLiveSeconds="6000"
overflowToDisk="true"/>