情况的Infinispan缓存的执行情况:在Wildfly 9(考虑集群)
我有多个上千条记录结算表。它们被分成包装,例如, 500条记录。然后每个数据包通过消息驱动Bean发送到AS。 AS根据每条记录的内容(例如currency,validStart,validEnd)计算一个密钥,并需要将该密钥存储在数据库中(与内容的组合一起)。
请求:
为了避免重复,我想集中的“工具”,它计算的关键,并将其存储,从而通过缓存与记录这些键减少与数据库的通信。
现在我试着在每个包处理线程的Utility-class-implementation中使用本地Infinispan缓存。这导致了这样的事实,即多个软件包计算出相同的密钥,因此将重复项插入到数据库中。或者有时我会陷入僵局。
我试图通过一个静态变量来实现一个“锁定”来在数据库插入期间阻止对缓存的访问,但没有成功。 下一次尝试是使用复制分别分布式的Infinispan缓存。这并没有改变AS行为的结果。
我最后的想法是实现一个bean管理的单例会话bean,以在插入数据库期间获取事务锁。
AS当前在独立模式下运行,但在不久的将来将被移到群集,因此高可用性解决方案是首选。
恢复:
什么是锁创建的(键,值)对,以避免重复期间的Infinispan缓存访问正确的方法是什么?
更新:
@cruftex:我的要求是:我有一组(键,值)对,这应被缓存。如果插入新记录,则应用算法并计算密钥。然后,如果密钥已经存在并且值将被追加到新记录中,则应检查缓存。但是,如果价值不存在,则应创建并存储在数据库中。
缓存需要使用Infinispan来实现,因为AS应该在群集中运行。创建密钥的算法存在。在数据库中也插入值(通过JDBC或实体)。但我有这个问题,即使用消息驱动Bean(并因此在AS中多线程)相同(键,值)对计算在不同的线程中,因此每个线程都尝试将值插入到数据库中(我想要避免!)。
@戴夫:
public class Cache {
private static final Logger log = Logger.getLogger(Cache.class);
private final Cache<Key, FullValueViewer> fullCache;
private HomeCache homes; // wraps EntityManager
private final Session session;
public Cache(Session session, EmbeddedCacheManager cacheContainer, HomeCache homes) {
this.session = session;
this.homes = homes;
fullCache = cacheContainer.getCache(Const.CACHE_CONDCOMBI);
}
public Long getId(FullValueViewer viewerWithoutId) {
Long result = null;
final Key key = new Key(viewerWithoutId);
FullValueViewer view = fullCache.get(key);
if(view == null) {
view = checkDatabase(viewerWithoutId);
if(view != null) {
fullCache.put(key, view);
}
}
if(view == null) {
view = createValue(viewerWithoutId);
// 1. Try
fullCache.put(key, view);
// 2. Try
// if(!fullCache.containsKey(key)) {
// fullCache.put(key, view);
// } else {
// try {
// homes.condCombi().remove(view.idnr);
// } catch (Exception e) {
// log.error("remove", e);
// }
// }
// 3. Try
// synchronized(fullCache) {
// view = createValue(viewerWithoutId);
// fullCache.put(key, view);
// }
}
result = view.idnr;
return result;
}
private FullValueViewer checkDatabase(FullValueViewer newView) {
FullValueViewer result = null;
try {
CondCombiBean bean = homes.condCombi().findByTypeAndKeys(_parameters_);
result = bean.getAsView();
} catch (FinderException e) {
}
return result;
}
private FullValueViewer createValue(FullValueViewer newView) {
FullValueViewer result = null;
try {
CondCombiBean bean = homes.condCombi().create(session.subpk);
bean.setFromView(newView);
result = bean.getAsView();
} catch (Exception e) {
log.error("createValue", e);
}
return result;
}
private class Key {
private final FullValueViewer view;
public Key(FullValueViewer v) {
this.view = v;
}
@Override
public int hashCode() {
_omitted_
}
@Override
public boolean equals(Object obj) {
_omitted_
}
}
}
缓存配置我试着用Wildfly:
<cache-container name="server" default-cache="default" module="org.wildfly.clustering.server">
<local-cache name="default">
<transaction mode="BATCH"/>
</local-cache>
</cache-container>
<cache-container name="server" default-cache="default" module="org.wildfly.clustering.server">
<transport lock-timeout="60000"/>
<distributed-cache name="default" mode="ASYNC"/>
</cache-container>
请发表您已经写过/尝试过的内容; [so]在这里是为了帮助你解决你的编码问题,而不是为你写下全部内容:) – Dave
它很不清楚你所要求的。你想知道如何生成密钥?将某些内容存储在缓存中?或者用缓存生成密钥(为什么?)?从数据中得到的密钥应该如何?如果您想生成唯一的密钥,请参阅UUID算法示例,您不需要任何通信。 – cruftex