前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【微信开发】getAccessToken 和 getJsapi_ticket缓存支持

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

作者头像
冷冷
发布2018-02-08 11:18:02
8330
发布2018-02-08 11:18:02
举报
文章被收录于专栏:冷冷冷冷冷冷

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"/>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档