Terracotta 10.7 | Ehcache API Developer Guide | The JCache (JSR-107) Cache Provider | Getting Started with Ehcache and JCache (JSR-107)
 
Getting Started with Ehcache and JCache (JSR-107)
In addition to the Cache interface, the JCache specification defines the interfaces CachingProvider and CacheManager. Applications need to use a CacheManager to create/retrieve a Cache. Similarly a CachingProvider is required to get/access a CacheManager.
Here is a code sample that demonstrates the usage of the basic JCache configuration APIs:

CachingProvider provider = Caching.getCachingProvider(); // <1>
CacheManager cacheManager = provider.getCacheManager(); // <2>
MutableConfiguration<Long, String> configuration =
new MutableConfiguration<Long, String>() // <3>
.setTypes(Long.class, String.class) // <4>
.setStoreByValue(false) // <5>
.setExpiryPolicyFactory(
CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // <6>
Cache<Long, String> cache =
cacheManager.createCache("jCache", configuration); // <7>
cache.put(1L, "one"); // <8>
String value = cache.get(1L); // <9>
assertThat(value, is("one"));
Citations from the previous code example:
1
Retrieves the default CachingProvider implementation from the application's classpath. This method will work if and only if there is exactly one JCache implementation jar in the classpath. If there are multiple providers in your classpath then use the fully qualified name org.ehcache.jsr107.EhcacheCachingProvider to retrieve the Ehcache caching provider. You can do this by using the Caching.getCachingProvider(String) static method instead.
2
Retrieve the default CacheManager instance using the provider.
3
Create a cache configuration using MutableConfiguration…​
4
with key type and value type as Long and String respectively…​
5
configured to store the cache entries by reference (not by value)…​
6
and with an expiry time of one minute defined for entries from the moment they are created.
7
Using the cache manager, create a cache named jCache with the configuration created in step 3.
8
Put some data into the cache.
9
Retrieve the data from the same cache.