配置文件pom.xml引入jar包即可
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
代码 :
调用类
public class SystemParamServiceImpl implements SystemParamService
{
private Logger logger=LoggerFactory.getLogger(SystemParamServiceImpl.class);
/**缓存获取时间(分钟)*/
private static final int EXPIRE_TIME=3;
/**最大缓存数量*/
private static final int MAX_SIZE=1000;
/**本地缓存*/
private Cache<String, List<SystemParamBean>> paramCache = CacheBuilder.newBuilder().expireAfterWrite(EXPIRE_TIME, TimeUnit.MINUTES).maximumSize(MAX_SIZE).build();
@Resource(name = "systemParamMapper")
private SystemParamMapper systemParamMapper;
@Override
public List<SystemParamBean> selectByCategory(String category)
{
String cacheKey = this.getCacheKey(null,category,null);
try
{
return paramCache.get(cacheKey, new Callable<List<SystemParamBean>>()
{
@Override
public List<SystemParamBean> call() throws Exception
{
return systemParamMapper.selectByCategory(category);
}
});
}
catch (ExecutionException e)
{
logger.error("获取本地缓存内容出错",e);
return null;
}
}
@Override
public List<SystemParamBean> selectByCategoryAndSubCategory(String category, String subCategory)
{
String cacheKey = this.getCacheKey(null,category, subCategory);
try
{
return paramCache.get(cacheKey, new Callable<List<SystemParamBean>>()
{
@Override
public List<SystemParamBean> call() throws Exception
{
return systemParamMapper.selectByCategoryAndSubCategory(category, subCategory);
}
});
}
catch (ExecutionException e)
{
logger.error("获取本地缓存内容出错",e);
return null;
}
}
}
本文详细介绍如何使用Google Guava库实现本地缓存,通过具体代码示例展示如何配置缓存过期时间与最大容量,并利用缓存提高系统查询效率。
1889

被折叠的 条评论
为什么被折叠?



