使用google guava插件实现缓存

1、引入google guava依赖

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>21.0</version>
</dependency>

2、在需要作缓存地方加入代码

//缓存60s
//<String, JSONArray>表示缓存的数据结构,类似Map
private LoadingCache<String, JSONArray> cache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS)
	.build(new CacheLoader<String, JSONArray>() {
		@Override
		public JSONArray load(String key) throws Exception {
			//return new RestTemplate().getForEntity("http://127.0.0.1:8080/ebus-app-global/custom/push", JSONArray.class).getBody();
			return restTemplate.getForEntity(pushUrl, JSONArray.class).getBody();
		}
	});

附:完整代码

/**
* <p>Title: TestImplCommandLineRunner.java</p>
* <p>Copyright: Copyright (c) 2017</p>
* <p>Company: lty</p>
*/
package com.lantaiyuan.ebus.task;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.lantaiyuan.ebus.model.PushPlatformGroup;

/**
 * <p>Title: TestImplCommandLineRunner</p>
 * <p>Description: RESTful调用第三方接口</p>
 * <p>Company: lty</p>
 * @author liuhao
 * @date 2017年5月15日 下午3:57:36
 */
@Component
public class RestTask {
	private static final Logger logger = LoggerFactory.getLogger(RestTask.class);
	
	@Autowired
	private RestTemplate restTemplate;

	@Value("${push.url}")
	private String pushUrl;
	
	//缓存60s
	//<String, JSONArray>表示缓存的数据结构,类似Map
	private LoadingCache<String, JSONArray> cache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS)
			.build(new CacheLoader<String, JSONArray>() {
				@Override
				public JSONArray load(String key) throws Exception {
					//return new RestTemplate().getForEntity("http://127.0.0.1:8080/ebus-app-global/custom/push", JSONArray.class).getBody();
					return restTemplate.getForEntity(pushUrl, JSONArray.class).getBody();
				}
			});

	/*********** HTTP GET method *************/
	public List<PushPlatformGroup> invokeUrl() {
		JSONArray jsonArray = null;
		
		try {
			//key名称可任意指定
			jsonArray = cache.get("push");
		} catch (Exception e) {
			logger.error("推送url接口异常!", e);
			return null;
		}

		if (jsonArray == null) {
			return null;
		}

		return JSONObject.parseArray(jsonArray.toJSONString(), PushPlatformGroup.class);
	}

//	public static void main(String[] args) throws ExecutionException, InterruptedException {
        //为便于测试,上面超时时间改为5
//		RestTask restUtil = new RestTask();
//		for(int i = 0 ;i<21 ;i++){
//			TimeUnit.SECONDS.sleep(1);
//			//key名称可任意指定
//			System.err.println(restUtil.cache.get("push"));
//		}
//	}
}

 

转载于:https://my.oschina.net/Howard2016/blog/1058075

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值