Caching Mapped Statement Results
The results from a Query Mapped Statement can be cached simply by specifying the cacheModel parameter
in the statement tag (seen above). A cache model is a configured cache that is defined within your SQL
map. Cache models are configured using the cacheModel element as follows:
<cacheModel id="product-cache" type ="LRU" readOnly=”true” serialize=”false”>
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”cache-size” value=”1000” />
</cacheModel>
The cache model above will create an instance of a cache named “product-cache” that uses a Least Recently
Used (LRU) implementation. The value of the type attribute is either a fully qualified class name, or an
alias for one of the included implementations (see below). Based on the flush elements specified within the
cache model, this cache will be flushed every 24 hours. There can be only one flush interval element and it
can be set using hours, minutes, seconds or milliseconds. In addition the cache will be completely flushed
whenever the insertProduct, updateProduct, or deleteProduct mapped statements are executed. There can
be any number of “flush on execute” elements specified for a cache. Some cache implementations may
need additional properties, such as the ‘cache-size’ property demonstrated above. In the case of the LRU
cache, the size determines the number of entries to store in the cache. Once a cache model is configured,
you can specify the cache model to be used by a mapped statement, for example:
<select id=”getProductList” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>