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

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

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

Cache.put介绍

[英]Associates the specified value with the specified key in the cache.

If the Cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only if #containsKey(Object) would return true.)
[中]将指定的值与缓存中的指定键相关联。
如果缓存以前包含键的映射,则旧值将替换为指定值。(缓存c被称为包含密钥k的映射,当且仅当#containsKey(Object)返回true。)

代码示例

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

@Test
public void test_getCachePuts() throws Exception {
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 assertThat(heapStatistics.getCachePuts(), is(5L));
}

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

@Test
public void testSimplePutIfAbsentWithLoaderAndWriter_existsInStore() throws Exception {
 testCache.put(1, "un");
 reset(cacheWriter);
 assertThat(testCache.putIfAbsent(1, "one"), is(false));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verifyZeroInteractions(cacheLoader);
 verifyZeroInteractions(cacheWriter);
}

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

@Test
public void test_getAveragePutTime() throws Exception {
 assertThat(heapStatistics.getAveragePutTime(), is(0.0f));
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 heapCache.put("key", "value");
 assertFor(1100L, () -> heapStatistics.getAveragePutTime(), is(not(0.0f)));
 assertThat(heapStatistics.getAveragePutTime(), greaterThan(0.0f));
}

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

@Test
public void test_getCacheEvictions_heapAndOffheap() throws Exception {
 String ONE_MB = new String(new byte[1024 * 512]);
 for (int i = 0; i < 20; i++) {
  offheapCache.put("key" + i, ONE_MB);
 }
 assertThat(offheapStatistics.getCacheEvictions(), greaterThan(0L));
}

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

@Test
public void spliterator() {
 cache.put(1, "a");
 cache.put(2, "b");
 cache.put(3, "c");
 changesOf(0, 0, 3, 0);
 StreamSupport.stream(cache.spliterator(), false).forEach(e -> {});
 changesOf(3, 0, 0, 0);
}

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

@Test
public void put() {
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 cache.put(1, "b");
 changesOf(0, 0, 1, 0);
}

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

@Test
public void test_getCacheEvictions_heapOnly() throws Exception {
 for (int i = 0; i < 20; i++) {
  heapCache.put("key" + i, "value");
 }
 assertThat(heapStatistics.getCacheEvictions(), is(10L));
}

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

@Test
public void test_getCacheEvictions_heapAndDisk() throws Exception {
 String ONE_MB = new String(new byte[1024 * 512]);
 for (int i = 0; i < 20; i++) {
  diskCache.put("key" + i, ONE_MB);
 }
 assertThat(diskStatistics.getCacheEvictions(), greaterThan(0L));
}

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

@Test
public void foreach() {
 cache.put(1, "a");
 cache.put(2, "b");
 cache.put(3, "c");
 changesOf(0, 0, 3, 0);
 cache.forEach(e -> {});
 changesOf(3, 0, 0, 0);
}

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

@Test
public void test_getCacheHitsAndMisses() {
 heapCache.put("key1", "value1");
 heapCache.put("key3", "value3");
 heapCache.put("key5", "value5");
 HashSet<String> keys = new HashSet<>(5);
 for (int i = 1; i <= 5; i++) {
  keys.add("key" + i);
 }
 heapCache.getAll(keys);
 assertThat(heapStatistics.getCacheHits(), is(3L));
 assertThat(heapStatistics.getCacheMisses(), is(2L));
}

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

@Test
public void clear() {
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 cache.clear();
 changesOf(0, 0, 0, 0);
}

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

@Test
public void removeAll() {
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 cache.removeAll();
 changesOf(0, 0, 0, 2);
}

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

@Test
public void testSimpleRemove2ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception {
 testCache.put(1, "un");
 reset(cacheWriter);
 assertThat(testCache.remove(1, "one"), is(false));
 verifyZeroInteractions(cacheLoader);
 verifyZeroInteractions(cacheWriter);
}

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

@Test
public void testSimpleRemove2ArgsWithLoaderAndWriter_existsInStore() throws Exception {
 testCache.put(1, "un");
 reset(cacheWriter);
 assertThat(testCache.remove(1, "un"), is(true));
 verifyZeroInteractions(cacheLoader);
 verify(cacheWriter, times(1)).delete(eq(1));
}

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

@Test
public void testTemplateAddsListeners() throws Exception {
 CacheManager cacheManager = cachingProvider.getCacheManager(getClass().getResource("/ehcache-107-listeners.xml")
   .toURI(), getClass().getClassLoader());
 MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
 configuration.setTypes(String.class, String.class);
 MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(Test107CacheEntryListener::new, null, false, true);
 configuration.addCacheEntryListenerConfiguration(listenerConfiguration);
 Cache<String, String> cache = cacheManager.createCache("foos", configuration);
 cache.put("Hello", "Bonjour");
 assertThat(Test107CacheEntryListener.seen.size(), Matchers.is(1));
 assertThat(TestCacheEventListener.seen.size(), Matchers.is(1));
}

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

@Test
public void removeAllKeys() {
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 cache.removeAll(asSet(1, 2, 3));
 changesOf(0, 0, 0, 2);
}

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

@Test
public void testJSR107DefaultSerializer() throws URISyntaxException {
 Cache<Long, String> cache = cacheManager.getCache("cache", Long.class, String.class);
 cache.put(19L, "foo");
 assertThat(cache.get(19L), equalTo("foo"));
}

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

@Test
 public void testJSR107Serializer() {
  Cache<Long, String> cache1 = cacheManager.getCache("cache1", Long.class, String.class);
  cache1.put(17L, "foo");
  assertThat(cache1.get(17L), equalTo("foo"));
 }
}

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

@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception {
 testCache.put(1, "un");
 reset(cacheWriter);
 assertThat(testCache.replace(1, "uno", "one"), is(false));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verifyZeroInteractions(cacheLoader);
 verifyZeroInteractions(cacheWriter);
}

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

@Test
public void testCopierAtServiceLevel() throws Exception {
 CacheManager cacheManager = cachingProvider.getCacheManager(
   getClass().getResource("/ehcache-107-default-copiers.xml").toURI(),
   getClass().getClassLoader());
 MutableConfiguration<Long, Client> config = new MutableConfiguration<>();
 config.setTypes(Long.class, Client.class);
 Cache<Long, Client> bar = cacheManager.createCache("bar", config);
 Client client = new Client("tc", 1000000L);
 long key = 42L;
 bar.put(key, client);
 assertThat(bar.get(key), not(sameInstance(client)));
}

相关文章