loading table of contents...

Content Application Developer Manual / Version 2310

Table Of Contents

4.2.4 Writing Cacheable Beans

As mentioned above, the DataViewFactory's caching mechanism takes care of dependencies. Any data view property may define one or more objects (called "dependencies") on which this property depends on. When caching a property, two things are stored in the cache: The property's value as well as its dependencies. In case that any dependent object becomes invalid (by modifications on it, for example) the dependent property value becomes invalid as well and will be removed from the cache automatically.


A data view property "headline" is calculated from a row in a database table and so this row is defined as a dependency. When caching an instance of this property's value, the dependency is tracked as well. Changing the table's row causes the cached value to become invalid and this value to be removed from the cache.

Defining dependencies for a property value is done during the property's value computation by invoking the static method com.coremedia.cache.Cache#dependencyOn(Object) for each dependency. In order to notify the cache about a dependency invalidation, the method invalidate(Object) needs to be invoked on the DataViewFactory's Cache instance. As a result, any cached item depending on this object is removed from the cache.

public class Bean {
  public String getHeadline() {
    Cache.dependencyOn(new String("mydependency"));
    return getHeadlineFromDatabase();

Example 4.3. Bean property with custom dependency. Value of "headline" depends on dependency "mydependency".

DataViewFactory dataViewFactory = ...
Bean bean = new Bean();
Bean dataView = (Bean) dataViewFactory.loadCached(bean1);
String headline = dataView.getHeadline();

Example 4.4. Accessing getHeadline() causes the property's value to be cached together with the dependency "mydependency" of type "String" in case caching is enabled for Bean's property "headline".

DataviewFactory dataViewFactory = ...
Cache cache = dataViewFactory.getCache();
cache.invalidate(new String("mydependency"));

Example 4.5. Triggering an invalidation of the dependency "mydependency"

Types of dependencies

You may use any object as a dependency which is suitable as a key in a HashMap, typically by implementing the methods equals(Object) and hashCode() properly or by using the very same object as a dependency and for invalidation.

The class com.coremedia.cache.Cache already provides support for timed dependencies that invalidate automatically at a certain point in time. You may define these dependencies by invoking Cache#cacheUntil(Date) or Cache#cacheFor(long) during the evaluation of the cached property method. Have a look at com.coremedia.cache.Cache's Javadoc for further details.

Dependency tracking and Content Beans

When using ContentBeans or (more generally) the Unified API's content repository as the data source for your beans, you don't need to take care on the content's dependencies and invalidations: any access on the content repository's content objects causes appropriate dependencies to be tracked automatically. Further on, changes on the content objects leads to automatic invalidations. The only prerequisite (which is fulfilled by the default CAE configuration) is that the DataViewFactory and the Unified API share the same Cache instance.

Dependencies of the Unified API cache

Figure 4.4. Dependencies of the Unified API cache

Search Results

Table Of Contents

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.