18.2. Page fragment caching
The most interesting user of JBossCache is the <s:cache>
tag, Seam's solution to the problem of page fragment caching in JSF. <s:cache>
uses pojoCache
internally, so you need to follow the steps listed above before you can use it. (Put the jars in the EAR, wade through the scary configuration options, etc.)
<s:cache>
is used for caching some rendered content which changes rarely. For example, the welcome page of our blog displays the recent blog entries:
<s:cache key="recentEntries-#{blog.id}" region="welcomePageFragments">
<h:dataTable value="#{blog.recentEntries}" var="blogEntry">
<h:column>
<h3>#{blogEntry.title}</h3>
<div>
<s:formattedText value="#{blogEntry.body}"/>
</div>
</h:column>
</h:dataTable>
</s:cache>
The key
let's you have multiple cached versions of each page fragment. In this case, there is one cached version per blog. The region
determines the JBossCache node that all version will be stored in. Different nodes may have different expiry policies. (That's the stuff you set up using the aforementioned scary configuration options.)
Of course, the big problem with <s:cache>
is that it is too stupid to know when the underlying data changes (for example, when the blogger posts a new entry). So you need to evict the cached fragment manually:
public void post() {
...
entityManager.persist(blogEntry);
pojoCache.remove("welcomePageFragments", "recentEntries-" + blog.getId() );
}
Alternatively, if it is not critical that changes are immediately visible to the user, you could set a short expiry time on the JbossCache node.