javax.cache.Cache.invoke()方法的使用及代码示例

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

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

Cache.invoke介绍

[英]Invokes an EntryProcessor against the Entry specified by the provided key. If an Entry does not exist for the specified key, an attempt is made to load it (if a loader is configured) or a surrogate Entry, consisting of the key with a null value is used instead.
[中]根据提供的键指定的条目调用EntryProcessor。如果指定键的项不存在,则尝试加载它(如果配置了加载程序),或者使用由具有空值的键组成的代理项。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public <T> T get(Object key, Callable<T> valueLoader) {
  try {
    return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader);
  }
  catch (EntryProcessorException ex) {
    throw new ValueRetrievalException(key, valueLoader, ex.getCause());
  }
}

代码示例来源:origin: org.springframework/spring-context-support

@Override
@Nullable
public <T> T get(Object key, Callable<T> valueLoader) {
  try {
    return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader);
  }
  catch (EntryProcessorException ex) {
    throw new ValueRetrievalException(key, valueLoader, ex.getCause());
  }
}

代码示例来源:origin: apache/ignite

/**
 * @throws Exception If failed.
 */
@Test
public void testInvoke() throws Exception {
  Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
  assertNotNull(cache.invoke(100, new EntryProcessor<Integer, String, Object>() {
    @Override public Object process(MutableEntry<Integer, String> entry, Object... args) {
      CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class);
      checkVersionedEntry(verEntry);
      return verEntry.version();
    }
  }));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void invoke() {
 expect(cache.invoke(1, new GetEntryProcessor())).isNull(); // miss
 changesOf(0, 1, 0, 0);
 expect(cache.invoke(1, new GetKeyEntryProcessor())).isEqualTo(1); // miss
 changesOf(0, 1, 0, 0);
 expect(cache.invoke(1, new ExistEntryProcessor())).isEqualTo(false); // miss
 changesOf(0, 1, 0, 0);
 expect(cache.invoke(1, new SetEntryProcessor("a"))).isEqualTo("a"); // put
 changesOf(0, 1, 1, 0); // FIXME Why is there a miss?
 expect(cache.invoke(1, new SetEntryProcessor("b"))).isEqualTo("b"); // update
 changesOf(1, 0, 1, 0);
 expect(cache.invoke(1, new GetEntryProcessor())).isEqualTo("b"); // hit
 changesOf(1, 0, 0, 0);
 expect(cache.invoke(1, new GetKeyEntryProcessor())).isEqualTo(1); // hit
 changesOf(1, 0, 0, 0);
 expect(cache.invoke(1, new ExistEntryProcessor())).isEqualTo(true); // hit
 changesOf(1, 0, 0, 0);
 expect(cache.invoke(1, new RemoveEntryProcessor())).isNull(); // hit
 changesOf(1, 0, 0, 1); // FIXME Why is there a hit?
 expect(cache.invoke(1, new RemoveEntryProcessor())).isNull(); // miss
 changesOf(0, 1, 0, 1); // FIXME Why is there a remove?
}

代码示例来源:origin: vladimir-bukhtoyarov/bucket4j

@Override
public void createInitialState(K key, BucketConfiguration configuration) {
  JCacheEntryProcessor<K, Nothing> entryProcessor = JCacheEntryProcessor.initStateProcessor(configuration);
  cache.invoke(key, entryProcessor);
}

代码示例来源:origin: vladimir-bukhtoyarov/bucket4j

@Override
public <T extends Serializable> CommandResult<T> execute(K key, GridCommand<T> command) {
  JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command);
  return cache.invoke(key, entryProcessor);
}

代码示例来源:origin: com.github.vladimir-bukhtoyarov/bucket4j-jcache

@Override
public <T extends Serializable> CommandResult<T> execute(K key, GridCommand<T> command) {
  JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command);
  return cache.invoke(key, entryProcessor);
}

代码示例来源:origin: vladimir-bukhtoyarov/bucket4j

@Override
public <T extends Serializable> T createInitialStateAndExecute(K key, BucketConfiguration configuration, GridCommand<T> command) {
  JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
  CommandResult<T> result = cache.invoke(key, entryProcessor);
  return result.getData();
}

代码示例来源:origin: cache2k/cache2k

@Override
public <T> T invoke(
  K key, final EntryProcessor<K, V, T> entryProcessor, Object... arguments) throws EntryProcessorException {
 EntryProcessor<K0, V0, T> processor = wrapEntryProcessor(entryProcessor);
 return cache.invoke(keyTransformer.compact(key), processor, arguments);
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void nullGetValue() {
 String result = cache.invoke(123, new GetEntryProcessor<Integer, String>());
 assertNull(result);
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

@Override
public void execute(Context context) throws Exception {
  int userId = context.readUserId();
  context.write("New username: ");
  String username = context.readLine();
  Cache<Integer, User> userCache = context.getUserCache();
  User result = userCache.invoke(userId, new UserUpdateEntryProcessor(), username);
  context.writeln("User updated: " + result);
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void close() {
 cache.close();
 try {
  cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class));
  fail("null key");
 } catch (IllegalStateException e) {
  //
 }
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void testProcessorEmptyExceptionIsWrapped() {
 try {
  cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class));
  fail();
 } catch (EntryProcessorException e) {
  assertTrue(e.getCause() instanceof RuntimeException);
  //expected
 }
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void noValueSetValue() {
 final Integer key = 123;
 final Integer ret = 456;
 final String  value = "abc";
 assertEquals(ret, cache.invoke(key, new SetValueCreateEntryReturnDifferentTypeEntryProcessor<Integer, String, Integer>(ret, value)));
 assertEquals(value, cache.get(key));
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void noValueNoMutation() {
 final Integer key = 123;
 final Integer ret = 456;
 assertEquals(ret, cache.invoke(key, new AssertNotPresentEntryProcessor<Integer, String, Integer>(ret)));
 assertFalse(cache.containsKey(key));
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void testProcessorExceptionIsWrapped() {
 try {
  cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class));
  fail();
 } catch (EntryProcessorException e) {
  assertTrue(e.getCause() instanceof RuntimeException);
  //expected
 }
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void existingReplace() {
 final Integer key = 123;
 final String oldValue = "abc";
 final String newValue = "def";
 cache.put(key, oldValue);
 assertEquals(oldValue, cache.invoke(key, new ReplaceEntryProcessor<Integer, String, String>(oldValue, newValue)));
 assertEquals(newValue, cache.get(key));
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void shouldWriteThroughUsingInvoke_remove_nonExistingEntry() {
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 cache.invoke(1, new RemoveEntryProcessor<Integer, String, Object>());
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(1, cacheWriter.getDeleteCount());
 assertFalse(cacheWriter.hasWritten(1));
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void shouldWriteThroughUsingInvoke_remove() {
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 cache.put(1, "Gudday World");
   cache.invoke(1, new RemoveEntryProcessor<Integer, String, Object>(true));
 assertEquals(1, cacheWriter.getWriteCount());
 assertEquals(1, cacheWriter.getDeleteCount());
 assertFalse(cacheWriter.hasWritten(1));
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void shouldWriteThroughUsingInvoke_setValue_UpdateEntry() {
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 cache.put(1, "Gudday World");
 cache.invoke(1, new SetEntryProcessor("Hello World"));
 assertEquals(2, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 assertTrue(cacheWriter.hasWritten(1));
 assertEquals("Hello World", cacheWriter.get(1));
}

相关文章