码迷,mamicode.com
首页 > 微信 > 详细

微信 getAccessToken 和 getJsapi_ticket缓存支持

时间:2015-08-04 00:55:45      阅读:551      评论:0      收藏:0      [点我收藏+]

标签:

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"/>


微信 getAccessToken 和 getJsapi_ticket缓存支持

标签:

原文地址:http://my.oschina.net/giegie/blog/487318

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!