org.infinispan.Cache.computeIfAbsent()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中org.infinispan.Cache.computeIfAbsent()方法的一些代码示例,展示了Cache.computeIfAbsent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.computeIfAbsent()方法的具体详情如下:
包路径:org.infinispan.Cache
类名称:Cache
方法名:computeIfAbsent

Cache.computeIfAbsent介绍

暂无

代码示例

代码示例来源:origin: org.infinispan/infinispan-query

/**
* Gets the cached query object. The key used for lookup is an object pair containing the query string and a
* discriminator value which is usually the Class of the cached query object and an optional {@link List} of {@link
* FieldAccumulator}s.
*/
public <T> T get(String queryString, List<FieldAccumulator> accumulators, Object queryTypeDiscriminator, QueryCreator<T> queryCreator) {
 QueryCacheKey key = new QueryCacheKey(queryString, accumulators, queryTypeDiscriminator);
 return (T) getCache().computeIfAbsent(key, (k) -> queryCreator.create(k.queryString, k.accumulators));
}

代码示例来源:origin: org.infinispan/infinispan-embedded-query

/**
* Gets the cached query object. The key used for lookup is an object pair containing the query string and a
* discriminator value which is usually the Class of the cached query object and an optional {@link List} of {@link
* FieldAccumulator}s.
*/
public <T> T get(String queryString, List<FieldAccumulator> accumulators, Object queryTypeDiscriminator, QueryCreator<T> queryCreator) {
 QueryCacheKey key = new QueryCacheKey(queryString, accumulators, queryTypeDiscriminator);
 return (T) getCache().computeIfAbsent(key, (k) -> queryCreator.create(k.queryString, k.accumulators));
}

代码示例来源:origin: org.infinispan/infinispan-core

public void testExceptionOnInsertFunctional() {
 for (int i = 0; i < SIZE; ++i) {
   cache(0).computeIfAbsent(i, k -> SIZE);
 }
 try {
   cache(0).computeIfAbsent(-1, k -> SIZE);
   fail("Should have thrown an exception!");
 } catch (Throwable t) {
   Exceptions.assertException(ContainerFullException.class, getMostNestedSuppressedThrowable(t));
 }
}

代码示例来源:origin: org.infinispan/infinispan-core

public void testComputeIfAbsent() {
 Function<Object, String> mappingFunction = k -> k + " world";
 assertEquals("hello world", cache.computeIfAbsent("hello", mappingFunction));
 assertEquals("hello world", cache.get("hello"));
 Function<Object, String> functionAfterPut = k -> k + " happy";
 // hello already exists so nothing should happen
 assertEquals("hello world", cache.computeIfAbsent("hello", functionAfterPut));
 assertEquals("hello world", cache.get("hello"));
 int cacheSizeBeforeNullValueCompute = cache.size();
 Function<Object, String> functionMapsToNull = k -> null;
 assertNull("with function mapping to null returns null", cache.computeIfAbsent("kaixo", functionMapsToNull));
 assertNull("the key does not exist", cache.get("kaixo"));
 assertEquals(cacheSizeBeforeNullValueCompute, cache.size());
 RuntimeException computeRaisedException = new RuntimeException("hi there");
 Function<Object, String> functionMapsToException = k -> {
   throw computeRaisedException;
 };
 expectException(RuntimeException.class, "hi there", () -> cache.computeIfAbsent("es", functionMapsToException));
}

代码示例来源:origin: org.infinispan/infinispan-core

public void testComputeIfAbsentFromNonOwner() throws InterruptedException {
 // do nothing if value exists
 initAndTest();
 Object retval = getFirstNonOwner("k1").computeIfAbsent("k1", (k) -> "computed_" + k);
 if (testRetVals) assertEquals("value", retval);
 // Since the command fails on primary it won't be replicated to the other nodes
 asyncWaitOnPrimary("k1", ComputeIfAbsentCommand.class);
 assertOnAllCachesAndOwnership("k1", "value");
 // Compute key and add result value if absent
 retval = getFirstNonOwner("notExists").computeIfAbsent("notExists", (k) -> "computed_" + k);
 if (testRetVals) assertEquals("computed_notExists", retval);
 asyncWait("notExists", ComputeIfAbsentCommand.class);
 assertOnAllCachesAndOwnership("notExists", "computed_notExists");
 // do nothing if function result is null
 retval = getFirstNonOwner("doNothing").computeIfAbsent("doNothing", k -> null);
 asyncWaitOnPrimary("doNothing", ComputeIfAbsentCommand.class);
 if (testRetVals) assertNull(retval);
 assertRemovedOnAllCaches("doNothing");
 RuntimeException computeRaisedException = new RuntimeException("hi there");
 SerializableFunction<Object, String> mappingToException = k -> {
   throw computeRaisedException;
 };
 expectException(RemoteException.class, () -> getFirstNonOwner("somethingWrong").computeIfAbsent("somethingWrong", mappingToException));
}

代码示例来源:origin: org.infinispan/infinispan-core

@SuppressWarnings("ConstantConditions")
public void testComputeNullParameters() {
 expectException(NullPointerException.class, "Null keys are not supported!",
    () -> cache.compute(null, (o, o2) -> "X"));
 expectException(NullPointerException.class, "Null functions are not supported!", () -> cache.compute("k", null));
 expectException(NullPointerException.class, "Null keys are not supported!",
    () -> cache.computeIfAbsent(null, o -> "X"));
 expectException(NullPointerException.class, "Null functions are not supported!",
    () -> cache.computeIfAbsent("k", null));
 expectException(NullPointerException.class, "Null keys are not supported!",
    () -> cache.computeIfPresent(null, (o, o2) -> "X"));
 expectException(NullPointerException.class, "Null functions are not supported!",
    () -> cache.computeIfPresent("k", null));
}

代码示例来源:origin: org.infinispan/infinispan-query

cache2.computeIfAbsent(key, mappingFunction);
if (transactionsEnabled()) transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
cache2.computeIfAbsent(key1, mappingFunction);
if (transactionsEnabled()) transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);
cache2.computeIfAbsent("anotherKey", mappingToNull);
if (transactionsEnabled()) transactionManager.commit();
StaticTestingErrorHandler.assertAllGood(cache1, cache2);

代码示例来源:origin: org.infinispan/infinispan-core

assertEquals("a", cache(2, cacheName).computeIfAbsent(key, k -> "b"));
assertLocalValue(0, key, "a");
assertLocalValue(1, key, "a");
assertEquals("a", cache(1, cacheName).computeIfAbsent(key, k -> "c"));
assertLocalValue(0, key, "a");
assertLocalValue(1, key, "a");
assertEquals("a", cache(2, cacheName).computeIfAbsent(key, k -> "d"));
assertLocalValue(0, key, "a");
assertLocalValue(1, key, "a");
assertEquals("x", cache(1, cacheName).computeIfAbsent(otherKey, k -> "x"));
assertLocalValue(0, otherKey, "x");
assertLocalValue(1, otherKey, "x");
assertEquals("x", cache(2, cacheName).computeIfAbsent(otherKey, k -> "y"));
assertLocalValue(0, otherKey, "x");
assertLocalValue(1, otherKey, "x");
assertEquals("x", cache(0, cacheName).computeIfAbsent(otherKey, k -> null));
assertLocalValue(0, otherKey, "x");
assertLocalValue(1, otherKey, "x");
assertEquals("x", cache(1, cacheName).computeIfAbsent(otherKey, k -> null));
assertLocalValue(0, otherKey, "x");
assertLocalValue(1, otherKey, "x");
assertEquals("x", cache(2, cacheName).computeIfAbsent(otherKey, k -> null));

相关文章