Using the ICacheManager in Pentaho’s BI Server

Caching objects is a core capability of any server architecture. Within Pentaho’s BI Server, we have a simple API for defining caches that any plugin or component can use easily.  In a Java environment, the simplest cache is a java.util.Map.  This is used often and unfortunately it has some major drawbacks, the primary one being that you can run out of memory if you’re not careful.  Why invent your own caching solution when there is one that can easily be used?

Pentaho defines a simple org.pentaho.platform.api.engine.ICacheManager interface, and the default implementation, org.pentaho.platform.plugin.services.cache.CacheManager, uses Hibernate’s Cache.  Here is a simple code example that demonstrates using Pentaho’s ICacheManager:

// get a reference to ICacheManager
ICacheManager cacheMgr = PentahoSystem.getCacheManager(pentahoSession);

// create a cache region if necessary
if(!cacheMgr.cacheEnabled("my_plugin_cache")) {
cacheMgr.addCacheRegion("my_plugin_cache");
}

// store an object into the cache
cacheMgr.putInRegionCache("my_plugin_cache", "url_1", "http://www.pentaho.com");

// now retrieve the item from the cache
String url = (String)cacheMgr.getFromRegionCache("my_plugin_cache", "url_1");

// now clear the cache
cacheMgr.clearRegionCache("my_plugin_cache");

That’s it!  To learn more about how to use the ICacheManager, check out our BI Server documentation at http://wiki.pentaho.com/display/ServerDoc2x/Using+PentahoSystem+ICacheManager.

No Comment

No comments yet

Leave a reply