Repository: polygene-java Updated Branches: refs/heads/develop f09636df1 -> 46e26e665
ehcache: upgrade to 3.3 ; optional use of FileConfig for disk overflow Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/4d726251 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/4d726251 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/4d726251 Branch: refs/heads/develop Commit: 4d7262518a0b3fac29f7f8217cf09856e485ecf2 Parents: f09636d Author: Paul Merlin <[email protected]> Authored: Mon Mar 6 11:21:50 2017 +0100 Committer: Paul Merlin <[email protected]> Committed: Mon Mar 6 11:21:50 2017 +0100 ---------------------------------------------------------------------- dependencies.gradle | 2 +- extensions/cache-ehcache/build.gradle | 1 + .../cache-ehcache/src/docs/cache-ehcache.txt | 3 + .../cache/ehcache/EhCachePoolMixin.java | 87 ++++++++++++-------- .../polygene/cache/ehcache/EhCacheTest.java | 20 +++-- 5 files changed, 73 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/4d726251/dependencies.gradle ---------------------------------------------------------------------- diff --git a/dependencies.gradle b/dependencies.gradle index adb7bb6..1188eb0 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -46,7 +46,7 @@ def codahaleMetricsVersion = '3.1.2' def commonsDbcpVersion = '2.1.1' def commonsLangVersion = '3.5' def derbyVersion = '10.13.1.1' -def ehcacheVersion = '3.1.3' +def ehcacheVersion = '3.3.0' def elasticsearchVersion = '5.0.1' def freemarkerVersion = '2.3.25-incubating' def geodeVersion = '1.0.0-incubating' http://git-wip-us.apache.org/repos/asf/polygene-java/blob/4d726251/extensions/cache-ehcache/build.gradle ---------------------------------------------------------------------- diff --git a/extensions/cache-ehcache/build.gradle b/extensions/cache-ehcache/build.gradle index 279ed20..48bd5a5 100644 --- a/extensions/cache-ehcache/build.gradle +++ b/extensions/cache-ehcache/build.gradle @@ -26,6 +26,7 @@ jar { manifest { name = "Apache Polygene⢠Extension: EhCache" } } dependencies { api polygene.core.bootstrap + api polygene.library( 'fileconfig' ) implementation polygene.library( 'constraints' ) implementation libraries.ehcache http://git-wip-us.apache.org/repos/asf/polygene-java/blob/4d726251/extensions/cache-ehcache/src/docs/cache-ehcache.txt ---------------------------------------------------------------------- diff --git a/extensions/cache-ehcache/src/docs/cache-ehcache.txt b/extensions/cache-ehcache/src/docs/cache-ehcache.txt index 3ffcfbd..ca462d3 100644 --- a/extensions/cache-ehcache/src/docs/cache-ehcache.txt +++ b/extensions/cache-ehcache/src/docs/cache-ehcache.txt @@ -54,3 +54,6 @@ Here are the configuration properties for the EhCache EntityStore Cache: source=extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCacheConfiguration.java tag=config ---- + +Cache overflow to disk may be configured using the <<library-fileconfig>>. +If the FileConfig library is not in use, then it defaults to a temporary directory in `java.io.tmpdir`. http://git-wip-us.apache.org/repos/asf/polygene-java/blob/4d726251/extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCachePoolMixin.java ---------------------------------------------------------------------- diff --git a/extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCachePoolMixin.java b/extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCachePoolMixin.java index ab9c593..dbf9ed4 100644 --- a/extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCachePoolMixin.java +++ b/extensions/cache-ehcache/src/main/java/org/apache/polygene/cache/ehcache/EhCachePoolMixin.java @@ -19,13 +19,16 @@ */ package org.apache.polygene.cache.ehcache; +import java.io.File; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; - +import org.apache.polygene.api.common.Optional; import org.apache.polygene.api.configuration.Configuration; import org.apache.polygene.api.identity.HasIdentity; +import org.apache.polygene.api.injection.scope.Service; import org.apache.polygene.api.injection.scope.This; +import org.apache.polygene.library.fileconfig.FileConfiguration; import org.apache.polygene.spi.cache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; @@ -34,7 +37,8 @@ import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; import org.ehcache.expiry.Duration; -import org.ehcache.expiry.Expirations; + +import static org.ehcache.expiry.Expirations.*; public abstract class EhCachePoolMixin implements EhCachePoolService @@ -48,30 +52,46 @@ public abstract class EhCachePoolMixin @This private Configuration<EhCacheConfiguration> configuration; + @Optional + @Service + private FileConfiguration fileConfiguration; + private CacheManager cacheManager; @Override public void activateService() - throws Exception + throws Exception { - cacheManager = CacheManagerBuilder.newCacheManagerBuilder() - .withDefaultDiskStoreThreadPool( cacheManagerThreadPoolName( "disk-store" ) ) - .withDefaultEventListenersThreadPool( cacheManagerThreadPoolName( "event-listeners" ) ) - .withDefaultWriteBehindThreadPool( cacheManagerThreadPoolName( "write-behind" ) ) - .build(); + cacheManager = CacheManagerBuilder + .newCacheManagerBuilder() + .with( CacheManagerBuilder.persistence( diskStorePath().getAbsolutePath() ) ) + .withDefaultDiskStoreThreadPool( cacheManagerThreadPoolName( "disk-store" ) ) + .withDefaultEventListenersThreadPool( cacheManagerThreadPoolName( "event-listeners" ) ) + .withDefaultWriteBehindThreadPool( cacheManagerThreadPoolName( "write-behind" ) ) + .build(); cacheManager.init(); } + private File diskStorePath() + { + String stringIdentity = identity.identity().get().toString(); + if( fileConfiguration != null ) + { + return new File( fileConfiguration.cacheDirectory(), stringIdentity ); + } + return new File( System.getProperty( "java.io.tmpdir" ), stringIdentity ); + } + @Override public void passivateService() - throws Exception + throws Exception { cacheManager.close(); cacheManager = null; } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings( "unchecked" ) public <T> Cache<T> fetchCache( String cacheId, Class<T> valueType ) { // Note: Small bug in Ehcache; If the cache name is an empty String it will actually work until @@ -92,51 +112,52 @@ public abstract class EhCachePoolMixin configuration.refresh(); EhCacheConfiguration config = configuration.get(); - ResourcePoolsBuilder poolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder() - .heap( config.heapSize().get(), MemoryUnit.valueOf( config.heapUnit().get() ) ); + ResourcePoolsBuilder poolsBuilder = ResourcePoolsBuilder.newResourcePoolsBuilder(); + + poolsBuilder = poolsBuilder.heap( config.heapSize().get(), MemoryUnit.valueOf( config.heapUnit().get() ) ); + if( config.offHeapSize().get() != null ) { - poolsBuilder = poolsBuilder - .offheap( config.offHeapSize().get(), MemoryUnit.valueOf( config.offHeapUnit().get() ) ); + poolsBuilder = poolsBuilder.offheap( config.offHeapSize().get(), + MemoryUnit.valueOf( config.offHeapUnit().get() ) ); } if( config.diskSize().get() != null ) { - poolsBuilder = poolsBuilder - .disk( config.diskSize().get(), MemoryUnit.valueOf( config.diskUnit().get() ), config.diskPersistent().get() ); + poolsBuilder = poolsBuilder.disk( config.diskSize().get(), + MemoryUnit.valueOf( config.diskUnit().get() ), + config.diskPersistent().get() ); } CacheConfigurationBuilder<String, T> configBuilder = CacheConfigurationBuilder - .newCacheConfigurationBuilder( String.class, valueType, poolsBuilder ); + .newCacheConfigurationBuilder( String.class, valueType, poolsBuilder ); if( config.maxObjectSize().get() != null ) { - configBuilder = configBuilder - .withSizeOfMaxObjectSize( config.maxObjectSize().get(), MemoryUnit.valueOf( config.maxObjectSizeUnit().get() ) ); + configBuilder = configBuilder.withSizeOfMaxObjectSize( config.maxObjectSize().get(), + MemoryUnit.valueOf( config.maxObjectSizeUnit().get() ) ); } if( config.maxObjectGraphDepth().get() != null ) { - configBuilder = configBuilder - .withSizeOfMaxObjectGraph( config.maxObjectGraphDepth().get() ); + configBuilder = configBuilder.withSizeOfMaxObjectGraph( config.maxObjectGraphDepth().get() ); } - switch( config.expiry().get() ) { + switch( config.expiry().get() ) + { case "TIME_TO_IDLE": - configBuilder = configBuilder.withExpiry( Expirations.timeToIdleExpiration( Duration.of( - config.expiryLength().get() == null ? -1L : config.expiryLength().get(), - TimeUnit.valueOf( config.expiryTimeUnit().get() ) - ) ) ); + configBuilder = configBuilder.withExpiry( timeToIdleExpiration( Duration.of( + config.expiryLength().get() == null ? - 1L : config.expiryLength().get(), + TimeUnit.valueOf( config.expiryTimeUnit().get() ) ) ) ); break; case "TIME_TO_LIVE": - configBuilder = configBuilder.withExpiry( Expirations.timeToLiveExpiration( Duration.of( - config.expiryLength().get() == null ? -1L : config.expiryLength().get(), - TimeUnit.valueOf( config.expiryTimeUnit().get() ) - ) ) ); + configBuilder = configBuilder.withExpiry( timeToLiveExpiration( Duration.of( + config.expiryLength().get() == null ? - 1L : config.expiryLength().get(), + TimeUnit.valueOf( config.expiryTimeUnit().get() ) ) ) ); break; case "NONE": default: - configBuilder = configBuilder.withExpiry( Expirations.noExpiration() ); + configBuilder = configBuilder.withExpiry( noExpiration() ); break; } CacheConfiguration<String, T> cacheConfig = configBuilder.build(); - org.ehcache.Cache<String,T> cache = cacheManager.createCache( cacheId, cacheConfig ); + org.ehcache.Cache<String, T> cache = cacheManager.createCache( cacheId, cacheConfig ); return new EhCacheImpl<>( cacheId, cache, valueType ); } @@ -154,6 +175,6 @@ public abstract class EhCachePoolMixin private String cacheManagerThreadPoolName( String name ) { - return identity.identity().getClass() + "-" + name; + return identity.identity().get().toString() + "-" + name; } } http://git-wip-us.apache.org/repos/asf/polygene-java/blob/4d726251/extensions/cache-ehcache/src/test/java/org/apache/polygene/cache/ehcache/EhCacheTest.java ---------------------------------------------------------------------- diff --git a/extensions/cache-ehcache/src/test/java/org/apache/polygene/cache/ehcache/EhCacheTest.java b/extensions/cache-ehcache/src/test/java/org/apache/polygene/cache/ehcache/EhCacheTest.java index f52ba1c..52e6291 100644 --- a/extensions/cache-ehcache/src/test/java/org/apache/polygene/cache/ehcache/EhCacheTest.java +++ b/extensions/cache-ehcache/src/test/java/org/apache/polygene/cache/ehcache/EhCacheTest.java @@ -20,28 +20,36 @@ package org.apache.polygene.cache.ehcache; import org.apache.polygene.api.common.Visibility; -import org.apache.polygene.bootstrap.AssemblyException; import org.apache.polygene.bootstrap.ModuleAssembly; import org.apache.polygene.cache.ehcache.assembly.EhCacheAssembler; +import org.apache.polygene.library.fileconfig.FileConfigurationAssembler; +import org.apache.polygene.library.fileconfig.FileConfigurationOverride; import org.apache.polygene.test.EntityTestAssembler; import org.apache.polygene.test.cache.AbstractCachePoolTest; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; public class EhCacheTest extends AbstractCachePoolTest { + @Rule + public final TemporaryFolder tmpDir = new TemporaryFolder(); + @Override // START SNIPPET: assembly public void assemble( ModuleAssembly module ) - throws AssemblyException { - // END SNIPPET: assembly + // END SNIPPET: assembly ModuleAssembly confModule = module.layer().module( "confModule" ); new EntityTestAssembler().visibleIn( Visibility.layer ).assemble( confModule ); + new FileConfigurationAssembler() + .withOverride( new FileConfigurationOverride().withConventionalRoot( tmpDir.getRoot() ) ) + .assemble( module ); // START SNIPPET: assembly - new EhCacheAssembler(). - withConfig( confModule, Visibility.layer ). - assemble( module ); + new EhCacheAssembler() + .withConfig( confModule, Visibility.layer ) + .assemble( module ); } // END SNIPPET: assembly }
