英文:
How to convert concurentHashMap into Guava Cache <Key, Pair>
问题
以下是您要翻译的代码部分:
Let say that we have a method that returns ConcurentMap with pairs <String, String> for old and new values of numbers.
public ConcurrentMap<String, String> getAllOldAndNewPairs() {
return databaseConnector.executeQuery(QUERY, new QueryLauncher<ConcurrentHashMap<String, String>>() {
@Override
public ConcurrentHashMap<String, String> processResult(Results results) throws DAOException {
ConcurrentHashMap<String, String> oldAndNewPairs = new ConcurrentHashMap<>();
while (results.next()) {
oldAndNewPairs.put(results.getString(Column.OLD_NUMBER), results.getString(Column.NEW_NUMBER));
}
return oldAndNewPairs;
}
});
}
I want to fill the guava cache with <Key, Pair> record from method ```getAllOldAndNewPairs()```
So far I have a method like:
public void propagateCache() {
LoadingCache<String, String> loadingCache =
CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
return getAllOldAndNewPairs();
}
});
}
但目前IDE报错如下:
return getAllOldAndNewPairs();
> Required Type: String - Provided: Concurrent Map<String, String>
我尝试了以下两种方式:
```java
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.build(CacheLoader.from(this::getAllOldAndNewPairs));
但是都没有产生期望的效果。
我将感谢您关于如何解决这个问题以及如何从方法 getAllOldAndNewPairs
填充缓存以获取 <Key, Pair> 值的建议。
英文:
Let say that we have a method that returns ConcurentMap with pairs <String, String> for old and new values of numbers.
public ConcurrentMap<String, String> getAllOldAndNewPairs() {
return databaseConnector.executeQuery(QUERY, new QueryLauncher<ConcurrentHashMap<String, String>>() {
@Override
public ConcurrentHashMap<String, String> processResult(Results results) throws DAOException {
ConcurrentHashMap<String, String> oldAndNewPairs = new ConcurrentHashMap<>();
while (results.next()) {
oldAndNewPairs.put(results.getString(Column.OLD_NUMBER), results.getString(Column.NEW_NUMBER));
}
return oldAndNewPairs;
}
});
}
I want to fill the guava cache with <Key, Pair> record from method getAllOldAndNewPairs()
So far I have a method like:
public void propagateCache() {
LoadingCache<String, String> loadingCache =
CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
return getAllOldAndNewPairs();
}
});
}
but currently IDE complaining about line:
return getAllOldAndNewPairs();
> Required Type: String - Provided: Concurrent Map<String, String>
I was trying either:
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.build(CacheLoader.from(this::getAllOldAndNewPairs));
and either this did not bring a desirable effect.
I will be grateful for suggestions on how to solve this problem and fill the cache with <Key, Pair> values from method getAllOldAndNewPairs.
答案1
得分: 1
任何Guava缓存实现都可以通过映射进行填充。以下方法是Guava缓存接口的一部分:
void putAll(Map<? extends K, ? extends V> var1);
话虽如此,你正在错误地使用LoadingCache。当缓存中不存在键时,缓存加载器提供确定与键对应的值的逻辑。因此,缓存加载器返回值对象(在你的情况下是String)。
英文:
Any guava cache implementation can be populated via map. The following method is part of Guava Cache Interface:
void putAll(Map<? extends K, ? extends V> var1);
That being said, you are incorrectly using LoadingCache. When the key is not present in the cache, cache loader provides the logic to determine the value corresponding to the key. Hence the cache loader returns value object (String in your case).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论