Java Reference
In-Depth Information
public Object getObject(CacheModel cacheModel,
Object key);
public Object removeObject(CacheModel cacheModel,
Object key);
public void flush(CacheModel cacheModel);
}
The next few sections will take you through an example of implementing a cache.
This is not meant to teach you how to write an enterprise capable cache controller.
We're going to employ a simple caching approach using a Map. A more common
case would be to plug in a third-party cache that has advanced features. We do not
recommend that you make the following example your caching strategy of choice.
12.3.1
Creating a CacheController
The CacheController implementation starts out with configuration. Configuration
is achieved by implementing the configure() method; this method takes a Java
Properties instance, which can contain relevant configuration information. For
our cache, we don't need any configuration properties, but we will need the map
in which to store our objects. Here's a start to implementing our CacheController :
public class MapCacheController {
private Map cache = new HashMap();
public void configure(Properties props) {
// There is no configuration necessary, and therefore
// this cache will depend upon external
// flush policies (e.g. time interval)
}
// other methods implied …
}
OK, now that we have a skeletal cache model, let's build the rest of it.
12.3.2
Putting, getting, and flushing a CacheController
At this point, we can start to think about adding objects to the cache. The iBATIS
CacheModel manages all of the keys and determines how to distinguish various
statement calls and result sets. So to put an object on the cache, all you need to do
is pass the key and the object to your cache implementation of choice.
For example, here are the put, get, and remove methods:
public void putObject(CacheModel cacheModel, Object key,
Object object) {
cache.put (key, object);
Search WWH ::




Custom Search