gpt4 book ai didi

java - Guava CacheLoader 未命中

转载 作者:行者123 更新时间:2023-12-01 17:11:07 24 4
gpt4 key购买 nike

我在这里不知所措。

我在手动加载/插入 Guava 缓存方面非常幸运,但我认为它应该更加“线程安全”(通过 CacheLoader)。

当我重构为使用 CacheLoader.load 样式语法时,我的缓存停止工作。现在每个 get() 都是缓存未命中,并开始调用 load()。

我错过了一些非常简单的东西吗?

缓存:

public class DatabasePropertyCache implements RemovalListener<DatabasePropertyCache.Key, Optional<Object>> {

//InitializeOnDemand style singleton
// http://en.wikipedia.org/wiki/Singleton_pattern#Initialization_On_Demand_Holder_Idiom
private static class Singleton {
static DatabasePropertyCache INSTANCE = new DatabasePropertyCache();
}
public static DatabasePropertyCache getInstance() {
return Singleton.INSTANCE;
}
private DatabasePropertyCache() {
logger = Logger.getLogger(DatabasePropertyCache.class);
cache = CacheBuilder
.newBuilder()
.expireAfterWrite(24, TimeUnit.HOURS)
.removalListener(this)
.build(
new CacheLoader<Key, Optional<Object>>() {

@Override
public Optional<Object> load(Key key) throws Exception {
Object propertyValue = [performs a query]
return Optional.fromNullable(propertyValue);
}
});
}

private final LoadingCache<Key, Optional<Object>> cache;
private final Logger logger;

public void onRemoval(RemovalNotification<Key, Optional<Object>> arg0) {
if(logger.isDebugEnabled()) {
logger.debug(String.format("Cached database property expiring %s", arg0.getKey().toString()));
}
}

/**
* Returns a cached value. If it has never been cache before, it will be fetched from the database.
*
* @param value
* @param locale
* @param dataset
* @return
*/
public Object getObject(String id, DatabasePropertiesEnum property, Locale locale) {
Key key = new Key(id, property, locale);

try {
return cache.get(key).orNull();
}
catch (ExecutionException eex) {
logger.warn(String.format("Error fetching database property %s", key.toString()), eex);
}
return null;
}

protected class Key {

private static final String TOSTRING_TEMPLATE = "DatabasePropertyCache.Key[dataset=%s, locale=%s, property=%s]";

private String id;
private DatabasePropertiesEnum property;
private Locale locale;

public Key(String id, DatabasePropertiesEnum property, Locale locale) {
this.id= id;
this.property = property;
this.locale = locale;
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.append(this.id)
.append(this.property)
.append(this.locale)
.toHashCode();
}

public String getId() {
return id;
}

public DatabasePropertiesEnum getProperty() {
return property;
}

public Locale getLocale() {
return locale;
}

@Override
public String toString() {
return String.format(TOSTRING_TEMPLATE, id, locale.toString(), property.name());
}
}
}

用法:

Object cacheHit = DatabasePropertyCache.getInstance().getObject("some_id", DatabasePropertiesEnum.SOME_KEY, [users current locale]);

最佳答案

Key 类必须正确实现 equals()hashcode()

您可以使用Guava EqualsTester测试您的 equals 和 hashCode:

关于java - Guava CacheLoader 未命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23812521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com