实现cacheStore时没有ID时如何加载数据点燃缓存?
how to load data to ignite cache when there is no ID when Implementing a cacheStore?
我正在使用 ignite 缓存,我想缓存一个 id 不相关的视图,所以在没有 id 的情况下,实现 loadCache
对我来说似乎有些棘手!!!
我应该如何更新下面的例子
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
您需要一些唯一 ID 才能在 Ignite 中存储数据。如果实际数据中没有合适的,你的选择是:
UUID.randomUUID()
- 简单计数器 (
id++
) 或 LongAdder
/AtomicLong
- 仅当您从单个节点加载时才有效
IgniteAtomicSequence
- 适用于整个集群
我正在使用 ignite 缓存,我想缓存一个 id 不相关的视图,所以在没有 id 的情况下,实现 loadCache
对我来说似乎有些棘手!!!
我应该如何更新下面的例子
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
您需要一些唯一 ID 才能在 Ignite 中存储数据。如果实际数据中没有合适的,你的选择是:
UUID.randomUUID()
- 简单计数器 (
id++
) 或LongAdder
/AtomicLong
- 仅当您从单个节点加载时才有效 IgniteAtomicSequence
- 适用于整个集群