Steps for implementing Dyna Cache in WebSphere


To keep it simple Dyna Cache is nothing more than a glorified distributed map. It is maintained by server and made available to all the applications installed on that particular server. Following example demonstrates how to store and retrieve an object from WebSphere Dyna Cache.

Note: Dyna Cache service is enabled by default on WebSphere Portal.

Create an Object Cache instance in WAS
  1. Go to WAS admin console.
  2. Navigate to Resources -> Cache Instances -> Object Cache Instances.
  3. Select the scope and Click “New”.
  4. Give a name to the new entry for e.g: TestCache.
  5. For JNDI name, enter “services/cache/TestCache”.
  6. Click “Apply” and the save the configuration.
  WAS Console

Referring the Dyna Cache Instance
  1. The resource reference is added to the portal project web.xml and then it can be looked up by using java:comp namespace.
  2. If you are using RAD, by default when you open the web.xml should open in easy to use UI, before RAD 8 all the tabs separated by category. From RAD 8 it is combined into one.
  3. Select References from tabs and add the following to the fields respectively
Resource reference name : map/TestCache
Resource type:
Authentication : Container
Sharing scope: Shareable

After saving the file, you are good to use the Dyna cache instances in your portlet by referring the namespace.

Referring the Dyna Cache Instance from Code

Follow the below steps to access the cache to store/retrieve the data to/from the object Cache.

We have to look up the service to get a handle on the cache object. Since look is costly it is recommended to do the look in the init() method if your portlet as shown below
private DistributedMap cacheObject = null;
public void init() throws PortletException {

InitialContext ctx;
try {
ctx = new InitialContext();
      cacheObject = (DistributedMap) ctx.lookup("services/cache/TestCache");
} catch (NamingException e) {
} }

Now that we have a handle on the cache object we can retrieve the data from cache by calling the get method on the object as shown below
cacheObject .get(Key);

and similarly we can store the information into the cache object by calling
cacheObject .put(object arg0, object arg1);

Note : If you are trying to store a result set from a database or a result from a Web Service call, make sure you do not update the cache directly. Save the result to a temp object and update the cache object with the value in the temp if the temp is not null or invalid value, this way you will not be saving the cache with null if the database or webservice is down.


Sravana Kumar Chikka said...

we can also called as object cacheeeeeeeeeeeeeeeeeee

mani said...

How can i timeout the cache for dynacache?

Post a Comment

Recent Posts

Popular Posts

© 2011-2019 Web Portal Club