Java Code Examples for org.ehcache.config.ResourcePools

Following code examples demonstrate how to use org.ehcache.config.ResourcePoolsfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.config.ResourcePoolsand various code implementation of this class.

  public void testThrowsWithNullValueType() {
    expectedException.expect(NullPointerException.class);
    expectedException.expectMessage("valueType");

    new BaseCacheConfiguration<>(Long.class, null, null,
      null, null, mock(ResourcePools.class));
  } 


  public void testThrowsWithNullKeyType() {
    expectedException.expect(NullPointerException.class);
    expectedException.expectMessage("keyType");

    new BaseCacheConfiguration<>(null, String.class, null,
      null, null, mock(ResourcePools.class));
  } 

	public void test() {

		EvictionAdvisor<K, V> evictionAdvisor = null;
		Expiry<K, V> expiry = null;
		ResourcePools resourcePools = null;
		ServiceConfiguration serviceConfigurations1 = null;
		ServiceConfiguration serviceConfigurations2 = null;

		BaseCacheConfiguration<K, V> base = new BaseCacheConfiguration<>(K.class, V.class, evictionAdvisor,
				this.getClass().getClassLoader(), expiry, resourcePools, serviceConfigurations1,
				serviceConfigurations2);
		

	} 

			public void customize(CacheManager cacheManager) {
				ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
						.heap(ON_HEAP_CACHE_SIZE_ENTRIES, EntryUnit.ENTRIES)
						.offheap(OFF_HEAP_CACHE_SIZE_MB, MemoryUnit.MB).build();

				CacheEventListenerConfigurationBuilder eventLoggerConfig = CacheEventListenerConfigurationBuilder
						.newEventListenerConfiguration(new CacheEventLogger(), EventType.CREATED, EventType.EXPIRED)
						.unordered().asynchronous();

				CacheConfiguration<?, ?> cacheConfiguration = CacheConfigurationBuilder
						.newCacheConfigurationBuilder(Long.class, BigDecimal.class, resourcePools)
						.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(CACHE_EXPIRY_SECONDS)))
						.add(eventLoggerConfig).build();

				cacheManager.createCache("squareCache",
						Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));

			} 

    public void testProgrammaticConfiguration() throws Exception {
        EhcacheManager manager = ehcacheConf.getManager();
        Cache<String, String> cache = manager.getCache("myCacheConf", String.class, String.class);

        ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools();

        SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP);
        assertNotNull(h);
        assertEquals(100, h.getSize());
        assertEquals(EntryUnit.ENTRIES, h.getUnit());

        SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP);
        assertNotNull(o);
        assertEquals(1, o.getSize());
        assertEquals(MemoryUnit.MB, o.getUnit());
    } 

    public EhCacheAdapter(EhCacheConfiguration configuration) {
        this.configuration = configuration;

        ResourcePools resourcePools = ResourcePoolsBuilder.heap(configuration.getResourcePoolSize()).build();

        ExpiryPolicy<Object, Object> expiry = ExpiryPolicyBuilder
                .timeToLiveExpiration(Duration.ofSeconds(configuration.getTimeToLiveSeconds()));

        org.ehcache.config.CacheConfiguration<String, Object> cacheConfiguration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, Object.class, resourcePools)
                .withExpiry(expiry)
                .build();

        cache = CACHE_MANAGER.createCache(configuration.getAlias(), cacheConfiguration);
    } 

    public EhCache(long heapEntries, long offHeapMBSize, String diskPath, long diskGBSize) {

        ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
                .heap(heapEntries, EntryUnit.ENTRIES)
                .offheap(offHeapMBSize, MemoryUnit.MB)
                .disk(diskGBSize, MemoryUnit.GB)
                .build();

        CacheConfiguration<String, Serializable> configuration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, Serializable.class, resourcePools)
                .build();

        cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .with(CacheManagerBuilder.persistence(diskPath))
                .withCache("cachex", configuration)
                .build(true);

        ehcache = cacheManager.getCache("cachex", String.class, Serializable.class);
    } 

  public void testUnSupportedResourceType() {
    XAStore.Provider provider = new XAStore.Provider();

    @SuppressWarnings("unchecked")
    Store.Configuration<Object, Object> configuration = mock(Store.Configuration.class);

    ResourcePools resourcePools = mock(ResourcePools.class);
    Set<ResourceType<?>> resourceTypes = new HashSet<>();
    resourceTypes.add(new TestResourceType());

    when(configuration.getResourcePools()).thenReturn(resourcePools);
    when(resourcePools.getResourceTypeSet()).thenReturn(resourceTypes);

    try {
      provider.createStore(configuration, (ServiceConfiguration<?>) null);
      fail("IllegalStateException expected");
    } catch (IllegalStateException e) {

    }

  } 

    public void testFileConfiguration() throws Exception {
        Cache<String, String> cache = getCache(ehcacheFileConf, "myFileCacheConf");
        ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools();

        SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP);
        assertNotNull(h);
        assertEquals(150, h.getSize());
        assertEquals(EntryUnit.ENTRIES, h.getUnit());
    } 

    public void testProgrammaticConfiguration() throws Exception {
        Cache<String, String> cache = getCache(ehcacheConf, "myProgrammaticCacheConf");
        ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools();

        SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP);
        assertNotNull(h);
        assertEquals(100, h.getSize());
        assertEquals(EntryUnit.ENTRIES, h.getUnit());

        SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP);
        assertNotNull(o);
        assertEquals(1, o.getSize());
        assertEquals(MemoryUnit.MB, o.getUnit());
    } 

    public void testCacheManager() throws Exception {
        assertEquals(
            context().getRegistry().lookupByNameAndType("ehcache", EhcacheComponent.class).getCacheManager(),
            endpoint.getManager().getCacheManager()
        );

        Cache<String, String> cache =  endpoint.getManager().getCache("myCache", String.class, String.class);
        ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools();

        SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP);
        assertNotNull(h);
        assertEquals(100, h.getSize());
        assertEquals(EntryUnit.ENTRIES, h.getUnit());

        SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP);
        assertNotNull(o);
        assertEquals(1, o.getSize());
        assertEquals(MemoryUnit.MB, o.getUnit());
    } 

Advertisement
Javadoc
A collection of ResourcePool resource pools that a org.ehcache.Cache Cache has at its disposal to st

ore its mappings.

Implementations must be immutable.

Read More
Advertisement