org.ehcache.Cache.get()方法的使用及代码示例

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

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

Cache.get介绍

[英]Retrieves the value currently mapped to the provided key.
[中]检索当前映射到提供的键的值。

代码示例

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

private void put(Cache<Long, String> cache, String value, boolean addToCacheRecords) {
 cache.put(KEY, value);
 if (addToCacheRecords) {
  cacheRecords.add(new Record(KEY, cache.get(KEY)));
 }
}

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

private void replace(Cache<Long, String> cache, String value, boolean addToCacheRecords) {
 cache.replace(KEY, value);
 if (addToCacheRecords) {
  cacheRecords.add(new Record(KEY, cache.get(KEY)));
 }
}

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

@Test
public void getMisses() throws Exception {
 cache.get(1L);
 assertThat(onHeap.getMisses()).isEqualTo(1L);
 assertStat("OnHeap:MissCount").isEqualTo(1L);
}

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

@Test
public void ttiExpiryGet() {
 Cache<Integer, String> cache = createCache(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(100)));
 cache.put(1, "a");
 assertThat(cache.get(1)).isEqualTo("a");
 timeSource.setTimeMillis(100);
 assertThat(cache.get(1)).isNull();
}

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

@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "one"), Matchers.<CharSequence>equalTo("un"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
}

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

@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "un", "one"), is(true));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
}

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

@Test
public void getHits() throws Exception {
 cache.put(1L, "a");
 cache.get(1L);
 assertThat(onHeap.getHits()).isEqualTo(1L);
 assertStat("OnHeap:HitCount").isEqualTo(1L);
}

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

@Test
public void getHits() {
 cache.put(1L, "a");
 cache.get(1L);
 assertThat(onHeap.getHits()).isEqualTo(0L);
 assertNoStat("OnHeap:HitCount");
}

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

@Test
public void getExpirations() throws Exception {
 cache.put(1L, "a");
 timeSource.advanceTime(TIME_TO_EXPIRATION);
 cache.get(1L);
 assertThat(onHeap.getExpirations()).isEqualTo(1L);
 assertStat("OnHeap:ExpirationCount").isEqualTo(1L);
}

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

@Test
public void testSimplePutWithExpiry_replace3Args() throws Exception {
 insert(testCache, getEntries());
 assertThat(cacheSize(testCache), is(2));
 manualTimeSource.setTimeMillis(1001);
 assertThat(testCache.replace(1, "one", "one#2"), is(false));
 assertThat(testCache.get(1), is(nullValue()));
 assertThat(testCache.replace(2, "two", "two#2"), is(false));
 assertThat(testCache.get(2), is(nullValue()));
}

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

@Test
public void putIfAbsent_absent() {
 createCacheManager();
 cache = createCache();
 assertThat(cache.putIfAbsent(1, "a")).isNull();
 assertThat(cache.get(1)).isEqualTo("a");
 changesOf(1, 1, 1, 0);
}

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

@Test
public void testDestroyLoop() throws Exception {
 for (int i = 0; i < 10; i++) {
  try (CacheManagerContainer cmc = new CacheManagerContainer(10, this::createCacheManager)) {
   // just put in one and get from another
   cmc.cacheManagerList.get(0).getCache(CACHE_NAME, Long.class, String.class).put(1L, "value");
   assertThat(cmc.cacheManagerList.get(5).getCache(CACHE_NAME, Long.class, String.class).get(1L),
    is("value"));
  }
  destroyCacheManager();
 }
}

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

@Test
public void get() {
 expect(cache.get(1)).isNull();
 changesOf(0, 1, 0, 0);
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 expect(cache.get(1)).isEqualTo("a");
 changesOf(1, 0, 0, 0);
}

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

@Test
 public void testClusteredWriteBehindLoading() throws Exception {
  cache.put(KEY, "Some value");
  checkValueFromLoaderWriter(cache, "Some value");
  cache.clear();

  assertThat(cache.get(KEY), notNullValue());

  doThreadDump = false;
 }
}

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

@Test
public void testSimpleReplace() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 testCache.put(1, "one");
 assertThat(testCache.replace(1, "one_"), Matchers.<CharSequence>equalTo("one"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one_"));
 assertThat(testCache.replace(2, "two_"), is(nullValue()));
}

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

@Test
public void testCacheConfigUsage() {
 final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
   .withCache("foo",
     CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class, heap(10))
       .add(new DefaultCacheLoaderWriterConfiguration(MyLoader.class))
       .build()).build(true);
 final Object foo = manager.getCache("foo", Object.class, Object.class).get(new Object());
 assertThat(foo, is(MyLoader.object));
}

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

public static void testByteSizedOnHeapInOsgi() {
  CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
   .withCache("myCache", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().heap(10, MemoryUnit.KB))
    .build())
   .build(true);
  Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class);
  cache.put(42L, "I am out of heap!!");
  cache.get(42L);
 }
}

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

public static void testProgrammaticClusteredCache(OsgiTestUtils.Cluster cluster) throws Throwable {
 try (PersistentCacheManager cacheManager = newCacheManagerBuilder()
  .with(cluster(cluster.getConnectionUri()).autoCreate())
  .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class,
   newResourcePoolsBuilder().with(clusteredDedicated("main", 2, MemoryUnit.MB))))
  .build(true)) {
  final Cache<Long, String> cache = cacheManager.getCache("clustered-cache", Long.class, String.class);
  cache.put(1L, "value");
  assertThat(cache.get(1L), is("value"));
 }
}

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

public static void testEhcache3WithSerializationAndClientClass() {
 CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
  .withCache("myCache", newCacheConfigurationBuilder(Long.class, Person.class, heap(10))
   .add(new DefaultCopierConfiguration<>(SerializingCopier.<Person>asCopierClass(), DefaultCopierConfiguration.Type.VALUE))
   .withClassLoader(TestMethods.class.getClassLoader())
   .build())
  .build(true);
 Cache<Long, Person> myCache = cacheManager.getCache("myCache", Long.class, Person.class);
 myCache.put(42L, new Person("Arthur"));
 assertTrue(myCache.get(42L) instanceof Person);
}

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

public static void testOffHeapInOsgi() {
 CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
  .withCache("myCache", newCacheConfigurationBuilder(Long.class, String.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).offheap(10, MemoryUnit.MB))
   .build())
  .build(true);
 Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class);
 cache.put(42L, "I am out of heap!!");
 cache.get(42L);
}

相关文章