Interface for the class that implement a cache manager.
Implementing this interface gives the application using ezcTemplate the possibilities to:
The CacheManager implementation of should be made available to the Template component. Register the CacheManager implementation to the 'cacheManager' property in the TemplateConfiguration.
The next example shows how the cache manager 'MyCacheManager' is registered:
The methods in this interface that are called only by ezcTemplate:
A typical cache creation process is as follows:
3. If a cache file exists for the requested template, the template component calls CacheManager::isValid() to see whether the cache file is still correct. If the cache file is still correct, it will execute it and return. Otherwise it will (over)write the cache file.
4. When the cache file needs to be recreated, it calls CacheManager::startCaching() first. The CacheManager is now informed that the operations in the user application originate from the template that is creating the cache. For example, the operations performed on the database are (indirectly) invoked, via a custom block or function, by the Template engine.
5. The application code that returns the requested data should also inform the Cache Manager via the CacheManager::register() method. For example, the Custom Function {fetch} calls CacheManager::register( "user", $userID ) for each requested user.
The CacheManager::register() method stores the values in the database and relates it to the cache file currently in creation. When one of these values changes, the cache file would be outdated.
6. The CacheManager::includeTemplate() is called when another template is included. Relate the current cache with the included template and store it (for example in the database). The CacheManager::isValid() can then also outdate the cache when a sub template is modified.
7. The CacheManager::stopCaching() is called when the cache file is created.
8. The user application should call the CacheManager::update() method when a change in the database or application may affect any of the caches.
Keeping track of the values used in the caches can probably best stored in a database. Using the file system only is also possible but makes things more complicated. The documentation of each method in this interface has also an example implementation. The examples rely on a MySQL database with at least the following tables.
The table 'cache_files' has (at least) the following properties:
The table 'cache_values' has the following properties:
Another table can also store the cache keys that belong to the cache. The structure of this table is similar to the 'cache_values' table.
Source for this file: /Template/src/interfaces/cache_manager.php
Version: | //autogen// |
public void |
cleanExpired(
)
The cleanExpired method should remove the expired caches. |
public void |
includeTemplate(
$template
, $templatePath
)
This method is called by the template engine when another template is included. |
public void |
isValid(
$template
, $templateName
, $cacheName
)
The isValid method is called by ezcTemplate to verify whether the cache is valid. |
public void |
register(
$name
, $value
)
The user application should call this method to register values used in the current cache creation. |
public void |
startCaching(
$template
, $templatePath
, $cachePath
, $cacheKeys
)
The template engine calls this method when a new cache file will be created. |
public void |
stopCaching(
)
The stopCaching method is called by the Template Engine when the cache file is created. |
public void |
update(
$name
, $value
)
The user application should call this method when the value changes that is previously registered with the register() method. |
The cleanExpired method should remove the expired caches.
The unused cache files on the hard-disk and the entries from the database tables: cache_values and cache_files can be removed. This method should be called once in a while to garantee that the system is not flooded with expired cache data.
An example implementation:
This method is called by the template engine when another template is included.
The implementation of this method should register the current included template. The following code registers all the included templates and relates them to the cache file in creation.
Name | Type | Description |
---|---|---|
$template |
ezcTemplate | |
$templatePath |
string |
The isValid method is called by ezcTemplate to verify whether the cache is valid.
The steps that could be implemented:
The following code is an implementation of the steps above:
Name | Type | Description |
---|---|---|
$template |
ezcTemplate | |
$templateName |
string | |
$cacheName |
string |
The user application should call this method to register values used in the current cache creation.
Typically, the function that does a database query and returns the result set should also call the register() method. The implementation of the register method should update the 'cache_values' table for all the cache_files on the stack. See the next example code:
The member variables: $this->depth and $this->keys keep the amount of cache files and the cache file data that currently created. Notice that the amount of cache files on the stack only increases with a 'template include' to another cache file.
In the cache_values table maps a name, value to an ID. The update() method uses this information.
Name | Type | Description |
---|---|---|
$name |
string | |
$value |
string |
The template engine calls this method when a new cache file will be created.
The $template parameter contains the Template object. This allows you to reach the current template configuration, if needed. The $templatePath is the (relative) path to the template currently creating the cache file. The $cachePath is the (relative) path to the cache file.
$cacheKeys contains all cache keys used in the template (if any).
The startCaching method commonly performs the following steps, of course it's up to your application:
3. Store the current template path in a database table that handles the included templates. The current template includes 'itself'. This makes it easier to mark the this cache outdated.
The following code demonstrates the steps from above and uses the ezcSignalSlot component to send the signals to the register method:
The code above assumes that the private or protected member variables $depth and $keys are available in the class.
Name | Type | Description |
---|---|---|
$template |
ezcTemplate | |
$templatePath |
string | |
$cachePath |
string | |
$cacheKeys |
array(string=>string) |
The stopCaching method is called by the Template Engine when the cache file is created.
The current cache information: cacheKeys, templatePath, Template object can be popped from the internal stack. This is demonstrated in the following code:
The user application should call this method when the value changes that is previously registered with the register() method.
Any name-value combination that is registered should be updated when the value changes. The cache file(s) using this name-value are marked as expired.
The next example implementation expires all the cache files that uses the name-value:
Name | Type | Description |
---|---|---|
$name |
string | |
$value |
string |