Java Reference
In-Depth Information
We looked at the shopping cart category in the previous chapter, so we will use
it again here. When visitors to the shopping cart select a category, it usually has
related subcategories that are also displayed. These categories are accessed quite a
bit, and do not change often; consequently, they are prime candidates for long-
term, read-only caching.
When thinking through how to set up category caching, consider how the
results will be queried and the caching strategy that best fits the pattern of access
to that data. In the case of caching subcategory lists, users will typically query for a
list of child categories based on their related parent category. Expressing this in
SQL terms would mean that the WHERE criteria will be based on equality of the
parentCategoryId with a passed-in parameter. Another consideration is how often
this cache should be flushed and which caching strategy should be used. Often
users interact with some categories more than others. So, surveying the options
we may want to go with a LRU strategy (listing 9.8). Using this approach will keep
items around that are accessed more recently while discarding those that have
been in the cache longer.
Listing 9.8
Sample LRU cacheModel
<cacheModel id="categoryCache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insert"/>
<flushOnExecute statement="update"/>
<flushOnExecute statement="delete"/>
<property name="size" value="50"/>
</cacheModel>
In listing 9.8 we start by setting up the cache model. We specify the type attribute
as LRU . The id attribute provides us with a unique identifier to reference when
setting up query mapped statements that will use the cache. By using the <flush-
Interval> tag, we ensure that the cache is never older than 24 hours. <flush-
Interval> will clear any cached results that are stored in the identified
categoryCache . Using <flushInterval> we specify that calling the identified
insert, update, or delete mapped statement will also trigger a flush of any results
stored in the identified categoryCache . Finally, we set the limit for how many
items will be stored in the cache by using the <property> tag and the <size>
property. Once the cache exceeds 50 stored results, it will begin to remove the
least recently used items.
Search WWH ::




Custom Search