|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object freemarker.template.FileTemplateCache
public class FileTemplateCache
A TemplateCache
that loads templates from a filesystem. Given a
directory path, the cache assumes by default that all files in the directory
are templates. It can optionally be given a filename suffix for templates.
The default loading policy is FileTemplateCache.Strategy.LOAD_ON_DEMAND
:
templates are loaded into the cache only when requested,
each template's modification date is checked each time it is requested,
and the periodic updates are used only to remove deleted templates
from the cache. If the loading policy is set to FileTemplateCache.Strategy.PRELOAD
, all
templates are loaded when the loading policy is set, and all files are checked
during each periodic update. If template files will not be changed
frequently, use FileTemplateCache.Strategy.PRELOAD
with a long delay value for maximum
performance.
A combination of the two above is FileTemplateCache.Strategy.LOAD_ON_DEMAND_WITH_REFRESH
.
This loads a template on demand the first time it is requested. Subsequently,
it will do periodic refreshes on any templates that have been requested
from its cache. This saves memory if some templates are unlikely to be
needed, since they won't occupy memory until they are requested, while
improving performance over FileTemplateCache.Strategy.LOAD_ON_DEMAND
since it doesn't have
to check the file system every time a template is requested.
For maximum flexibility FileTemplateCache.Strategy.LOAD_AD_HOC
mode exists so that all
templates are loaded when the loading policy is set but files are
not refreshed periodically. Instead, one can write a client that will ask
the FileTemplateCache
to update a single template via the
Updateable.update(String)
method.
Applications with a large number of templates many of which are not
frequently updated will work well with FileTemplateCache.Strategy.LOAD_AD_HOC
mode.
The first argument to the getItem()
method is interpreted
as the template's path relative to the cache's root directory, using a
forward slash (/
) as a separator (this is to facilitate using URL
path info to request templates). For example, if a TemplateCache
object was made for the directory templates
, which contains a
subdirectory foo
, in which there is a template file called
index.html
, you would call getItem("foo/index.html")
to retrieve that template.
If a second argument is specified in a call to getItem()
,
this will determine the type of object to be retrieved. The types that
can be returned depend on the TemplateRegistry
. Three types of
object are registered with the TemplateRegistry
by default:
template
-- returns a Template
object. This is
the default type to be retrieved by the cache.unparsed
-- returns an UnparsedTemplate
object.
This behaves like other Template
objects, but its contents
wont be parsed by FM-Classic.binary
-- returns a BinaryData
object. This is
useful for caching other file types, such as images.The owner of the cache should implement the CacheListener
interface and register itself using addCacheListener()
.
If the template cannot read its cache directory, the periodic updates will be cancelled until the next time a loading policy is set.
TemplateCache
,
CacheEvent
,
CacheListener
,
Updateable.update(String)
Nested Class Summary | |
---|---|
static class |
FileTemplateCache.Strategy
Enumeration of caching strategies available to this cache. |
Constructor Summary | |
---|---|
FileTemplateCache()
Constructs an empty FileTemplateCache . |
|
FileTemplateCache(java.io.File dir)
Constructs a FileTemplateCache with a directory in which
it will look for template files. |
|
FileTemplateCache(java.io.File dir,
long delay)
Constructs a FileTemplateCache with a directory in which
it will look for template files, and a delay representing the
number of seconds between cache updates. |
|
FileTemplateCache(java.lang.String path)
Constructs a FileTemplateCache with a directory in which
it will look for template files. |
|
FileTemplateCache(java.lang.String path,
long delay)
Constructs a FileTemplateCache with a directory in which
it will look for template files, and a delay representing the
number of seconds between cache updates. |
Method Summary | |
---|---|
void |
addCacheListener(CacheListener listener)
Registers a CacheListener for this Cache . |
CacheListener[] |
getCacheListeners()
Retrieves all the CacheListener s associated with this cache. |
java.lang.String |
getDefaultTemplate()
Retrieves the default template type to be created when retrieving items from the cache. |
long |
getDelay()
Returns the interval between two cache updates. |
java.io.File |
getDirectory()
Returns the template cache root directory. |
java.lang.String |
getEncoding()
Returns the character encoding to be used when reading template files. |
java.lang.String |
getFilenameSuffix()
Returns the template suffix. |
Cacheable |
getItem(java.lang.String name)
Gets the specified template from the cache, using the default template type. |
Cacheable |
getItem(java.lang.String name,
java.lang.String type)
Gets the specified template type from the cache. |
FileTemplateCache.Strategy |
getLoadingPolicy()
Returns the loading policy currently in effect |
int |
getMaximumAge()
Retrieves the maximum age a cache item can be before it is evicted from the cache. |
java.lang.String |
getPath()
Returns the template cache root directory. |
CacheRetriever |
getRetriever()
Gets the CacheRetriever currently in use. |
TemplateRegistry |
getTemplateRegistry()
Retrieves the current TemplateRegistry in use. |
java.util.Iterator<CacheElement> |
listCachedFiles()
Returns a list of cached files. |
void |
removeCacheListener(CacheListener listener)
Unregisters a CacheListener for a Cache . |
void |
setDefaultTemplate(java.lang.String aTemplateType)
Sets the default template type to be created when retrieving items from the cache. |
void |
setDelay(long delay)
Sets the interval between two cache updates. |
void |
setDirectory(java.io.File dir)
Sets the template cache root directory. |
void |
setEncoding(java.lang.String encoding)
Sets the character encoding to be used when reading template files. |
void |
setFilenameSuffix(java.lang.String filenameSuffix)
Sets the template suffix. |
void |
setLoadingPolicy(FileTemplateCache.Strategy loadingPolicy)
Sets the loading policy for this FileTemplateCache . |
void |
setMaximumAge(int age)
Sets the maximum age a cache item can be before it is evicted from the cache. |
void |
setPath(java.lang.String path)
Sets the template cache root directory. |
void |
setRetriever(CacheRetriever retriever)
Deprecated. use setRetrieverFactory(freemarker.template.cache.CacheRetrieverFactory) to set a retriever factory |
void |
setRetrieverFactory(CacheRetrieverFactory factory)
Sets the CacheRetrieverFactory
to be used for creating cache retrievers for the cache. |
void |
setTemplateRegistry(TemplateRegistry cRegistry)
Sets a TemplateRegistry implementation
to use when creating new templates. |
void |
stopAutoUpdate()
Stops the updating of the cache. |
java.lang.String |
toString()
Returns a string representation of the object. |
void |
update()
Updates the cache. |
void |
update(java.lang.String name)
Update a named template if in the FileTemplateCache.Strategy.LOAD_AD_HOC mode . |
void |
update(java.lang.String name,
java.lang.String type)
Update a named template if in the FileTemplateCache.Strategy.LOAD_AD_HOC mode. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public FileTemplateCache()
FileTemplateCache
.
public FileTemplateCache(java.lang.String path)
FileTemplateCache
with a directory in which
it will look for template files.
path
- the absolute path of the directory containing
templates for this cache.
java.lang.IllegalArgumentException
- the root directory is nullpublic FileTemplateCache(java.io.File dir)
FileTemplateCache
with a directory in which
it will look for template files.
dir
- the directory containing templates for this cache.
java.lang.IllegalArgumentException
- the root directory is nullpublic FileTemplateCache(java.lang.String path, long delay)
FileTemplateCache
with a directory in which
it will look for template files, and a delay representing the
number of seconds between cache updates.
path
- the absolute path of the directory containing
templates for this cache.delay
- the number of seconds between cache updates.public FileTemplateCache(java.io.File dir, long delay)
FileTemplateCache
with a directory in which
it will look for template files, and a delay representing the
number of seconds between cache updates.
dir
- the directory containing templates for this cache.delay
- the number of seconds between cache updates.Method Detail |
---|
public FileTemplateCache.Strategy getLoadingPolicy()
public void setLoadingPolicy(FileTemplateCache.Strategy loadingPolicy)
Sets the loading policy for this FileTemplateCache
. If
FileTemplateCache.Strategy.LOAD_ON_DEMAND
, templates will be loaded as they are
requested, and each template's file modification date will be
checked each time it is requested. If FileTemplateCache.Strategy.PRELOAD
, all
templates in the cache directory and its subdirectories will
be loaded when the cache is started, and new templates will be
added to the cache each time it is updated. If FileTemplateCache.Strategy.LOAD_AD_HOC
,
all templates in the cache directory and its subdirectories will be
loaded when the cache is created and a particular template file's
modification date will be checked each time the client requests the
update of that and only that template.
Defaults to FileTemplateCache.Strategy.LOAD_ON_DEMAND
.
loadingPolicy
- cache mode
java.lang.IllegalArgumentException
- the caching policy is invalidpublic void setPath(java.lang.String path)
path
- the absolute path of the directory containing
templates for this cache.
java.lang.IllegalArgumentException
- the root directory is nullpublic java.lang.String getPath()
public void setDirectory(java.io.File dir)
dir
- the root directory containing templates for this cache
java.lang.IllegalArgumentException
- the root directory is nullpublic java.io.File getDirectory()
public void setDelay(long delay)
Sets the interval between two cache updates. This is meaningful
only if the cache policy is set to FileTemplateCache.Strategy.LOAD_ON_DEMAND
,
FileTemplateCache.Strategy.LOAD_ON_DEMAND_WITH_REFRESH
or FileTemplateCache.Strategy.PRELOAD
.
Defaults to five seconds.
delay
- the number of seconds between cache updatespublic long getDelay()
FileTemplateCache.Strategy.LOAD_ON_DEMAND
,
FileTemplateCache.Strategy.LOAD_ON_DEMAND_WITH_REFRESH
or FileTemplateCache.Strategy.PRELOAD
.
public void setMaximumAge(int age)
Sets the maximum age a cache item can be before it is evicted
from the cache. The age is determined as the number of cache updates
since the item was last accessed. This is meaningful
only if the cache policy is set to FileTemplateCache.Strategy.LOAD_ON_DEMAND
or
FileTemplateCache.Strategy.LOAD_ON_DEMAND_WITH_REFRESH
.
Defaults to never expiring.
age
- the maximum age before an item is evicted from the cache,
or 0 to indicate that items should never be evictedpublic int getMaximumAge()
FileTemplateCache.Strategy.LOAD_ON_DEMAND
or
FileTemplateCache.Strategy.LOAD_ON_DEMAND_WITH_REFRESH
.
public void setEncoding(java.lang.String encoding)
null
is specified, the default encoding will be used.
encoding
- the name of the encoding to be used; this will be
passed to the constructor of InputStreamReader
.public java.lang.String getEncoding()
null
is returned, the default encoding is used.
getEncoding
in interface TextEncoding
InputStreamReader
.public void setFilenameSuffix(java.lang.String filenameSuffix)
filenameSuffix
- the optional filename suffix of template
files to be read for this cache.public java.lang.String getFilenameSuffix()
public void addCacheListener(CacheListener listener)
CacheListener
for this Cache
.
addCacheListener
in interface Cache
listener
- the CacheListener
to be registered.CacheListener
public void removeCacheListener(CacheListener listener)
CacheListener
for a Cache
.
removeCacheListener
in interface Cache
listener
- the CacheListener
to be unregistered.CacheListener
public CacheListener[] getCacheListeners()
CacheListener
s associated with this cache.
getCacheListeners
in interface Cache
CacheListener
spublic void stopAutoUpdate()
stopAutoUpdate
in interface Cache
public java.util.Iterator<CacheElement> listCachedFiles()
listCachedFiles
in interface Cache
public void update(java.lang.String name)
FileTemplateCache.Strategy.LOAD_AD_HOC
mode .
Do nothing if in other modes.
update
in interface Updateable
name
- of template to updatepublic void update(java.lang.String name, java.lang.String type)
FileTemplateCache.Strategy.LOAD_AD_HOC
mode.
Do nothing if in other modes.
update
in interface Updateable
name
- the name of template to updatetype
- the type of template to updatepublic void update()
FileTemplateCache.Strategy.LOAD_AD_HOC
mode, this does nothing.
update
in interface Updateable
public Cacheable getItem(java.lang.String name)
getItem
in interface Cache
name
- a string uniquely identifying the template.
null
if not found.setDefaultTemplate(java.lang.String)
public Cacheable getItem(java.lang.String name, java.lang.String type)
getItem
in interface Cache
name
- a string uniquely identifying the template.type
- the type of template to be retrieved
null
if not found.setDefaultTemplate(java.lang.String)
public CacheRetriever getRetriever()
CacheRetriever
currently in use.
CacheRetriever
used for retrieving templatespublic void setRetrieverFactory(CacheRetrieverFactory factory)
CacheRetrieverFactory
to be used for creating cache retrievers for the cache.
factory
- the CacheRetrieverFactory
to be used for
retrieving templatespublic void setRetriever(CacheRetriever retriever)
setRetrieverFactory(freemarker.template.cache.CacheRetrieverFactory)
to set a retriever factory
CacheRetriever
to be used for the cache.
retriever
- the CacheRetriever
to be used for
retrieving templatespublic TemplateRegistry getTemplateRegistry()
TemplateRegistry
in use.
getTemplateRegistry
in interface RegistryAccepter
TemplateRegistry
to used when retrieving
items to be cachedpublic void setTemplateRegistry(TemplateRegistry cRegistry)
TemplateRegistry
implementation
to use when creating new templates.
setTemplateRegistry
in interface RegistryAccepter
cRegistry
- the TemplateRegistry
to be used when
retrieving items to be cachedpublic java.lang.String getDefaultTemplate()
public void setDefaultTemplate(java.lang.String aTemplateType)
TemplateRegistry
. Three types of object
are registered with the TemplateRegistry
by default:
template
-- returns a Template
object.unparsed
-- returns an UnparsedTemplate
object.
This behaves like other Template
objects, but its contents wont be
parsed by FM-Classic.binary
-- returns a BinaryData
object. This is
useful for caching other file types, such as images.
aTemplateType
- the type of template to be cached by defaultpublic java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |