guava cache 를 사용 하여 List 의 삭제 검 사 를 실현 합 니 다.

3309 단어 springboot 캐 시
며칠 간 자 료 를 조회 한 뒤 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 cache = CacheBuilder.newBuilder()
			.maximumSize(10)//        
			.expireAfterWrite(10, TimeUnit.MINUTES)//  ,10       
			.recordStats()//  ,        
			.build(new CacheLoader() {
				//    ,    -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 getAllInfo() {
		
		ConcurrentMap map = cache.asMap();
		Collection list = map.values();
		List zphinfolist = new ArrayList();
		for (ZphInfo zphInfo : list) {
			zphinfolist.add(zphInfo);
		}
		
		return zphinfolist;		
	}
}

2.ZphoInfoServiceImpl 에서 도구 류 를 사용 하여 실현:캐 시 에 데이터 가 있 을 때 캐 시 에 있 는 데 이 터 를 호출 하고 캐 시 에 데이터 가 없 으 면 데이터 베 이 스 를 직접 호출 합 니 다.
//    List     	
public List getZphInfoList() {
		//         ,          
		if(null != GuavaCache.getAllInfo() && !GuavaCache.getAllInfo().isEmpty()) {
			return GuavaCache.getAllInfo();
		}
		List 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 의 캐 시 기능 을 실현 했다.

좋은 웹페이지 즐겨찾기