public class CacheHelper extends BasicSectionHelper
{{#cache}}
All the content will be cached!
{{/cache}}
A key
might be used to cache multiple versions of the fragment
for the same part of the template, i.e. each version depends on some context
(e.g. logged-in user):
{{#cache key=username}}
All the content will be cached!
{{/cache}}
The cached fragments can be automatically updated. Either set the expiration
or the guard. expire
value must be a Long value.
unit
must be a TimeUnit
constant or its
Object.toString()
( TimeUnit.MILLISECONDS
is the default).
guard
may be any object - every time the helper is executed the
current value of the Object.toString()
is compared to the
Object.toString()
value of the guard referenced during the last
update of the fragment. If they're not equal the fragment is updated.
{{#cache key=username expire=2 unit="HOURS" guard=roles}}
All the content will be cached!
{{/cache}}
It's also possible to invalidate the cache manually (e.g. after user logout).
See also invalidateFragments()
and
invalidateFragments(String)
.
To limit the size of the fragment cache use
FRAGMENT_CACHE_MAX_SIZE_KEY
.
Modifier and Type | Field and Description |
---|---|
static ConfigurationKey |
FRAGMENT_CACHE_MAX_SIZE_KEY
Limit the size of the fragment cache.
|
SECTION_TYPES
HELPER_TAG_TYPES
configuration
Constructor and Description |
---|
CacheHelper() |
Modifier and Type | Method and Description |
---|---|
void |
execute(Options options)
Execute the helper.
|
Set<ConfigurationKey> |
getConfigurationKeys() |
protected Set<String> |
getSupportedHashKeys() |
void |
init()
Can be overridden so that there's no need to call
super.init(Configuration) . |
void |
invalidateFragments()
Invalidate all the cache fragments.
|
void |
invalidateFragments(String keyPart)
Invalidate the cache fragments whose key contains the given part of the
key.
|
protected int |
numberOfRequiredParameters() |
void |
validate(HelperDefinition definition)
Helper should validate the tag definition (e.g. number of parameters)
during compilation and fail fast if necessary.
|
allowedTagTypes
getRequiredHashKeys, numberOfRequiredHashEntries
append, convertAndAppend, convertValue, isSection, isUnescapeVariable, isVariable
checkNotInitialized, init
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
init
public static final ConfigurationKey FRAGMENT_CACHE_MAX_SIZE_KEY
public void execute(Options options)
Helper
public void init()
AbstractConfigurationAware
super.init(Configuration)
.init
in class AbstractHelper
public Set<ConfigurationKey> getConfigurationKeys()
protected int numberOfRequiredParameters()
numberOfRequiredParameters
in class BasicHelper
public void validate(HelperDefinition definition)
Helper
MustacheException
with code
MustacheProblem.COMPILE_HELPER_VALIDATION_FAILURE
should be
thrown in case of validation failure occurs.validate
in interface Helper
validate
in class BasicHelper
public void invalidateFragments()
public void invalidateFragments(String keyPart)
keyPart
- protected Set<String> getSupportedHashKeys()
getSupportedHashKeys
in class BasicHelper
*
to
allow any hash keysCopyright © 2017. All rights reserved.