J F W

making web application programming easier is possible

HOW TO : CORE

caching data

Here is the code of a class used for cache data:

The MyCache is a singleton. That means that there is only one instance per virtual machine and the unique instance is returned using a static method of the class, the getInstance in this example.

Since the GenericCache class is extended, the methods initialize and getData have to be implemented.

In the initialize method the data to cache are got from a source. In our example we get the data doing a database query. Once the data has been obtained, it is saved in an attribute, the HashMap data  in this example.

The attribute saved in the previous method will be returned by the method getData. This method can take an optional parameter of any type. Also, the returned value is an Object instance, so any kind of object can be retuned.

What the Generic class offers, is an asynchronous mechanism to update the cached data. That mechanism is based on the Observer-Observable pattern. The observable class is the UpdateObservable. This class when invoked from the administration console of JFW sends an update signal to the observers. When the update signal  is received, the initialize method is executed; you must be careful on how you update the data attribute especially if your code has to be thread safe.