Table of Contents
Cache invalidation module is an extension that allows to define cache invalidation policy programmatically.
<dependency> <groupId>org.apache.cayenne</groupId> <artifactId>cayenne-cache-invalidation</artifactId> <version>4.0.B2</version> </dependency>
Module supports autoloading mechanism, so no other actions required to enable it.
Just mark your entities with @CacheGroups
annotation and you are ready to use it:
@CacheGroups("some-group") public class MyEntity extends _MyEntity { // ... }
After any modification of MyEntity
objects cache group "some-group"
will be dropped from cache automatically.
You can read more about cache and cache groups in corresponding chapter of this documentation.
In case you need some complex logic of cache invalidation you can disable default behaviour and provide your own.
To do so you need to implement org.apache.cayenne.cache.invalidation.InvalidationHandler
interface and setup Cache Invalidation module to
use it.
Let's use implementation class called CustomInvalidationHandler
that will simply match
all entities' types with "custom-group"
cache group regardless of any annotations:
public class CustomInvalidationHandler implements InvalidationHandler { @Override public InvalidationFunction canHandle(Class<? extends Persistent> type) { return p -> Collections.singleton(new CacheGroupDescriptor("custom-group")); } }
Now we'll set up it's usage by ServerRuntime
:
ServerRuntime.builder() .addModule(CacheInvalidationModule.extend() // this will disable default handler based on @CacheGroups, and this is optional .noCacheGroupsHandler() .addHandler(CustomInvalidationHandler.class) .module())
You can combine as many invalidation handlers as you need.