如何将ConcurrentHashMap转换为Guava Cache

huangapple go评论75阅读模式
英文:

How to convert concurentHashMap into Guava Cache <Key, Pair>

问题

以下是您要翻译的代码部分:

Let say that we have a method that returns ConcurentMap with pairs &lt;String, String&gt; for old and new values of numbers. 

public ConcurrentMap&lt;String, String&gt; getAllOldAndNewPairs() {
    return databaseConnector.executeQuery(QUERY, new QueryLauncher&lt;ConcurrentHashMap&lt;String, String&gt;&gt;() {

        @Override
        public ConcurrentHashMap&lt;String, String&gt; processResult(Results results) throws DAOException {

            ConcurrentHashMap&lt;String, String&gt; oldAndNewPairs = new ConcurrentHashMap&lt;&gt;();
            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 &lt;Key, Pair&gt; record from method ```getAllOldAndNewPairs()```

So far I have a method like: 

public void propagateCache() {

    LoadingCache&lt;String, String&gt; loadingCache =
            CacheBuilder.newBuilder().build(new CacheLoader&lt;String, String&gt;() {
                @Override
                public String load(String s) throws Exception {
                    return getAllOldAndNewPairs();
                }
            });
}

但目前IDE报错如下:

return getAllOldAndNewPairs();

> Required Type: String - Provided: Concurrent Map<String, String>


我尝试了以下两种方式:

```java
LoadingCache&lt;String, String&gt; 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&lt;String, String&gt; getAllOldAndNewPairs() {
        return databaseConnector.executeQuery(QUERY, new QueryLauncher&lt;ConcurrentHashMap&lt;String, String&gt;&gt;() {

            @Override
            public ConcurrentHashMap&lt;String, String&gt; processResult(Results results) throws DAOException {

                ConcurrentHashMap&lt;String, String&gt; oldAndNewPairs = new ConcurrentHashMap&lt;&gt;();
                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&lt;String, String&gt; loadingCache =
                CacheBuilder.newBuilder().build(new CacheLoader&lt;String, String&gt;() {
                    @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&lt;String, String&gt; 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&lt;? extends K, ? extends V&gt; 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).

huangapple
  • 本文由 发表于 2020年8月5日 20:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定