https://github.com/vpro/jsr107-magnolia
A ComponentConfigurer for the Magnolia CMS, which enables JSR107 caching by annotations.
https://github.com/vpro/jsr107-magnolia
jsr-107 magnolia magnolia-cache magnolia-cms magnolia-component
Last synced: about 1 year ago
JSON representation
A ComponentConfigurer for the Magnolia CMS, which enables JSR107 caching by annotations.
- Host: GitHub
- URL: https://github.com/vpro/jsr107-magnolia
- Owner: vpro
- License: apache-2.0
- Created: 2016-02-15T12:13:07.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T09:11:38.000Z (almost 2 years ago)
- Last Synced: 2025-04-15T16:15:39.071Z (about 1 year ago)
- Topics: jsr-107, magnolia, magnolia-cache, magnolia-cms, magnolia-component
- Language: Java
- Homepage:
- Size: 524 KB
- Stars: 4
- Watchers: 7
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
Awesome Lists containing this project
README
= jsr107-magnolia
image:https://github.com/vpro/jsr107-magnolia/workflows/build/badge.svg?[Build Status,link=https://github.com/vpro/jsr107-magnolia/actions?query=workflow%3Abuild]
image:https://img.shields.io/maven-central/v/nl.vpro/jsr107-magnolia.svg?label=Maven%20Central[Maven Central,link=https://search.maven.org/search?q=g:%22nl.vpro%22%20AND%20a:%22jsr107-magnolia%22]
image:http://www.javadoc.io/badge/nl.vpro/jsr107-magnolia.svg?color=blue[javadoc,link=http://www.javadoc.io/doc/nl.vpro/jsr107-magnolia]
image:https://codecov.io/gh/vpro/jsr107-magnolia/branch/master/graph/badge.svg[codecov,link=https://codecov.io/gh/vpro/jsr107-magnolia]
image:https://img.shields.io/nexus/s/https/oss.sonatype.org/nl.vpro/jsr107-magnolia.svg[snapshots,link=https://oss.sonatype.org/content/repositories/snapshots/nl/vpro/jsr107-magnolia/]
See
- https://www.magnolia-cms.com/
- https://github.com/jsr107
After installation, you can cache the result of any method of any guice managed bean by adding the `@CacheResult` annotation.
[source,java]
----
@CacheResult(cacheName = "CinemaUtil-sortedMovies")
public List>> sortedMovies(Person person) {
...
}
----
In this case in the magnolia cache configuration automaticly a cache 'CinemaUtil-sortedMovies' will appear.
== Possible cache values
The cache values may be `null` and `Optional`. This implementation will arrange that no nulls are stored in the underlying magnolia cache. If the value is `Optional`, the value of the `Optional` will be serialized.
Non serializable values are only possible if the underlying eh-cache is configured not to store to disk.
== Model classes
Sadly, https://jira.magnolia-cms.com/browse/MAGNOLIA-6601["model classes are not instantiated by guice, but by Magnolia itself"], so they cannot be proxied by guice.
== Installation
Download the most recent jar from: https://oss.sonatype.org/content/repositories/snapshots/nl/vpro/jsr107-magnolia and install it like you'd normally would.
Or you can add this to your pom.xml
[source,xml]
----
nl.vpro
jsr107-magnolia
1.19
----
=== Configuration
For versions older then 1.14 caches were configured like so in the JCR-tree:
image:cache-config.png?raw=true[cache configuration]
From 1.14 onwards Magnolia 5.5.4 uses ehcache3 so the configuration has changed and looks like this:
image:cache-config-ehcache3.png?raw=true[cache configuration]
Cache-configurations can be automaticly created using tasks on the version handler of your module.
E.g. like this:
[source,java]
----
@Slf4j
public class CinemaVersionHandler extends DefaultModuleVersionHandler {
@Override
protected List getBasicInstallTasks(InstallContext installContext) {
List tasks = super.getBasicInstallTasks(installContext);
tasks.addAll(CreateConfigurationTasks.createConfigurationTasks(CinemaUtilWithCaching.class));
log.info("Created tasks {}", tasks);
return tasks;
}
}
----
Default settings could be configured using the `nl.vpro.magnolia.jsr107.DefaultCacheSettings` annotation:
[source,java]
----
@CacheResult(cacheName = "CinemaUtil-scheduleForChannel")
@DefaultCacheSettings(blockingTimeout = 30000)
List scheduleForChannel(String channel, LocalDate date) {
log.info("Getting movies for {} {}", channel, date);
MediaSearch search = new MediaSearch();
....
----
If you use an 'exception cache' too, you may want to configure this separately. You need to wrap a `@nl.vpro.magnolia.jsr107.Defaults` then.
[source,java]
----
@CacheResult(cacheKeyGenerator = ImageCacheKey.class, cacheName = ASSET_LINKS_CACHE, exceptionCacheName = ASSET_LINKS_CACHE + "-exceptions")
@Defaults(
overrideOnUpdate = true,
exceptionCacheSettings = @DefaultCacheSettings(maxElementsInMemory = 200, timeToLiveSeconds = 300),
cacheSettings = @DefaultCacheSettings(maxElementsInMemory = 2000, timeToLiveSeconds = 3600)
)
@Override
public String getAssetLink(Image image, String variation) {
----
Actually the code can also be accessed if you want to configure a cache programmaticly for some other reason. This more or less eliminates the need to configure cache outside code altogether.
The cache settings are in this way still visible in the JCR-tree, and can be modified and viewed via JMX, but they can be maintained in the code of your application.
[source,java]
----
// Create browser cache for api clients
setInstallOrUpdateTask(CreateCacheConfigurationTask.builder()
.name(CACHE)
.settings(CacheSettings.builder()
.eternal(true)
.overflowToDisk(true)
.diskSpoolBufferSizeMB(500)
.maxElementsInMemory(200)
.diskExpiryThreadInterval(Duration.ofHours(24))
)
.overrideOnUpdate(true)
.build());
----
== MgnlCacheManager
The `nl.vpro.magnolia.jsr107.MgnlCacheManager` implementation of `javax.cache.CacheManager` contains a few utilities which may come in useful when interacting with caches. E.g. utilities to get existing values from the caches, or to retrieve all keys, which can be used when activily refreshing entries in the cache (e.g. in conjection with `@javax.cache.annotation.CachePut`)
A `MgnlCacheManager` can simply be obtained using `@Inject`.
== Cache Event Listening
Since version 1.16 we will also support cache listening. E.g.
[source,java]
----
@Inject
public MediaPlayerPageCache(
Provider vtkUtil,
ServerConfiguration serverConfiguration,
Provider mgnlCacheManager,
@Named(SystemEventBus.NAME) EventBus systemEventBus
) {
this.vtkUtil = vtkUtil;
this.serverConfiguration = serverConfiguration;
this.mgnlCacheManager = mgnlCacheManager;
systemEventBus.addHandler(ModulesStartedEvent.class, this::registerCacheEntryListener);
}
protected void registerCacheEntryListener(ModulesStartedEvent event) {
log.info("{}", event);
mgnlCacheManager.get().getCache(CACHE_NAME).registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(
new FactoryBuilder.SingletonFactory<>(new Listener()),
new FactoryBuilder.SingletonFactory<>(e -> true),
false,
true));
}
----