0

当我使用GridCacheStore-backed 缓存时,第一个get()可能需要很长时间,因为在内部GridCacheStore将执行计算/慢速搜索等。

get()节点上的第二个将很快,因为结果已被节点缓存。

但是,get()在其他节点上仍然很慢,因为结果没有被复制。如何使这个复制?我已经设置了cacheMode=REPLICATED

我的配置:

<bean parent="cache-template">
    <property name="name" value="yagoEntityByLabel" />
    <property name="cacheMode" value="REPLICATED" />
    <property name="atomicityMode" value="ATOMIC" />
    <property name="distributionMode" value="NEAR_PARTITIONED" />
    <property name="backups" value="1" />
    <property name="store">
        <bean class="id.ac.itb.ee.lskk.lumen.core.yago.YagoEntityByLabelCacheStore" />
    </property>
    <property name="swapEnabled" value="false" />
    <property name="evictionPolicy">
        <bean class="org.gridgain.grid.cache.eviction.lru.GridCacheLruEvictionPolicy">
            <property name="maxSize" value="10000" />
        </bean>
    </property>
</bean>

解决方法是不使用GridCacheStore-backed 而是使用put(),但是有很多类型并且它不是原子的,因为逻辑将是:

@Nullable value = cache.get(key);
if (value == null) {
   value = calculateHeavily(key);
   cache.put(key, value);
}
4

1 回答 1

1

GridGain(与大多数其他数据网格一样)不会在 Get 操作时复制 - 它只是将数据加载到缓存中。您使用 Put 操作的解决方法很好。

为了使其原子化(如果需要),您可以将代码包装到事务中,如下所示:

try (GridCacheTx tx = cache.txStart()) {
   @Nullable value = cache.get(key);
   if (value == null) {
      value = calculateHeavily(key);
      cache.put(key, value);
   }

   tx.commit();
}
于 2014-07-07T08:22:02.097 回答