Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

Not persistent 

EhCache with free license

Persistent

EhCache with eh-cache commercial license

MemCache

Get the cache

Achieve an instance of 

Javadoc
0info.magnolia.module.cache.Cache
.  This is your cache to operate on. Give it a name which must be unique within the system.

...

Advanced Tables - Table Plus
heading0
multiplefalse
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue

Mgnl f
modules

 

Mgnl f
cache

 

Mgnl f
config

 

Mgnl n
cacheFactory

 

Mgnl n
caches

 

Mgnl n
default

 

Mgnl n
fooBarCache

 

Mgnl p
extends

../default

Mgnl p
timeToLiveSeconds

300

Mgnl n
persistence

 

Mgnl p
strategy

none

(warning) 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.

...

Code Block
languagejava
void put(Object key, Object value);
 
void put(Object key, Object value, int timeToLiveInSeconds);

Arguments:

key

required

The identifier of the object to cache

value

required

The object to cache

timeToLiveInSeconds

optional

The lifetime of the cached object in seconds.

Tip: The lifetime also can be configured globally, and you can configure a flush policy to clear the cache when some condition is met, or you may want to delete cached objects programmatically (see #remove and #clear below).

Example:

Code Block
languagejava
public class MyClass {
    public Object put(Object key, Object value){
        Cache cache = cacheFactoryProvider.get().get().getCache("fooBarCache");
        cache.put(key, value);
        return value;
    }
    public Object put(Object key, Object value, int timeToLiveInSeconds){
        Cache cache = cacheFactoryProvider.get().get().getCache("fooBarCache");
        cache.put(key, value, timeToLiveInSeconds);
        return value;
    }
} 

...

Info

Use #get when you are unsure whether the desired object is already cached. If the object is not yet cached, you should acquire it by other means and add it to the cache.

It is good practice to call #get and #put within the same associated try to -catch and -finally block for the same cache key.

If there is no object for a key, add null to the cache anyway. This way you can ensure that the service doesn't try to read data for a key without an existing value more than one time

...