Background documentationManaging Portal Caches Locate this document in the navigation structure

 

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 Note

The Cache Management API is currently supported by the navigation and OBN caches.

End of the note.
Using the Cache Management API

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 Syntax

  1. ICacheManager cacheManager = (ICacheManager); 
    PortalRuntime.getRuntimeResources().getService(ICacheManager.KEY);
End of the code.

With the Cache Management API, you can perform the following tasks:

Enabling or Disabling All Caches

The following example shows how to enable or disable all available caches:

Example Example

  1. String[] enableFailed = cacheManager.setEnabled(true);
    String[] disableFailed = cacheManager.setEnabled(false);
    
End of the code.
Performing Batch Actions on All Caches

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 Example

  1. 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();
    			}
    		}
    
End of the code.
Clearing a Cache Using a Predefined Clear Action

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 Example

  1. String cacheID = “myCacheId”;
    boolean cluster = true;
    CacheAction cacheAction = new ClearAction(cacheID);
    try {
         cacheManager.applyAction(cacheID, cacheAction, cluster);
    			} 
    catch (ApplyActionException e) {
         e.printStackTrace();
    			}
    
End of the code.
Defining and Performing a Cache-Specific Action

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 Example

  1. 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();
    			}
    
End of the code.