The Cache Management API provides a uniform way to manage internal portal caches. Instead of managing each individual cache, you can use this API to perform standard actions, such as enabling, disabling or clearing a cache, as well as custom actions available for specific caches. This API is most useful for performing batch actions on all caches.
Note
The Cache Management API is currently supported by the navigation and OBN caches.
The Cache Management API (ICacheManager interface) is part of the com.sap.portal.cache.api package.
To use the API, you need to obtain a reference to ICacheManager:
Syntax
ICacheManager cacheManager = (ICacheManager); PortalRuntime.getRuntimeResources().getService(ICacheManager.KEY);
With the Cache Management API, you can perform the following tasks:
The following example shows how to enable or disable all available caches:
Example
String[] enableFailed = cacheManager.setEnabled(true); String[] disableFailed = cacheManager.setEnabled(false);
To perform an action on all available caches in a batch, you obtain the list of all cache IDs and iterate through it to perform the required action on each cache. The following example illustrates clearing all caches.
Example
ICacheManager cacheManager = (ICacheManager) PortalRuntime.getRuntimeResources().getService(ICacheManager.KEY); String[] allCacheIDs = cacheManager.getAllCacheIDs(); for (int i = 0; i < allCacheIDs.length; i++) { String currentCacheID = allCacheIDs[i]; ClearAction clearAction = new ClearAction(currentCacheID); try { cacheManager.applyAction(currentCacheID, clearAction, true); } catch (ApplyActionException e) { e.printStackTrace(); } }
The following example shows how to clear a single cache by performing a predefined action (ClearAction). To do this, you need to specify the cache ID.
Example
String cacheID = “myCacheId”; boolean cluster = true; CacheAction cacheAction = new ClearAction(cacheID); try { cacheManager.applyAction(cacheID, cacheAction, cluster); } catch (ApplyActionException e) { e.printStackTrace(); }
The Cache Management API enables you to access specific actions, implemented by a cache. The following example shows how to define and perform such action:
Example
String cacheID = “newCacheId”; HashMap actionParams = new HashMap(); actionParams.put("key1", "value1"); actionParams.put("key2", "value2"); //Define the action CacheAction MyDummyAction = new CacheAction(cacheID, "dummyActionId", actionParams); try { //Perform the action cacheManager.applyAction(cacheID, MyDummyAction, cluster); } catch (ApplyActionException e) { e.printStackTrace(); }