Purging Element Caches with the Java SDK

The other day I had a requirement where I needed to give a single user access to clear Element Caches.  He didn’t have a Desktop license, but the permission to access the Caches isn’t tied to Desktop.  With a quick program written in Java using the MicroStrategy Java SDK, I was able to deploy a quick app to the user to execute a cache clear on their own.

Permissions
The permissions required to clear the caches aren’t tied to any license types.  They are under the Administration section of the Security Role, and they should be: Administer Caches, Configure Caches and Monitor Caches.  Add these as a new Security Role or directly to a new Group, and then add your target user to that group.

Java Code
I think this code is derived from a MicroStrategy Sample, though I can’t find it now to give it proper credit. In any case, I’ve adjusted the code for my purposes to purge the Element Caches, though if you’re interested in purging more caches, they should be simple modifications.

For background information on setting up your Environment to run this code, check out the post Getting Started with the Java Web SDK.


package Cache;

import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.admin.WebObjectsAdminException;
import com.microstrategy.web.objects.admin.monitors.CacheResults;
import com.microstrategy.web.objects.admin.monitors.CacheSource;
import com.microstrategy.web.objects.admin.monitors.Caches;
import com.microstrategy.web.objects.admin.monitors.EnumWebMonitorType;
import com.microstrategy.web.objects.admin.monitors.JobSource;
import com.microstrategy.webapi.EnumDSSXMLLevelFlags;
import com.microstrategy.webapi.EnumDSSXMLPurgeFlag;

public class ClearCache {

private static WebObjectsFactory factory = null;
private static WebIServerSession serverSession = null;

public static void main(String[] args) throws WebObjectsException, IllegalArgumentException {
getSession();
// create a WebObjectsFactory instance
//WebObjectsFactory factory = WebObjectsFactory.getInstance();

// Get the cache source object
CacheSource source = (CacheSource) factory.getMonitorSource(EnumWebMonitorType.WebMonitorTypeCache);

// Set up the flag to obtain browsing information
source.setLevel(EnumDSSXMLLevelFlags.DssXmlBrowsingLevel);

// Since we only need to get the project DSSID in order to purge its object cache,
// so we set the incremental fetch setting to the bare minimum
source.setBlockBegin(1);
source.setBlockCount(1);

try {
     // Sends the request to Intelligence Server to retrieve cache information
     CacheResults results = source.getCaches();
     System.out.println("Connected");
     // Cache are grouped in project level, so we loop through the caches collection
     // to obtain their project DSSID and then purge the object cache immediately
     for (int i = 0; i < results.size(); i++) {
          // Obtain the project DSSID of each caches collection
          Caches projectLevelCaches = results.get(i);
          String projectDSSID = projectLevelCaches.getProjectDSSID();
          if (projectDSSID.equals("0114E056402ECFF8D895DAB005972880") || projectDSSID.equals("3BA2B0C848B17C4C96C18C8FD390237E")) //Dev and Prod
          {
          // Purge the element cache of each project
          source.purgeCache(projectDSSID, EnumDSSXMLPurgeFlag.DssXmlPurgeElementCache);
          System.out.println("Purged Cache for " + projectDSSID);
          }
     }
     System.out.println("Done");
     serverSession.closeSession();
} catch (WebObjectsAdminException woae) {
         woae.printStackTrace();
}
}

public static void getSession() {
System.out.println("Connecting to MicroStrategy");
factory = WebObjectsFactory.getInstance();
serverSession = factory.getIServerSession();
serverSession.setServerName("ISERVER");
serverSession.setServerPort(0);
serverSession.setProjectName("PROJECT"); //just a project to connect to, not the cache we're going to clear (see above for projects to clear)
serverSession.setLogin("USER");
serverSession.setPassword("PASSWORD");

try {
serverSession.getSessionID();
} catch (WebObjectsException ex) {
System.out.println("Error: " + ex.getMessage());
}
}

}

Conclusion
Obviously if the user has Desktop access, the permissions outlined at the start could be used to access the regular Cache monitor.  And if you need to automate cache clearing, Command Manager is a better option that writing all of this code.  However, there was a niche for me for this code, and maybe at the very least it’ll serve as an example if you’re interested in related Java SDK development.

You may also like...