Java Reference
In-Depth Information
}
public Object getObject(CacheModel cacheModel,
Object key) {
return cache.get(key);
}
public Object removeObject(CacheModel cacheModel,
Object key) {
return cache.remove(key);
}
Notice how each method also provides access to the
CacheModel
instance that is
controlling the cache. This allows you to access any properties from the
CacheModel
that you might need. The
key
parameter is an instance of
CacheKey
, a
special class within
iBATIS
that compares sets of parameters passed to a statement.
For the most part, you shouldn't have to manipulate it in any way. In the case of
putObject()
, the
object
parameter contains the instance or collection of objects
to cache.
The last method that
CacheModel
describes is the
flush()
method. This
method simply clears the entire cache.
public void flush(CacheModel cacheModel) {
cache.clear();
}
That is, in a nutshell, a complete
CacheController
implementation. Now we need
to learn how to use our
CacheController
.
12.3.3
Registering a CacheController for use
Like all other
iBATIS
configurations,
CacheModel
s and
CacheController
s are con-
figured within the
XML
configuration files. The easiest way to start using your
CacheModel
is to first declare a type alias for your new class. This will save you some
typing later.
<typeAlias alias="MapCacheController"
type="com.domain.package.MapCacheController"/>
Now that we've saved ourselves some keystrokes, we can apply the cache control-
ler type to a
<cacheModel>
definition, just like we do with any other cache model
type. For example:
<cacheModel id="PersonCache" type="
MapCacheController
" >
<flushInterval hours="24"/>
<flushOnExecute statement="updatePerson"/>
