Skip navigation.
Downloads | Product Documentation | Support

Application Developer's Guide

# Using the Cache Purging APIs

This section describes how to purge the cache for a query using the cache EJB API. It contains the following sections:

- The com.bea.ldi.cache.ejb Package
- Security Issues When Using the Cache APIs
- Writing Java Code to Purge Cache Entries

For details on configuring the Liquid Data results cache, see Configuring the Results Cache in the Administration Guide.

# The com.bea.ldi.cache.ejb Package

The `com.bea.ldi.cache.ejb` package has a `CacheRemote` interface with methods you can use to purge cache entries for queries. There is a `purgeCache()` method to purge the entire cache, and there are methods to purge entries for a specified query. For details on the APIs, see the Javadoc.

# Security Issues When Using the Cache APIs

If security is enabled in your Liquid Data domain, then the user who makes the API call to purge the cache must have the necessary permissions to purge the cache. If the user does not have the needed permissions, the purge will fail. For details about setting a security policy for purging the cache, see Configuring a Security Policy for Purging the Cache Results in the Liquid Data Administration Guide.

# Writing Java Code to Purge Cache Entries

You can write Java code to purge the cache entries for a query. There are methods to purge the entire cache, purge all the entries for a specified query, and to purge the instances of a query with specified parameters. This section describes the basic steps required to write Java code to purge cache entries in the results cache, including a sample program, and includes the following subsections:

- Enable Caching in Liquid Data
- Import the Liquid Data Packages
- Lookup the EJB Home in the JNDI Tree
- Sample Cache Purging Code

## Enable Caching in Liquid Data

In order for the Liquid Data cache APIs to purge any cache entries, you must configure and enable the results cache in Liquid Data. Setting up and enabling the cache involves the following general steps:

- Creating the cache database
- Creating the cache `LDCacacheDS` data source
- Enabling Results Caching in Liquid Data
- Setting cache policies for individual queries

For details on setting up the Liquid Data results cache, see Configuring the Results Cache in the Administration Guide.

## Import the Liquid Data Packages

Any program that uses the Liquid Data cache APIs must import the Liquid Data packages. Depending on which APIs the program uses, you should have import statements similar to the following:

>     //import the Liquid Data cache and QueryParameters Packages
>     import com.bea.ldi.cache.ejb.CacheRemote;
>     import com.bea.ldi.cache.ejb.CacheRemoteHome;
>     import com.bea.ldi.server.common.QueryParameters;

## Lookup the EJB Home in the JNDI Tree

A program that purges a result cache entry must find the JNDI entry for the Cache EJB. The Cache EJP has the following JNDI name:

>     bea.ldi.cache.CacheHome

If the JNDI name does not exist in the JNDI tree, then the EJB is not properly deployed and the cache APIs will not be available.

The following code shows a sample JNDI lookup.

>     //Declare JNDI_NAME variable
>     private String JNDI_NAME="bea.ldi.cache.CacheHome";

>     // Lookup the EJBs home in the JNDI tree
>        private CacheRemoteHome lookupHome()
>         throws NamingException
>       {
>         // Lookup the beans home using JNDI
>         Context ctx = getInitialContext();

>         try {
>           return (CacheRemoteHome)ctx.lookup(JNDI_NAME);
>          } catch (NamingException ne) {
>            ne.printStackTrace();
>           log("The client was unable to lookup the EJBHome.  Please make sure ");
>           log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+
>               " on the WebLogic server at "+url);
>           throw ne;
>         }
>       }

## Sample Cache Purging Code

The following sample program uses the `purgeCache` method to purge the Liquid Data query cache.

>     package myFolder;

>     //import the Liquid Data cache and QueryParameters Packages
>     import com.bea.ldi.cache.ejb.CacheRemote;
>     import com.bea.ldi.cache.ejb.CacheRemoteHome;
>     import com.bea.ldi.server.common.QueryParameters;

>     import javax.naming.Context;
>     import javax.naming.InitialContext;
>     import javax.naming.NamingException;
>     import java.util.Properties;

>     public class QueryPurgeClient {
>        private String url=null;
>        private String JNDI_NAME="bea.ldi.cache.CacheHome";
>        private String cacheQueryName=null;
>        private QueryParameters qparams = null;
>        private static CacheRemoteHome home=null;

>      //  public static boolean stop=false;

>        /* normally you would pass the argument for the parameter. But in this
>         * example we are hardcoding the stream
>        */
>        public QueryPurgeClient(String url, String queryName,
>                                QueryParameters qparams){
>         this.url= url;
>         this.cacheQueryName=queryName;
>         this.qparams = qparams;

>        }

>        public static void main(String[] args) throws Exception  {
>         // ignore query parameters for now
>         QueryPurgeClient qpc = new QueryPurgeClient(args[0], args[1],null);
>         qpc.runPurgeCache(args[1]);
>        }

>       // Create a method to purge cache entries for a specified query
>       // This method uses com.bea.ldi.cache.ejb.CacheRemote.purgeCache(queryName)
>       //
>        public void runPurgeCache(String queryName) throws Exception{

>           try{
>                if(home==null)
>                   home = lookupHome();
>              // log("Creating a query client");
>               CacheRemote cacheQuery = (CacheRemote) home.create();
>               cacheQuery.purgeCache(queryName);
>               log("Purged all cached instances of: >>>>>>>\n" + cacheQueryName);
>           } catch(Exception e){
>               e.printStackTrace();
>               throw e;
>           }
>        }

>     // Lookup the EJBs home in the JNDI tree
>       private CacheRemoteHome lookupHome()
>         throws NamingException
>       {
>         // Lookup the beans home using JNDI
>         Context ctx = getInitialContext();

>         try {
>           return (CacheRemoteHome)ctx.lookup(JNDI_NAME);
>          } catch (NamingException ne) {
>            ne.printStackTrace();
>           log("The client was unable to lookup the EJBHome.  Please make sure ");
>           log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+
>               " on the WebLogic server at "+url);
>           throw ne;
>         }
>       }

>     // Get the context for WebLogic Server, to make sure it is running
>       private Context getInitialContext() throws NamingException {

>         try {
>           // Get an InitialContext
>           Properties h = new Properties();
>           h.put(Context.INITIAL_CONTEXT_FACTORY,
>             "weblogic.jndi.WLInitialContextFactory");
>           h.put(Context.PROVIDER_URL, url);
>           return new InitialContext(h);
>         } catch (NamingException ne) {
>           ne.printStackTrace();
>           log("We were unable to get a connection to the WebLogic server at "+url);
>           log("Please make sure that the server is running.");
>           throw ne;
>         }
>       }

>       private static void log(String s) {
>         System.out.println(s);
>       }
>     }