Java中具有数据初始化和定期刷新整个数据的缓存映射 [英] Cached map with data initialization and whole data refreshed periodically in Java

查看:88
本文介绍了Java中具有数据初始化和定期刷新整个数据的缓存映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java代码中需要一个缓存的地图.映射是从数据库加载的,需要定期从数据库重新加载(对于映射中的所有数据).由于我们现在无法导入任何新软件包.谷歌的番石榴软件包或代码示例中是否有任何现有功能?

I need a cached map in my Java code. The map is loaded from DB and needs reloaded periodically from DB (for all of the data in the map). Since we can't import any new package now. Is there any existing function in google's guava package or code example?

最好将映射实现为线程安全的.但是,如果不够简单的话,还是可以的.

It is better if the map is implemented as thread safe. But it is still OK if it is not but simple enough.

"LoadingCache"是我喜欢的一种,但是它没有数据初始化方法,我无法将数据从一开始就放入地图中.并且它需要在地图过期后每次获取"到数据库时到达数据库.

The "LoadingCache" is kind of what I like but it doesn't have data initialization method for me to put the data into the map at the beginning. And it needs to reach the DB for everytime "get" comes after the map expired.

谢谢!

一些示例代码可能对您有帮助:

Some sample codes may help here:

public interface AToBMapper
{

    public static final String DEFAULT_B_NAME = "DEFAULT";

    public String getBForA(final String a);
}

public class AToBMapperImpl implements AToBMapper
{
    private final SomeDAO dao;

    private Map<String, String> cachedMap;

    public AToBMapperImpl(final SomeDAO dao)
    {
        this.dao = dao;
        cachedMap = new HashMap<String, String>();
    }

    public String getBForA(final String a)
    {
        // if the map is not initialized, initialize it with the data
        // if the map is expired, refresh all the data in the map
        // return the mapped B for A (if there is no mapping for A, return the "DEFAULT")
    }

    private Map<String, String> getTheData(final List<String> listOfB)
    {
        Map<String, String> newData = dao.getAToBMapping(listOfB);
    }
}

推荐答案

"LoadingCache"有点像我喜欢的,但是没有数据 我将数据放到地图上的初始化方法 开始.

The "LoadingCache" is kind of what I like but it doesn't have data initialization method for me to put the data into the map at the beginning.

当然有这样的方法-

Of course it does have such method - putAll(Map<K, V>) from Cache interface which LoadingCache extends. The method Copies all of the mappings from the specified map to the cache.

还有类似的put(K, V)方法可用于此目的.

There's also similar put(K, V) method you can use for this purpose.

根据您的评论,我可以说您根本不需要LoadingCache,而是自己保持所有条目的到期时间.这是您可以使用的简单示例(仅JDK和Guava的类):

Based on your comments I can tell that you don't want LoadingCache at all but rather maintain expiration of all entries by yourself. Here is simple example of what you could use (only JDK's and Guava's classes):

public class AToBMapperImpl implements AToBMapper {
  public static final long EXPIRE_TIME_IN_SECONDS =
      TimeUnit.SECONDS.convert(1, TimeUnit.HOURS); // or whatever
  private final SomeDAO dao;
  private final ConcurrentMap<String, String> cache;
  private final Stopwatch stopwatch;

  public AToBMapperImpl(SomeDAO dao) {
    this.dao = dao;
    stopwatch = new Stopwatch();
    cache = new MapMaker().concurrencyLevel(2).makeMap();
  }

  @Override
  public synchronized String getBForA(final String a) {
    // if the map is not initialized, initialize it with the data
    if (!stopwatch.isRunning()) {
      cache.putAll(getNewCacheContents());
      stopwatch.start();
    }

    // if the map is expired, refresh all the data in the map
    if (stopwatch.elapsedTime(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS) {
      cache.clear();
      cache.putAll(getNewCacheContents());
      stopwatch.reset();
    }

    // return the mapped String for A
    // (if there is no mapping for A, return the "DEFAULT")
    return cache.containsKey(a) ? cache.get(a) : new String(DEFAULT_B_NAME);
  }

  private Map<String, String> getNewCacheContents() {
    return getTheData(Arrays.asList("keys", "you", "want", "to", "load"));
  }

  private Map<String, String> getTheData(List<String> listOfB) {
    return dao.getAToBMapping(listOfB);
  }
}

这篇关于Java中具有数据初始化和定期刷新整个数据的缓存映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆