Saturday, March 10, 2012

Implementing websphere dynacache example

If you are here, I assume that you already know what Websphere DynaCache is and you are looking for some code/instructions on how to implement it.

This example demonstrates how to store and Retireve an Object  in websphere DynaCache.


Dyna Cache service is enabled by Default for Websphere Portal 7 .

1. Create an Object Cache instance in WAS:

    a) Log in to WebSphere Application Server Admin console
    b) Navigate to Resources --- Cache instances --- Object cache instances  as shown below





c) Click New and enter the configuration details as show below and save the changes and restart the server.




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

we  have to lookup the service to get a handle on the cache object.
since look is costly it is recommanded to do the look in the init method if your portlet.
 as shown below

private DistributedMap appCache = null;
public void init() throws PortletException {
        super.init();
   

InitialContext ctx;

        try {
            ctx = new InitialContext();
            appCache = (DistributedMap) ctx.lookup("services/cache/TestCache");
        } catch (NamingException e) {
            // TODO need to change the exception handling...
            e.printStackTrace();
        }

}


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

and you can store the information into the cache by calling
appCache.put(object arg0, object arg 1);

if you are trying to store a  result set from a database or a result from a Webserivce 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.






4 comments:

  1. Very helpful post! Thanks!

    ReplyDelete
  2. There exists a copy of this article:
    http://www.webportalclub.com/2013/07/steps-for-implementing-dyna-cache-in.html

    ReplyDelete
  3. how to set the expiry time for dynacache?

    ReplyDelete
  4. Very interesting,good job and thanks for sharing such a good blog. Your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up . If You can create your own fake instagram post using fake instagram chat generator and Prank friends. So read this profilefake instagram chat generator.

    ReplyDelete

comments