配置文件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;
}
}
}