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}"
disabled="#{facesContext.application.projectStage eq 'Development'}">
<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 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;
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 io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.cache.CacheProvider;
import org.primefaces.component.tabview.Tab;
@RegisterForReflection
public class 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);
UIComponent tabs = UIComponent.getCurrentComponent(facesContext).getFacet("static-tabs");
if (tabs != null) {
attach(tabs, srcContent);
}
files = new ArrayList<>();
assert srcContent != null;
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);
}
}