Web cache (or caching) is one of the mechanism used for
tuning the web sites performances, especially focused to increase the speed of
feeding the browser with HTML code, images, etc. This will decrease bandwidth
usage, server load, and perceived lag. The idea behind basic caching consist in
temporary storing copies of data (pages, documents, images) and, at subsequent
request, serve these data from this location instead of hitting the application
business logic that initially provided the data. Some significant cases consist
in static data that comes from a database, large images, data used by multiple
users, keyword searching, auto-completion, etc.
Speaking from JSF angle, caching is handled by the resource
handler (ResourceHandler).
By using the JSF tags, like <h:outputStylesheet> instead of <link>,
<h:outputScript>
instead of <script>
and <h:graphicImage>
instead of
<img>, you will allow ResourceHandler to use its default
caching mechanism (e.g. images will be cached from 1 week - 604800 seconds).
Now, is important to notice that we are speaking about browser cache, not
server-side cache. The default caching period can be altered via a context
parameters. Per example, in Mojarra implementation this can be accomplished
like below:
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>time in seconds</param-value>
</context-param>
Another
context parameter specific to Mojarra, named com.sun.faces.resourceUpdateCheckPeriod
(default 5
minutes), allows us to indicate the frequency to check for changes in web
application artifacts that contain resources. If a change is detected, the
cache will be cleared and rebuilt. If the value of this option is -1,
the cache will never be cleared and new resources will not be picked up.
By the other
hand, MyFaces implementation supports several cache zones as follows:
·
Expression Language (EL) Cache
From version
2.0.8, MyFaces implementation allows a cache mechanism for EL expressions. This
can be configured using the below context parameter:
<context-param>
<param-name>org.apache.myfaces.CACHE_EL_EXPRESSIONS</param-name>
<param-value>one of the below value</param-value>
</context-param>
The
supported values are (the below snippet is copied from MyFaces official
documentation):
always: Only does not cache
when expressions are inside user tags or the expression contains a variable
resolved using VariableMapper
allowCset: Like always,
but does not allow cache when <ui:param> was used on the current
template context
strict: Like allowCset,
but does not allow cache when <c:set> with var and value
properties only is used on the current page context
noCache: All expression are
created each time the view is built
·
Resources Cache
From version
2.0.2, MyFaces provides org.apache.myfaces.RESOURCE_HANDLER_CACHE_ENABLED, which enables or disables the cache used to
"remember" if a resource handled by the default ResourceHandler
exists or not (default true), and org.apache.myfaces.RESOURCE_HANDLER_CACHE_SIZE,
which controls the size of the cache used to check if a resource exists or not
(default 500).
·
View Handling Cache
From version
2.0.2, MyFaces enables/disables cache via org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED
context parameter flag (default true), and set the size of this cache via org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE
context parameter (default 500). This cache is used to "remember" if
a view exists or not and reduce the impact of successive calls to ExternalContext.getResource()
method.
In addition, from version 2.0.13, MyFaces enable
or disable a cache used to "remember" the generated facelets unique
ids and reduce the impact over memory usage via org.apache.myfaces.VIEW_UNIQUE_IDS_CACHE_ENABLED
(default false).
MyFaces allows us to configure the size of cache used to store strings
generated using SectionUniqueIdCounter
for component ids, via org.apache.myfaces.COMPONENT_UNIQUE_IDS_CACHE_SIZE(default
100).
·
State Saving (state) Cache
From older
versions of MyFaces (1.1), we have org.apache.myfaces.SECRET.CACHE context
parameter - if is set to false, the secret key used for encryption
algorithm is not cached - and, org.apache.myfaces.MAC_SECRET.CACHE - if is
set to false,
the secret key used for MAC algorithm is not cached.
Of course,
let's not forget the org.apache.myfaces.CACHE_OLD_VIEWS_IN_SESSION_MODE
context parameter. It defines the way of handle old view references(views
removed from session), making possible to store it in a cache, so the state
manager first try to get the view from the session. By default, is off,
and supports the following values: off, no, hard-soft, soft,
soft-weak,
weak.
Beside these
context parameters, browsers provide internal wizards, add-ons, plug-ins etc
that can be used to control the how the cache is done. HTML meta-tags, like CACHE-CONTROL,
PRAGMA
NO-CACHE, EXPIRES and LAST-MODIFIED are also useful. The JSF
resource handler sets the EXPIRES and LAST-MODIFIED, and usually
all we have to set is the CACHE-CONTROL.
As a final
note, do not forget about the JSF versioning system, which is based on the
resources folder and libraries. By respecting JSF conventions, you can instruct
JSF to load your latest resources very easily.
For server-side cache, check the OmniFaces Cache component. Moreover, you can check Test OmniFaces Cache for Render Response Phase.
Niciun comentariu :
Trimiteți un comentariu