This post if part of a series about the File System Toolkit - a custom content delivery API for SDL Tridion.
This post describes the Cache Factory the Toolkit uses to store commonly used models. The factory interfaces with an underlying EHCache instance named 'toolkit-cache'. Configuring the cache is possible either using a ehcache.properties file present at run-time in the application's class-path, or in the Toolkit configuration file toolkit.properties.
Configuring the Toolkit cache is described in post Installation and Configuration. Creating an EHCache programmatically is described in post Create an EHCache Programmatically.
The Cache Factory is used mainly by the Model Factory in its interaction with storing/retrieving JSON models.
The factory provides three main operations: get, put, remove.
The EHCache stores CacheElement generic objects that are simply Java beans around properties: payload of generic type and a last-check timestamp used in determining a stale value.
There is one additional stale check, which I explain in the next post Cache Invalidation.
This post describes the Cache Factory the Toolkit uses to store commonly used models. The factory interfaces with an underlying EHCache instance named 'toolkit-cache'. Configuring the cache is possible either using a ehcache.properties file present at run-time in the application's class-path, or in the Toolkit configuration file toolkit.properties.
Configuring the Toolkit cache is described in post Installation and Configuration. Creating an EHCache programmatically is described in post Create an EHCache Programmatically.
The Cache Factory is used mainly by the Model Factory in its interaction with storing/retrieving JSON models.
The factory provides three main operations: get, put, remove.
The EHCache stores CacheElement generic objects that are simply Java beans around properties: payload of generic type and a last-check timestamp used in determining a stale value.
public class CacheElement<T> { private long lastCheck; private T payload; public CacheElement(T payload) { this.payload = payload; lastCheck = System.currentTimeMillis(); } // accessor methods below... }
Method get(String)
This method looks up a value in the cache based on the given key passed as parameter. If the value is present in the cache, it is returned in the form of a generic CacheElement object with a specific payload type.There is one additional stale check, which I explain in the next post Cache Invalidation.
public <T> CacheElement<T> get(String key) { Element element = cache.get(key); if (element == null) { return null; } else { if (isStale(element)) { remove(key); return null; } CacheElement<T> result = (CacheElement<T>) element.getObjectValue(); return result; } }
Method put(String, T payload)
This method stores a given payload object in the EHCache under a certain key. If the key is already present, it simply replaces its value.public <T> void put(String key, T payload) { Element element = new Element(key, new CacheElement<>(payload)); if (cache.isKeyInCache(key)) { cache.replace(element); } else { cache.put(element); } }
Method remove(String)
Finally, the remove method removes an entry from the EHCache corresponding to the given key.public boolean remove(String key) { return cache.remove(key); }
Comments