1. 程式人生 > >使用guava cache實現List的刪改查。

使用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的快取功能。