Java Code Examples for org.ehcache.PersistentCacheManager#createCache()

The following examples show how to use org.ehcache.PersistentCacheManager#createCache() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void clusteredCacheManagerWithDynamicallyAddedCacheExample() throws Exception {
  // tag::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
          = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
                  .autoCreateOnReconnect(server -> server.defaultServerResource("primary-server-resource")
                    .resourcePool("resource-pool-a", 8, MemoryUnit.MB)));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))).build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);

  } finally {
    cacheManager.close();
  }
  // end::clusteredCacheManagerWithDynamicallyAddedCacheExample[]
}
 
Example 2
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void explicitConsistencyConfiguration() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
          = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
                  .autoCreateOnReconnect(server -> server.defaultServerResource("primary-server-resource")
                    .resourcePool("resource-pool-a", 8, MemoryUnit.MB)));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    // tag::clusteredCacheConsistency[]
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB)))
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG)) // <1>
        .build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
    cache.put(42L, "All you need to know!"); // <2>

    // end::clusteredCacheConsistency[]
  } finally {
    cacheManager.close();
  }
}
 
Example 3
Source File: BasicLifeCyclePassiveReplicationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDestroyCacheManager() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> configBuilder = newCacheManagerBuilder().with(cluster(CLUSTER.getConnectionURI().resolve("/destroy-CM"))
    .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager1 = configBuilder.build(true);
  PersistentCacheManager cacheManager2 = configBuilder.build(true);

  cacheManager2.close();

  try {
    cacheManager2.destroy();
    fail("Exception expected");
  } catch (Exception e) {
    e.printStackTrace();
  }

  CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
  CLUSTER.getClusterControl().terminateActive();

  cacheManager1.createCache("test", newCacheConfigurationBuilder(Long.class, String.class, heap(10).with(clusteredDedicated(10, MB))));
}
 
Example 4
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void clusteredCacheTieredExample() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
      = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
          .autoCreateOnReconnect(server -> server.defaultServerResource("primary-server-resource")
            .resourcePool("resource-pool-a", 8, MemoryUnit.MB)));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    // tag::clusteredCacheTieredExample[]
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
            .heap(2, MemoryUnit.MB) // <1>
            .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB))) // <2>
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
        .build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache-tiered", config);
    cache.put(42L, "All you need to know!");

    // end::clusteredCacheTieredExample[]
  } finally {
    cacheManager.close();
  }
}
 
Example 5
Source File: LeaseTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void leaseExpiry() throws Exception {
  URI connectionURI = TCPProxyUtil.getProxyURI(CLUSTER.get().getConnectionURI(), proxies);

  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(connectionURI.resolve("/crud-cm"))
      .timeouts(TimeoutsBuilder.timeouts().connection(Duration.ofSeconds(20)))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
    resourcePoolsBuilder).build();

  Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
  cache.put(1L, "The one");
  cache.put(2L, "The two");
  cache.put(3L, "The three");
  assertThat(cache.get(1L), equalTo("The one"));
  assertThat(cache.get(2L), equalTo("The two"));
  assertThat(cache.get(3L), equalTo("The three"));

  long delay = CLUSTER.input().plusSeconds(1L).toMillis();
  setDelay(delay, proxies);
  try {
    Thread.sleep(delay);
  } finally {
    setDelay(0L, proxies);
  }

  assertThat(() -> cache.get(1L), within(Duration.ofSeconds(60)).is("The one"));
  assertThat(cache.get(2L), equalTo("The two"));
  assertThat(cache.get(3L), equalTo("The three"));

}
 
Example 6
Source File: BasicClusteredCacheOpsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void basicCacheCRUD() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
      = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/crud-cm"))
          .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  final PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
            .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 1, MemoryUnit.MB))).build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
    cache.put(1L, "The one");
    assertThat(cache.containsKey(2L), is(false));
    cache.put(2L, "The two");
    assertThat(cache.containsKey(2L), is(true));
    cache.put(1L, "Another one");
    cache.put(3L, "The three");
    assertThat(cache.get(1L), equalTo("Another one"));
    assertThat(cache.get(2L), equalTo("The two"));
    assertThat(cache.get(3L), equalTo("The three"));
    cache.remove(1L);
    assertThat(cache.get(1L), is(nullValue()));

    cache.clear();
    assertThat(cache.get(1L), is(nullValue()));
    assertThat(cache.get(2L), is(nullValue()));
    assertThat(cache.get(3L), is(nullValue()));
  } finally {
    cacheManager.close();
  }
}
 
Example 7
Source File: PassiveSyncTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 150000)
public void testSync() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
    = CacheManagerBuilder.newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/op-sync"))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  final PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 1, MemoryUnit.MB))).build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);

    for (long i = -5; i < 5; i++) {
      cache.put(i, "value" + i);
    }

    CLUSTER.getClusterControl().startOneServer();
    CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
    CLUSTER.getClusterControl().terminateActive();


    assertThat(() -> cache.get(0L), within(Duration.ofSeconds(130)).matches(notNullValue()));
    for (long i = -5; i < 5; i++) {
      assertThat(cache.get(i), equalTo("value" + i));
    }
  } finally {
    cacheManager.close();
  }
}
 
Example 8
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void unknownClusteredCacheExample()
{
  // tag::unspecifiedClusteredCacheExample[]

  CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilderAutoCreate = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
            .autoCreateOnReconnect(server -> server  // <1>
              .resourcePool("resource-pool", 8, MemoryUnit.MB, "primary-server-resource")));

  PersistentCacheManager cacheManager1 = cacheManagerBuilderAutoCreate.build(false);
  cacheManager1.init();
  try {
    CacheConfiguration<Long, String> cacheConfigDedicated = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB)))  // <2>
      .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
      .build();

    Cache<Long, String> cacheDedicated = cacheManager1.createCache("my-dedicated-cache", cacheConfigDedicated);  // <3>

    CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilderExpecting = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
        .expecting(server -> server  // <4>
          .resourcePool("resource-pool", 8, MemoryUnit.MB, "primary-server-resource")));

    PersistentCacheManager cacheManager2 = cacheManagerBuilderExpecting.build(false);
    cacheManager2.init();
    try {
      CacheConfiguration<Long, String> cacheConfigUnspecified = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
          .with(ClusteredResourcePoolBuilder.clustered()))  // <5>
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG))
        .build();

      Cache<Long, String> cacheUnspecified = cacheManager2.createCache("my-dedicated-cache", cacheConfigUnspecified); // <6>
    } finally {
      cacheManager2.close();
    }
  } finally {
    cacheManager1.close();
  }
  // end::unspecifiedClusteredCacheExample[]
  }