Terracotta 10.3 | Ehcache API Developer Guide | User Managed Caches | Code examples for User Managed Caches
 
Code examples for User Managed Caches
Example of a basic cache lifecycle
Here is a simple example showing a basic lifecycle of a user managed cache:

UserManagedCache<Long, String> userManagedCache =
UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
.build(false); // <1>
userManagedCache.init(); // <2>

userManagedCache.put(1L, "The one!"); // <3>

userManagedCache.close(); // <4>
1
Create a UserManagedCache instance. You can either pass true to have the builder init() it for you, or you can pass false and it is up to you to init() it prior to using it.
2
Since false was passed in <1>, you have to init() the UserManagedCache prior to using it.
3
You can use the cache exactly as a managed cache.
4
In the same vein, a UserManagedCache requires you to close it explicitly using UserManagedCache.close(). If you are also using managed caches simultaneously, the CacheManager.close() operation would not impact the user managed cache(s).
From this basic example, explore the API of UserManagedCacheBuilder in code or through Javadoc to discover all the directly available features. The following features apply in the exact same way to user managed caches:
*Serializers and copiers. See the section Serializers and Copiers for related information.
*Eviction advisor. See the section Eviction Advisors for related information.
Simply use the methods from UserManagedCacheBuilder which are equivalent to the ones from CacheConfigurationBuilder.
Below we will describe a more advanced setup where you need to maintain a service instance in order to have a working user managed cache.
Example with disk persistence and lifecycle
If you want to use disk persistent cache, you will need to create and lifecycle the persistence service.
LocalPersistenceService persistenceService = new DefaultLocalPersistenceService(
new DefaultPersistenceConfiguration(
new File(getStoragePath(), "myUserData"))); // <1>

PersistentUserManagedCache<Long, String> cache =
UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
.with(new UserManagedPersistenceContext<Long, String>("cache-name",
persistenceService)) // <2>
.withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10L, EntryUnit.ENTRIES)
.disk(10L, MemoryUnit.MB, true)) // <3>
.build(true);

// Work with the cache
cache.put(42L, "The Answer!");
assertThat(cache.get(42L), is("The Answer!"));

cache.close(); // <4>
cache.destroy(); // <5>

persistenceService.stop(); // <6>
1
Create the persistence service to be used by the cache for storing data on disk.
2
Pass the persistence service to the builder as well as a name for the cache. Note that this will make the builder produce a more specific type: PersistentUserManagedCache.
3
As usual, indicate here if the data should outlive the cache.
4
Closing the cache will not delete the data it saved on disk, since the cache is marked as persistent.
5
To delete the data on disk after closing the cache, you need to invoke the destroy method explicitly.
6
You need to stop the persistence service once you have finished using the cache.
Example with cache event listeners
Cache event listeners require executor services in order to work. You will have to provide either a CacheEventDispatcher implementation or make use of the default one by providing two executor services: one for ordered events and one for unordered ones.
Note: The ordered events executor must be single threaded to guarantee ordering.
For more information on cache event listeners, see the section Cache Event Listeners.
UserManagedCache<Long, String> cache =
UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
.withEventExecutors(Executors.newSingleThreadExecutor(),
Executors.newFixedThreadPool(5)) // <1>
.withEventListeners(CacheEventListenerConfigurationBuilder
.newEventListenerConfiguration(ListenerObject.class, EventType.CREATED,
EventType.UPDATED)
.asynchronous()
.unordered()) // <2>
.withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(3, EntryUnit.ENTRIES))
.build(true);

cache.put(1L, "Put it");
cache.put(1L, "Update it");

cache.close();
1
Provide the ExecutorService for ordered and unordered event delivery.
2
Provide a listener configuration using CacheEventListenerConfigurationBuilder.

Copyright © 2010-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.