DynaCache scenarios and implementation

I have seen so many queries where people are looking for solution to share object between different portlet applicataions.I just thought of writing this post for detailed solution for these kind of use cases.
So friends, If you have custom object and you want to share that object across any portlet application deployed on your portal environment,
DynaCache is the place where you can store your object and get the object where ever required back from DynaCache.

DynaCache Scenarios
scenarios i have worked with was
In my custom login portlet, i was required to store custom object at the time lo login and the same object should be available to all portlet applications deployed on portal.

For this, In my login portlet i stored the object into DynaCache and get the object from DynaCache where ever required.


There could be other scenarios as well, where you want to Cache data in JSR 168 portlets, Share information between IBM portlets and JSR 168 portlets
 

Technically, WebSphere Application Server provides the DistributedMap interface as a simple interface for the dynacache.

DynaCache implementation

First, you need to enable global dynacache service in the WebSphere Application Server administrative console.
By default, this service is enabled but in case if it's disabled, follow the steps below to enable it

1. Open the administrative console.
2. Select Servers => Application Servers in the administrative console navigation tree.
3. Click the WebSphere Portal server.
4. Select Additional Properties => Dynamic Cache Service.
5. In the Startup state field, select Enable service at server startup.
6. Click Apply or OK.
7. Restart WebSphere Application Server and WebSphere Portal Server.

Registering and configuring a new dynacache

Next, you need to register and configure a new dynacache instance for use by the portlet service

1. Open the administrative console.
2. Select Resources => Cache instance = > Object cache instances.
3. click New.
4. provide values to mandatory fields.

(I am giving JNDI values as services/cache/MyCustomObjectCache )

5. Click Ok and then Save.

Accessing/lookup of DynaCache service

Next, we will access/lookup DyanaCache Service. In portlets, where ever you are storing or getting the object to/from dyanacahe, you will have to lookup the service first and then you can store or get the object to/from dynacache.
Best paractice would be to lookup this service at the time of initialization.(init method of portlet)

//declare a class level variable
private DistributedMap distributedDynaCacheMap = null;

public void init(PortletConfig portletConfig) throws PortletException {

super.init(portletConfig);
Context context;

try {

context = new InitialContext();
distributedDynaCacheMap = (DistributedMap)context.lookup("services/cache/MyCustomObjectCache");

} catch (NamingException e) {

throw new PortletException();
}

Now, you have got your dynacache service lookedup...

To store object into dynacahe, simply call put method.

distributedDynaCacheMap.put(Object arg0, Object arg1);

To get object from dynacache in any of portlet, first you need to access/lookup the service and just call get method on distributedDynaCacheMap.

distributedDynaCacheMap.get(object argo);

10 comments:

John Mackey said...

Great article! Thanks for posting it.

-John

Neeraj Sidhaye said...

Thanks for your comment John.

Anoop said...

hey great article

Anoop said...

Neeraj, we have a custom tai in place so can we invoke the dynacache service in tai instead os the custom login portlet as you mentioned in your use case.

Sushant said...

Your article gives the consolidated quick-steps to directly implement and test a DynaCache scenario.
Just what the doctor ordered for me...
Great Work!

Anonymous said...

Great article...Cleared the concept of DynaCache

Nisu said...

can we implement DynaCache for property file ? I want to store some property file in memory and when date is change it should re load in memory.

Nisu said...

Can we implement DynaCache for property file ? I want to load Property file in Memory and when date change it should reload again.

Thoughts said...

Very helpful article in simple explanation.

Neeraj Sidhaye said...

@Nishu - Apologies for late reply. I hope by now you have already figured out way!
however, I would suggest to go for standard caching techniques like Ehcache ( example), and move your property config to DB. Easy to manage in DB!
steps -
1) have your property configuration in DB
2) use Ehache or any other caching tool to cache your configuration in memory
3) Refresh cache every night or depends on your req. This refresh is just a configuration part of your cache tool.
4) For any change in configuration, just run DML and with next cache refresh you would have updated values in cache.

hoe this helps.