Java Guava缓存:如何处理空值
我构建了一个缓存,当您输入参数时,它以列表格式返回一个值。如果该值不在缓存中,它将转到数据库并检索它,将其放入缓存中以供将来参考:Java Guava缓存:如何处理空值,java,null,return-value,guava,Java,Null,Return Value,Guava,我构建了一个缓存,当您输入参数时,它以列表格式返回一个值。如果该值不在缓存中,它将转到数据库并检索它,将其放入缓存中以供将来参考: private ProfileDAO profileDAO; private String[] temp; private LoadingCache<String, List<Profile>> loadingCache = CacheBuilder.newBuilder() .refreshAfterWrit
private ProfileDAO profileDAO;
private String[] temp;
private LoadingCache<String, List<Profile>> loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(
new CacheLoader<String, List<Profile>>() {
@Override
public List<Profile> load(String key) throws Exception {
logger.info("Running method to retrieve from database");
temp = key.split("\\|");
String instance = temp[0];
String name = temp[1];
List<Profile> profiles= profileDAO.getProfileByFields(id, name);
if (profiles.isEmpty()) {
List<Profile> nullValue = new ArrayList<Profile>();
logger.info("Unable to find a value.");
return nullValue;
}
logger.info("Found a value");
return profileDAO.getProfileByFields(id, name);
}
}
);
public List<Profile> getProfileByFields(String id, String name) throws Exception {
String key = id.toLowerCase() + "|" + name.toLowerCase()
return loadingCache.get(key);
}
如果数据库中没有匹配项,但if语句失败,我只想返回一个空列表(概要文件)。对于这个特定的用例,有没有办法避免这个错误?在代码中进行更改,以检查第一个配置文件是否为null(使用profiles==null…):
私有ProfileDAO ProfileDAO;
私有字符串[]临时;
private LoadingCache LoadingCache=CacheBuilder.newBuilder()
.refreshAfterWrite(5,时间单位:分钟)
.expireAfterWrite(5,时间单位:分钟)
.建造(
新缓存加载程序(){
@凌驾
公共列表加载(字符串键)引发异常{
info(“从数据库检索的运行方法”);
temp=键拆分(“\\\\”);
字符串实例=temp[0];
字符串名称=temp[1];
List profiles=profileDAO.getProfileByFields(id、名称);
if(profiles==null | | profiles.isEmpty()){
List nullValue=new ArrayList();
logger.info(“无法找到值”);
返回空值;
}
logger.info(“找到值”);
返回profileDAO.getProfileByFields(id,name);
}
}
);
公共列表getProfileByFields(字符串id、字符串名称)引发异常{
String key=id.toLowerCase()+“|”+name.toLowerCase()
返回loadingCache.get(key);
}
请检查这段代码是否适用于空值。虽然这感觉有点不对劲,但我认为这是一个更完整的解决方案(Suresh的答案只适用于集合) 定义一个表示
null
的单例对象,并将该值插入缓存,而不是null
(在检索时转换为null
):
类MyDAO
{
静态最终对象NULL=新对象();
LoadingCache cache=CacheBuilder.newBuilder()
.build(新的缓存加载程序()
{
公共对象加载(字符串键)
{
对象值=database.get(key);
如果(值==null)
返回NULL;
返回值;
}
});
对象获取(字符串键)
{
对象值=cache.get(key);
if(value==NULL)//使用“==”比较对象引用
返回null;
返回值;
}
}
我认为,就效率而言,这种方法比任何涉及使用例外的方法都更可取
com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key A01|Peter
private ProfileDAO profileDAO;
private String[] temp;
private LoadingCache<String, List<Profile>> loadingCache = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build(
new CacheLoader<String, List<Profile>>() {
@Override
public List<Profile> load(String key) throws Exception {
logger.info("Running method to retrieve from database");
temp = key.split("\\|");
String instance = temp[0];
String name = temp[1];
List<Profile> profiles= profileDAO.getProfileByFields(id, name);
if (profiles == null || profiles.isEmpty()) {
List<Profile> nullValue = new ArrayList<Profile>();
logger.info("Unable to find a value.");
return nullValue;
}
logger.info("Found a value");
return profileDAO.getProfileByFields(id, name);
}
}
);
public List<Profile> getProfileByFields(String id, String name) throws Exception {
String key = id.toLowerCase() + "|" + name.toLowerCase()
return loadingCache.get(key);
}