Cache reduces the page load time: it stores the rendered output of its content in a region of a cache provider (e.g. Ehcache) after the initial rendering and reuses it afterwards. The showcase uses it for the source code tabs of every page, because parsing an XHTML file takes time and the output does not change while the application runs.
<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>
package org.primefaces.showcase.util;
import org.primefaces.cache.CacheProvider;
import org.primefaces.component.tabview.Tab;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.faces.application.ProjectStage;
import jakarta.faces.component.UIComponent;
import jakarta.faces.component.UIPanel;
import jakarta.faces.context.FacesContext;
public class ShowcaseUtil {
private static final Logger LOGGER = Logger.getLogger(ShowcaseUtil.class.getName());
private ShowcaseUtil() {
}
public static 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);
if (srcContent == null) {
LOGGER.severe("File " + fullPath + " not found");
}
else {
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 Object getPropertyValueViaReflection(Object o, String field)
throws ReflectiveOperationException, IllegalArgumentException, IntrospectionException {
return new PropertyDescriptor(field, o.getClass()).getReadMethod().invoke(o);
}
}