Esempio n. 1
0
  @Test
  public void testStoreByValue() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);
    cacheManager.init();

    DefaultCopierConfiguration<String> copierConfiguration =
        new DefaultCopierConfiguration(SerializingCopier.class, CopierConfiguration.Type.VALUE);
    final Cache<Long, String> cache1 =
        cacheManager.createCache(
            "cache1",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .withResourcePools(
                    ResourcePoolsBuilder.newResourcePoolsBuilder().heap(1, EntryUnit.ENTRIES))
                .build());
    performAssertions(cache1, true);

    final Cache<Long, String> cache2 =
        cacheManager.createCache(
            "cache2",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .add(copierConfiguration)
                .build());
    performAssertions(cache2, false);

    final Cache<Long, String> cache3 =
        cacheManager.createCache(
            "cache3",
            CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
                .build());
    performAssertions(cache3, true);

    cacheManager.close();
  }
 @Test
 public void testAddingCacheEventListenerConfigurationAtCacheLevel() {
   CacheManagerBuilder<CacheManager> cacheManagerBuilder =
       CacheManagerBuilder.newCacheManagerBuilder();
   CacheEventListenerConfiguration cacheEventListenerConfiguration =
       CacheEventListenerConfigurationBuilder.newEventListenerConfiguration(
               ListenerObject.class, EventType.CREATED)
           .unordered()
           .asynchronous()
           .build();
   CacheManager cacheManager = cacheManagerBuilder.build(true);
   final Cache<Long, String> cache =
       cacheManager.createCache(
           "cache",
           CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class)
               .add(cacheEventListenerConfiguration)
               .withResourcePools(
                   ResourcePoolsBuilder.newResourcePoolsBuilder()
                       .heap(100, EntryUnit.ENTRIES)
                       .build())
               .build());
   Collection<ServiceConfiguration<?>> serviceConfiguration =
       cache.getRuntimeConfiguration().getServiceConfigurations();
   assertThat(
       serviceConfiguration,
       IsCollectionContaining.<ServiceConfiguration<?>>hasItem(
           instanceOf(DefaultCacheEventListenerConfiguration.class)));
   cacheManager.close();
 }
  @Test
  public void testThreadPoolsUsingDefaultPool() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/thread-pools.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    try {
      Cache<String, String> cache =
          cacheManager.createCache(
              "testThreadPools",
              newCacheConfigurationBuilder()
                  .add(
                      new DefaultCacheLoaderWriterConfiguration(
                          ThreadRememberingLoaderWriter.class))
                  .add(newUnBatchedWriteBehindConfiguration())
                  .buildConfig(String.class, String.class));

      cache.put("foo", "bar");

      ThreadRememberingLoaderWriter.USED.acquireUninterruptibly();

      assertThat(ThreadRememberingLoaderWriter.LAST_SEEN_THREAD.getName(), containsString("[big]"));
    } finally {
      cacheManager.close();
    }
  }
  @Test
  public void testCall() {
    List<Context> contextList =
        Arrays.asList(
            Context.create().with("cacheManagerName", "myCM1").with("cacheName", "aCache1"),
            Context.create().with("cacheManagerName", "myCM1").with("cacheName", "aCache4"),
            Context.create().with("cacheManagerName", "myCM2").with("cacheName", "aCache2"),
            Context.create().with("cacheManagerName", "myCM55").with("cacheName", "aCache55"));

    cacheManager1.getCache("aCache1", Long.class, String.class).put(1L, "1");
    cacheManager2.getCache("aCache2", Long.class, String.class).put(2L, "2");

    assertThat(cacheManager1.getCache("aCache1", Long.class, String.class).get(1L), equalTo("1"));
    assertThat(cacheManager2.getCache("aCache2", Long.class, String.class).get(2L), equalTo("2"));

    CacheConfiguration<Long, String> cacheConfiguration =
        CacheConfigurationBuilder.newCacheConfigurationBuilder()
            .withResourcePools(
                ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES).build())
            .buildConfig(Long.class, String.class);
    cacheManager1.createCache("aCache4", cacheConfiguration);

    cacheManager1.getCache("aCache4", Long.class, String.class).put(4L, "4");
    assertThat(cacheManager1.getCache("aCache4", Long.class, String.class).get(4L), equalTo("4"));

    ResultSet<ContextualReturn<Void>> results =
        service.withCapability("ActionsCapability").call("clear").on(contextList).build().execute();

    assertThat(results.size(), Matchers.equalTo(4));

    assertThat(results.getResult(contextList.get(0)).hasValue(), is(true));
    assertThat(results.getResult(contextList.get(1)).hasValue(), is(true));
    assertThat(results.getResult(contextList.get(2)).hasValue(), is(true));
    assertThat(results.getResult(contextList.get(3)).hasValue(), is(false));

    assertThat(results.getResult(contextList.get(0)).getValue(), is(nullValue()));
    assertThat(results.getResult(contextList.get(1)).getValue(), is(nullValue()));
    assertThat(results.getResult(contextList.get(2)).getValue(), is(nullValue()));

    try {
      results.getResult(contextList.get(3)).getValue();
      fail();
    } catch (Exception e) {
      assertThat(e, instanceOf(NoSuchElementException.class));
    }

    assertThat(
        cacheManager1.getCache("aCache1", Long.class, String.class).get(1L),
        is(Matchers.nullValue()));
    assertThat(
        cacheManager2.getCache("aCache2", Long.class, String.class).get(2L),
        is(Matchers.nullValue()));
    assertThat(
        cacheManager1.getCache("aCache4", Long.class, String.class).get(4L),
        is(Matchers.nullValue()));
  }
Esempio n. 5
0
 private Cache<String, Object> getCacheForGroupOrCreateIt(String group) {
   Cache<String, Object> cache = cacheManager.getCache(group, String.class, Object.class);
   if (cache == null) {
     synchronized (lock) {
       cache = cacheManager.getCache(group, String.class, Object.class);
       if (cache == null) {
         cache =
             cacheManager.createCache(
                 group, cacheTemplate.buildConfig(String.class, Object.class));
       }
     }
   }
   return cache;
 }
  @Test
  public void testSerializers() throws Exception {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/default-serializer.xml"));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();

    Cache<Long, Double> bar = cacheManager.getCache("bar", Long.class, Double.class);
    bar.put(1L, 1.0);
    assertThat(bar.get(1L), equalTo(1.0));

    Cache<String, String> baz = cacheManager.getCache("baz", String.class, String.class);
    baz.put("1", "one");
    assertThat(baz.get("1"), equalTo("one"));

    Cache<String, Object> bam =
        cacheManager.createCache(
            "bam", newCacheConfigurationBuilder().buildConfig(String.class, Object.class));
    bam.put("1", "one");
    assertThat(bam.get("1"), equalTo((Object) "one"));

    cacheManager.close();
  }