22.2. Page fragment caching
The
<s:cache>
tag is Seam's solution to the problem of page fragment caching in JSF. <s:cache>
uses pojoCache
internally, so you will need to follow the previous steps — place the JAR
s in the EAR
and edit additional configuration options — before you can use it.
<s:cache>
stores some rendered content that is rarely updated. For example, the welcome page of our blog displays 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
lets you store multiple versions of each page fragment. In this case, there is one cached version per blog. The region
determines the cache or region node where all versions are stored. Different nodes may have differing expiry policies.
The
<s:cache>
cannot tell when the underlying data is updated, so you will need to manually remove the cached fragment when a change occurs:
public void post() { ... entityManager.persist(blogEntry); cacheProvider.remove("welcomePageFragments", "recentEntries-" + blog.getId()); }
If changes need not be immediately visible to the user, you can set up a short expiry period on the cache node.