Chapter 23. Cache invalidation extension

Table of Contents

Including in a project


Cache invalidation module is an extension that allows to define cache invalidation policy programmatically.

Including in a project




compile 'org.apache.cayenne:cayenne-cache-invalidation:4.1.M1'


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:

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 {
    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:

                // this will disable default handler based on @CacheGroups, and this is optional


You can combine as many invalidation handlers as you need.