When I use a GridCacheStore
-backed cache, the first get()
may take a long time, because internally the GridCacheStore
will perform calculations / slow search etc.
The second get()
on that node will be quick because the result has been cached by that node.
However, the get()
on other node is still slow because the result is not being replicated. How to make this replicated? I've set cacheMode=REPLICATED
.
My config:
<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>
The workaround is to not use GridCacheStore
-backed but to use put()
instead, but there's a lot of typing and it's not atomic, since the logic will be:
@Nullable value = cache.get(key);
if (value == null) {
value = calculateHeavily(key);
cache.put(key, value);
}
GridGain (as most other data grids) does not replicate on Get operation - it simply loads data into cache. Your workaround using Put operation is good.
To make it atomic (if you need to do so), you can wrap your code into transaction, like so:
try (GridCacheTx tx = cache.txStart()) {
@Nullable value = cache.get(key);
if (value == null) {
value = calculateHeavily(key);
cache.put(key, value);
}
tx.commit();
}