English 中文(简体)
Guava Cache的载前值
原标题:Pre-load values for a Guava Cache

我有一项要求,即我们从数据库中装载固定数据,供在 Java应用中使用。 任何教学机制都应具备以下功能:

  • Load all static data from the database (once loaded, this data will not change)
  • Load new data from the database (data present in the database at start-up will not change but it is possible to add new data)

所有数据的齐填不成选项,因为申请将部署到多个地理区域,并且必须与一个数据库进行沟通。 如果在另一个区域申请数据库,数据充斥就会使第一个具体要素的要求过于缓慢。

我在瓜瓦成功地利用了马凯尔文稿,但我们现在正在升级到最新版本中,我似乎无法在CacheBuilder文稿中找到同样的功能;我似乎无法找到在开始的时候装上所有数据的干净方法。

一种办法是将数据库的所有钥匙装上,并单独通过Cache装载这些钥匙。 这将奏效,但将会向数据库发出N+1电话,而该数据库的高效解决办法是Im。

public void loadData(){
    List<String> keys = getAllKeys();
    for(String s : keys)
        cache.get(s);
}

或者,另一种解决办法是利用一个同步的HashMap实施,处理所有线索和缺失条目本身? 我不热心这样做,因为地图制作人和CacheBuilder的预报器提供了在不必进行额外测试的情况下为免费打上了关键校对。 我也相信,地图制作员/CacheBuilder执行器会有一些效率,我不知道/甚至没有时间进行调查。

public Element get(String key){
    Lock lock = getObjectLock(key);
    lock.lock();
    try{
        Element ret = map.get(key)
        if(ret == null){
            ret = getElement(key); // database call
            map.put(key, e);
        }
        return ret;
    }finally {
        lock.unlock();
    } 
}

谁能认为我的两项要求得到更好的解决?


<><>>><>>>

我不认为先装满一个海滩是一种不寻常的要求,因此,如果CacheBuilder提供配置选择,先装上海滩,那将是空洞的。 我认为,提供一个互动(如Cache Loader)的办法,在开办时把藏匿点放在一起,是一个理想的解决办法,例如:

CacheBuilder.newBuilder().populate(new CachePopulator<String, Element>(){

    @Override
    public Map<String, Element> populate() throws Exception {
        return getAllElements();
    }

}).build(new CacheLoader<String, Element>(){

    @Override
    public Element load(String key) throws Exception {       
        return getElement(key);
    }

});

This implementation would allow the Cache to be pre-populated with all relevant Element objects, whilst keeping the underlying CustomConcurrentHashMap non-visible to the outside world.

问题回答

在短期内,我只使用<条码>Cache.asMap(......)。putAll(Map<K,V>)。

Guava 11.0被释放后,你可使用Cache.getAll(Iterable<K>,其中将对所有没有的内容提出单一批量要求。

I d 装载来自行文的所有静态数据,并使用<条码>cache.asMap(......)>(关键值)

Of course, this static data might get evicted, if your cache is configured to evict entries...

CachePopulator的想法令人感兴趣。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签