Page History
This page explains how to cache arbitrary objects in Magnolia. You can cache basically every object. This takes caching well beyond page and page fragment page fragment caching.
Table of Contents | ||
---|---|---|
|
...
The functionality for caching arbitrary objects is provided by at least two modules.
- Module
magnolia-cache-core
which defines the cache API. - A second module which implements the API. Magnolia provides two implementations based on established third-party libraries for the low-level functions.
magnolia-cache-ehcache
is the default implementation which uses Ehcache.magnolia-cache-ehcache3
is the default implementation which uses Ehcache3.magnolia-cache-memcached
is is a high-performance, distributed memory object caching system which uses Memcached.
...
When putting objects to the cache you can restrict their lifetime. In this case, instead of checking the validity of cached objects, you can simply let them expire over time.
Rebuild the cache on non-persistent implementations after server restart
Warning |
---|
Whether cache is persistent depends on the underlying implementation. For non-persistent implementations the cache must be rebuilt after server restart. See Cache implementations for more. |
Implementations:
...
EhCache with free license
...
Configure the cache
Magnolia allows to configure a cache factory specifically for each cache. For example you could have a specific configuration for fooBarCache
. When no specific configuration is defined, the default
factory configuration is used.
Info |
---|
Configuration differs depending on which version of Ehcache you use. |
Ehcache
This is an example of how to configure a cache using Ehcache 2. Here you will find documentation of all configuration options.
Extend the default
factory and override the persistence strategy with none
. This persistence strategy allows to cache non-serializable objects.
Advanced Tables - Table Plus | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||
|
The /modules/cache/config/cacheFactory/caches/fooBarCache
node defines the cache name. fooBarCache
is the "name" of the cache which was also used above when acquiring the Cache object within Java code.
Ehcache3
This is an example of how to configure a cache using Ehcache 3. Here you will find documentation of all configuration options.
Extend the default
factory and override the persistence strategy for disk
set to the value false
. This persistence strategy allows to cache non-serializable objects.
Advanced Tables - Table Plus | |||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||||||||
|
The /modules/cache/config/cacheFactory/delegateFactories/fooBarCache
node defines the cache name. fooBarCache
is the "name" of the cache which was also used above when acquiring the Cache object within Java code.
EhCache with eh-cache commercial license
...
Get the cache
Achieve an instance of
Javadoc resource link | ||||||
---|---|---|---|---|---|---|
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
import javax.inject.Inject;
import javax.inject.Provider;
import info.magnolia.module.cache.Cache;
import info.magnolia.module.cache.inject.CacheFactoryProvider;
public class MyClass {
private final Provider<CacheFactoryProvider> cacheFactoryProvider;
@Inject
public MyClass(Provider<CacheFactoryProvider> cacheFactoryProvider){
this.cacheFactoryProvider = cacheFactoryProvider;
}
public void fooBar(){
Cache cache = cacheFactoryProvider.get().get().getCache("fooBarCache");
}
} |
...
All subsequent actions - adding, reading and removing objects - are applied on this Cache
object.
Configure the cache
Magnolia allows to configure a cache factory specifically for each cache. For example you could have a specific configuration for fooBarCache
. When no specific configuration is defined, the default
factory configuration is used.
Example: Extend the default
factory and override the persistence strategy with none
. This persistence strategy allows to cache non-serializable objects.
...
heading | 0 |
---|---|
multiple | false |
enableHeadingAttributes | false |
enableSorting | false |
class | m5-configuration-tree |
enableHighlighting | false |
Node name | Value | |
---|---|---|
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ../default | |
| 300 | |
| ||
| none |
The /modules/cache/config/cacheFactory/caches/fooBarCache
node defines the cache name. fooBarCache
is the "name" of the cache which was also used above when acquiring the Cache object within Java code.
Cache Interface
Code Pro | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Put to cache
Javadoc resource link | ||||||
---|---|---|---|---|---|---|
|
...
This method is used more to "observe" or manage cached objects than to fetch a specific object. The Magnolia Cache Browser app is using #getQuiet, for instance.
...