Evolving the easy XPage keyword bean

The first part of the easy keyword bean can be found here

When working with keywords in XPages your application could greatly take advantage of the possibilities in the XPage Engine. Thru caching of data, What do I mean with caching?

Well today the keyword bean is located in viewScope and that means that it lives only on the current page that is opened. If you move the bean to applicationscope and add some caching functionality we only need to go into the database if the keyword never have been fetched before.

But there is a problem when moving things into applicationscope that is multi user access, and that can easily be solved using synchronization or threadsafe objects.

Another thing is that you need to maintain is keyword updates because they need to be maintained but that is also easy to solve because if you write the Keyword edit save using XPages that you can update the keyword when saving a keyword.

OK so how do we change the code so we get a common applicationscope keyword bean.

first we add a Global CacheObject and next we check against the GlobalCache Object before accessing the database. And we also need a recache option for a certain keyword to get it refreshed when you have saved a keyword.

Let’s Look at some code

we start by importing java.util.concurrent.ConcurrentHashMap and setting up the hasmap object as a cache object. I use the concurrenthashmap to get a threadsafe Cache object in my application scope bean.

We also need to change the object from static to be an ordinary Java object because it will live in the applicationscope. Another thing we also need to implement is serializable if the server want to store the object on disk for any reason.

Let’s move over to the getKeyword function. What we need to do is to check if the keyword is in cache and if it is, return the cached data. If it isn’t found get the data like before and store it in cache and return the data.

And because I call getKeword from the function getKeywordString I only need to change getKeyword when implementing this new functionality.

The last thing we need to add is the recache function, and that one will be a function that clears the data in cache so the next request to get the data will do it from the database.

I almost forgot, we need to change our managebean also and move if from viewScope to Application Scope in the faces-configt.xml file

If you want the full code for this check out this xSnippet on openntf.org

  1. Another improvement you may want to consider is having the keyword bean implement DataObject or Map<String, List> (with the former being much more straightforward but also XPage-specific). If you do that, then you can reference it in EL like #{FormHelpers.someKeyword}, which will call .getValue(“someKeyword”) (for DataObject) or .get(“someKeyword”) (for Map).

    • Fredrik Norling

      That would really give a much cleaner writing, if you like a more EL like writing of code.
      But if you come from LotusScript it might be more difficult to read because you might not know that
      these specific actions is performed for you in the background

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.