Recommended to read before: Caching
via PrimeFaces and EHCache (register MBeans in JConsole via OmniFaces @Eager)
PrimeFaces supports two different providers of cache implementation: EHCache and Hazelcast. In this post, we will take a look on the Hazelcast provider
based on the same scenario described in the Caching
via PrimeFaces and EHCache (register MBeans in JConsole via OmniFaces @Eager).
So, instead of re-rendering this static table, we better cache it. In
order to accomplish that via PrimeFaces and Hazelcast, we need to follow these
steps:
1.Configure the cache provider in web.xml via the primefaces.CACHE_PROVIDER context parameter:
<context-param>
<param-name>primefaces.CACHE_PROVIDER</param-name>
<param-value>org.primefaces.cache.HazelcastCacheProvider</param-value>
</context-param>
2. Use the <p:cache/>
tag to point out the content that should be cached. As you can see in the official
documentation this tag support a bunch of optional attributes. We are
especially interested in the region
and key attributes. The region attribute allows us to point to the cache
region that we want to use, which is myCache in our case. The key attribute represents an unique id of the
cache entry in region
and defaults to client id of component. We set it as myTable. Of course, this means that we
can use <p:cache/>
with different regions and keys:
<p:cache region="myCache"
key="myTable">
<p:dataTable var="t"
value="#{playersBean.data}">
<p:column
headerText="Player">
<h:panelGroup
id="playerId">#{t.player}</h:panelGroup>
</p:column>
<p:column headerText="Age">
<h:panelGroup
id="ageId">#{t.age}</h:panelGroup>
</p:column>
<p:column
headerText="Birthplace">
<h:panelGroup
id="birthplaceId">#{t.birthplace}</h:panelGroup>
</p:column>
<p:column
headerText="Residence">
<h:panelGroup
id="residenceId">#{t.residence}</h:panelGroup>
</p:column>
<p:column
headerText="Height">
<h:panelGroup id="heightId">#{t.height}
cm</h:panelGroup>
</p:column>
<p:column
headerText="Weight">
<h:panelGroup
id="weightId">#{t.weight} kg</h:panelGroup>
</p:column>
</p:dataTable>
</p:cache>
Done! If you run the application at this point then everything should
work as expected. You will notice that initial request take some time to load,
while postbacks will work very fast. This is a sign that, at postbacks, the
table markup comes from cache. Moreover, you can see that Hazelcast is at work:
But, let's add some code to check the cache content. First, let's
display the cache content in page:
Cache content:
<hr/>
<p>
<code>
#{playersBean.showCache()}
</code>
</p>
<hr/>
public Object
showCache() {
HazelcastCacheProvider cacheProvider =
(HazelcastCacheProvider)
RequestContext.getCurrentInstance().getApplicationContext().getCacheProvider();
return cacheProvider.get("myCache",
"myTable");
}
We also may want to programmatically remove our entry from cache. For
this we can simply use an action method as below:
public void
clearCache() {
HazelcastCacheProvider cacheProvider = (HazelcastCacheProvider)
RequestContext.getCurrentInstance().getApplicationContext().getCacheProvider();
cacheProvider.remove("myCache",
"myTable");
}
At initial request there is nothing in the cache:
At postback (click the Refresh
button) the cache content shows the HTML markup corresponding to our data
table:
Further, if you click the Clear Cache button, the cache entry will be removed.
The complete application is available here.
Niciun comentariu :
Trimiteți un comentariu