使用guava cache实现List的删改查。

在进行了几天的查阅资料之后,决定使用guava 来实现List的操作。


直接上代码。

1、首先,我们实现了一个guava 的工具类,在后面使用时,直接调用即可。

package com.learning.www.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.learning.www.entity.ZphInfo;


public class GuavaCache {
	private static Logger logger = LoggerFactory.getLogger(GuavaCache.class);
	
	private static LoadingCache<Integer, ZphInfo> cache = CacheBuilder.newBuilder()
			.maximumSize(10)//最多存放十个数据
			.expireAfterWrite(10, TimeUnit.MINUTES)//缓存,10秒之后进行回收
			.recordStats()//开启,记录状态数据功能
			.build(new CacheLoader<Integer, ZphInfo>() {
				//数据加载,默认返回-1,也可以是查询操作,如从DB查询
				// 默认数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载。
				ZphInfo zph = new ZphInfo();
				@Override
				public ZphInfo load(Integer key) throws Exception {
					// TODO Auto-generated method stub
					logger.info("未得到数据,返回-1");
					return zph;
				}
			});
	// 设置缓存的key和value
	public static void setKey(Integer key,ZphInfo value) {
		cache.put(key, value);
	}

	public static int delcache(Integer key) {
		cache.invalidate(key);
		return 1;
	}
	// 根据key获取缓存
	public static ZphInfo getKey(Integer key) {
		ZphInfo value = null;
		
		try {
			value = cache.get(key);
			if(null == value) {
				return null;
			}
			//logger.info(value.toString());
			return value;
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			logger.info("获取缓存出错");
			e.printStackTrace();
		}
		return null;
	}
	// 查看缓存中所有的数据
	public static List<ZphInfo> getAllInfo() {
		
		ConcurrentMap<Integer, ZphInfo> map = cache.asMap();
		Collection<ZphInfo> list = map.values();
		List<ZphInfo> zphinfolist = new ArrayList<ZphInfo>();
		for (ZphInfo zphInfo : list) {
			zphinfolist.add(zphInfo);
		}
		
		return zphinfolist;		
	}
}

2、在ZphInfoServiceImpl中使用工具类,实现:在缓存中有数据时,调用缓存中的数据,若缓存中没有数据,则直接调用数据库。

// 这边将List存入缓存中	
public List<ZphInfo> getZphInfoList() {
		// 先从缓存中取数据,如果没有再从数据库取
		if(null != GuavaCache.getAllInfo() && !GuavaCache.getAllInfo().isEmpty()) {
			return GuavaCache.getAllInfo();
		}
		List<ZphInfo> zphinfolist = zphinfomapper.getZphInfoList();
		for (ZphInfo zphInfo : zphinfolist) {
			GuavaCache.setKey(zphInfo.getId(), zphInfo);
		}
		
		return zphinfolist;
	}

	public int deleteZphInfo(int id) {
		// 删除缓存中的数据,再删除数据库中的数据
		GuavaCache.delcache(id);
		int ret = zphinfomapper.deleteZphInfo(id);
		return ret;
	}

	public ZphInfo getZphInfoById(int id) {
		// 查找缓存,其中 招聘会标题不能为null
		if(null != GuavaCache.getKey(id).getTitle() &&     !GuavaCache.getKey(id).getTitle().equals("")) {
			//logger.info(GuavaCache.getKey(id).toString());
			return GuavaCache.getKey(id);
		}
		return zphinfomapper.getZphInfoById(id);
		
	}

这样就大体上实现了List的缓存功能。

猜你喜欢

转载自blog.csdn.net/qq_39847344/article/details/84755417
今日推荐