Cache component is used to reduce page load time by caching the content in a global cache after the initial rendering. Various cache providers are supported like ehcache and hazelcast. Showcase use actually this component to render source examples for every PrimeFaces components because parsing an xhtml file takes time, and since the rendering won't change during the whole lifecycle of the application, it's better to cache the content.
<p:tabView var="entry" value="#{value}" touchable="false">
<p:tab title="#{entry.title}" >
<p:cache region="sources" key="#{entry}">
<pre class="line-numbers"><code class="language-#{entry.type}">
#{entry.value}
</code></pre>
</p:cache>
</p:tab>
<f:facet name="static-tabs">
<ui:insert />
</f:facet>
</p:tabView>
public class ShowcaseUtil {
private ShowcaseUtil() {
}
public static final List<FileContent> getFilesContent(String fullPath, Boolean readBeans) {
CacheProvider provider = CDI.current().select(ShowcaseCacheProvider.class).get().getCacheProvider();
List<FileContent> files = (List<FileContent>) provider.get("contents", fullPath);
if (files == null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
FileContent srcContent = getFileContent(fullPath, readBeans);
UIComponent tabs = UIComponent.getCurrentComponent(facesContext).getFacet("static-tabs");
if (tabs != null) {
attach(tabs, srcContent);
}
files = new ArrayList<>();
flatFileContent(srcContent, files);
if (facesContext.isProjectStage(ProjectStage.Production)) {
provider.put("contents", fullPath, files);
}
}
return files;
}
public static final Object getPropertyValueViaReflection(Object o, String field)
throws ReflectiveOperationException, IllegalArgumentException, IntrospectionException {
return new PropertyDescriptor(field, o.getClass()).getReadMethod().invoke(o);
}
}