首页 > 解决方案 > 实现cacheStore时没有ID时如何加载数据以点燃缓存?

问题描述

我正在使用 ignite 缓存,我想缓存一个 id 不相关的视图,所以loadCache当没有 id 时,实现对我来说似乎有点棘手!!!

我应该如何更新下面的示例

public class CacheJdbcPersonStore extends CacheStoreAdapter<Long, Person> {
    ...
  // This method is called whenever "IgniteCache.loadCache()" or
  // "IgniteCache.localLoadCache()" methods are called.
  @Override public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) {
    if (args == null || args.length == 0 || args[0] == null)
      throw new CacheLoaderException("Expected entry count parameter is not provided.");

    final int entryCnt = (Integer)args[0];

    Connection conn = null;

    try (Connection conn = connection()) {
      try (PreparedStatement st = conn.prepareStatement("select * from PERSONS")) {
        try (ResultSet rs = st.executeQuery()) {
          int cnt = 0;

          while (cnt < entryCnt && rs.next()) {
            Person person = new Person(rs.getLong(1), rs.getString(2), rs.getString(3));

            clo.apply(person.getId(), person);

            cnt++;
          }
        }
      }
    }
    catch (SQLException e) {
      throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
  }
  ...
}

clo.apply(person.getId(), person);这部分是我逻辑中的问题我的视图没有 ID

标签: javaoracleignitein-memory-database

解决方案


您需要一些唯一的 ID 来在 Ignite 中存储数据。如果实际数据中没有合适的内容,您的选择是:

  • UUID.randomUUID()
  • 简单计数器 ( id++) 或LongAdder/ AtomicLong- 仅当您从单个节点加载时才有效
  • IgniteAtomicSequence- 跨整个集群工作

推荐阅读