There isn’t anything special for you to do to use Ehcache3 as the caching provider for your application: add the
ehcache-3.0.0.jar
to your application’s classpath (possibly removing the previous provider’s jar) and you are ready to
go:
CachingProvider provider = Caching.getCachingProvider(); // (1)
CacheManager cacheManager = provider.getCacheManager(); // (2)
-
Retrieves the default
CachingProvider
, this should beorg.ehcache.EhCachingProvider
, which you can also force, by using theCaching.getCachingProvider(String)
static method instead; -
Retrieve the default
CacheManager
instance using the provider.
You can also add a XML file that preconfigure Cache
instances upfront. See
the XML README file for more details on configuring Cache
in XML.
In order to pre-configure the CacheManager
at creation time using the XML file, simply:
CachingProvider provider = Caching.getCachingProvider();
CacheManager cacheManager = provider.getCacheManager( // (1)
this.getClass().getResource("/ehcache.xml").toURI(), // (2)
Customer.class.getClassLoader()); // (3)
-
Invoking
javax.cache.spi.CachingProvider.getCacheManager(java.net.URI, java.lang.ClassLoader)
-
where the first argument is an
URI
pointing to our XML configuration file, e.g.ehcache.xml
; -
the second argument being the
ClassLoader
to use to load user-types if needed; i.e.Class
instances that are stored in theCache
managed by ourCacheManager
.
Note
|
You can also use the CachingProvider.getCacheManager() method that doesn’t take any argument instead.
The URI and ClassLoader used to configure the CacheManager will then use the
vendor specific values returned by CachingProvider.getDefaultURI and .getDefaultClassLoader respectively.
Be aware that these aren¹t entirely spec¹ed for Ehcache3 and may change in future releases!
|
You can also create cache-templates
as of Ehcache3, see
Cache Templates section of the XML README file for more details. The Ehcache3
JSR-107 Caching Provider comes with an extension to the regular XML configuration, so you can:
-
Configure a default template all programmatically created
Cache
instances will inherit from, and -
Configure a given named
Cache
to inherit from a specific template.
This feature is particularly useful to configure Cache
beyond the JSR-107 specification, e.g. giving them a capacity
constraint. All is needed is adding a jsr107 service in your XML configuration file:
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'> <!--(1)-->
<service> <!--(2)-->
<jsr107:defaults default-template="tinyCache"> <!--(3)-->
<jsr107:cache name="foos" template="stringCache"/> <!--(4)-->
</jsr107:defaults>
</service>
<cache-template name="stringCache">
<key-type>java.lang.String</key-type>
<value-type>java.lang.String</value-type>
<capacity>2000</capacity>
</cache-template>
<cache-template name="tinyCache">
<capacity>20</capacity>
</cache-template>
</config>
-
First, declare a namespace for the 107 extension, e.g.
jsr107
-
Within a
service
element at the top of you configuration, add ajsr107:defaults
element -
The element takes an optional attribute
default-template
, which references thecache-template
to use for alljavax.cache.Cache
created by the application at runtime usingjavax.cache.CacheManager.createCache
. In this example, the defaultcache-template
used will betinyCache
, meaning that atop of their particular config, programmatically createdCache
instances will have their capacity constrained to 20 entries. -
Nested within the
jsr107:defaults
, add specificcache-templates
to use for given namedCache
, e.g. when creating theCache
namedfoos
at runtime, Ehcache will enhance its config, giving it a capacity of 2000 entries, as well as insuring both key and value types areString
.