Use of caching in the application

Caching is handled by the Cache class, which is located in <install_path>/jadu/JaduCache.php. The following code demonstrates how to create an instance of the Cache class and store the results of a database query in the cache.

require_once('JaduCache.php');

$params = [
    'select' => ['name', 'value'],
    'table' => 'my_table',
    'where' => [
        'id' => 1234
    ],
    'orderBy' => [
        ['value', 'ASC']
    ]
];

$cache = new Cache('my_table', json_encode($params));
$result = null;

if ($cache->isEmpty()) {
    // run the query against the database and store the data in $result
    $cache->setData($result);
} else {
    $result = $cache->data;
}

The following extracts from the code above demonstrates a number of features of the Cache class.

$cache = new Cache('my_table', json_encode($params));

This creates a cache object for the my_table namespace with the key of the data passed in as the second parameter. The namespace is used to group together cache items so that they can be deleted when data under the namespace is updated. The most common analogy of a namespace is the name of the database table that a query relates to.

If the database table name is used as the namespace, when a record in the table is added, updated or deleted, the items cached under the namespace are deleted so that future queries are based on the latest data.

The second parameter, the key of the data, is used to identify a particular piece of data under a namespace, such as a database query. This allows a specific piece of data to be identified as being in the cache and fetched if necessary.

if ($cache->isEmpty()) {

The isEmpty function for a Cache object returns true or false, depending on if the data for the requested key and namespace is available in the cache. This can then be used to determine whether to generate the data afresh or return the data from the cache.

$cache->setData($result);

The setData function adds data to the cache under the namespace and key set in the constructor when instantiating the Cache object. The data will remain in the cache until a request is made to delete the namespace under which the data is stored. Any serializable data type can be stored in the cache. The data need not be serialized before it is passed to the setData method; serialization is handled by the Cache class.

$result = $cache->data;

When instantiating a cache object, if data is available for the namespace and key provided in the constructor then it is retrieved and stored in the $data variable in the Cache class.

Deleting a Cache Namespace

To delete a cache namespace call the deleteTableCache function. This function resides in <install_path>/JaduCache.php, however it is not part of the Cache class. To delete all data in the cache within the “users” namespace, call the function as follows:

require_once('JaduCache.php');
deleteTableCache('my_table');

Timed Based Caching

As an alternative to caching data indefinitely (until it is deleted) it is also possible to cache data for a specific period of time. This can be achieved using the TimedCache class.

The main difference between the interface of the standard Cache class and the TimedCache class is when using the setData function. After a TimedCache instance has been created, a time (in the form of a unix timestamp) can be provided to the setData function to determine when the cache will expire, for example:

require_once('JaduCache.php');
$timedCache = new TimedCache('namespace', 'timer');
$timedCache->setData('fred', time() + 120);

The preceding code would cache the value ”fred” under the “namespace” namespace for the key “timer” for 120 seconds. If the same cache object is created after 120 seconds the cache would be classed as empty, i.e. calls to the isEmpty function would return false.

Clearing the Cache

Any Jadu files that update database table records should clear the caches of any tables that are updated. In this way the Continuum CMS cache becomes self-regulating. Should a file or database table be updated manually, i.e. not through using a Continuum CMS user interface, then it will be necessary to clear the cache of the respective table. This can be done by manually deleting the directory with the name associated with the database table from the file system. Jadu also provide a user interface for clearing table caches. The script can be found at https://<DOMAIN>/jadu/maintenance/cacheBash.php.

Each checkbox represents a cache namespace, the majority of which are associated with a single database table. To clear a cache (or caches) tick the box next to the cache and click “Delete Selected”.

results matching ""

    No results matching ""