By default, caching in TriTan CMS is somewhat non-persistent. This means that after a request, the cache could have expired by the time the next request is made. Check out the section on Cache Expiry Time if you would like to extend the expiry time.

Listed below are the cache functions available.

ttcms_cache_* Functions

Most of the cache functions use similar variables:

  • $key: Unique key connected to the content being cached.
  • $data: The content that should be stored.
  • $namespace: (optional) The way of namespacing data within the cache. Namespacing allows you to use the same key across different namespaces.
  • $expire: (optional) Defined in seconds of how long the data remains in cache. The default is 120 seconds which equals to 2 minutes.

ttcms_cache_add

ttcms_cache_add( $key, $data, $namespace, $expire )

The ttcms_cache_add function is used to add content to the cache if the content does not exist.

ttcms_cache_get

ttcms_cache_get( $key, $namespace )

The ttcms_cache_get function is used to retrieve content stored in the cache by a unique key.

ttcms_cache_replace

ttcms_cache_replace( $key, $data, $namespace, $expire )

The ttcms_cache_replace function is a bit of a misnomer. For some libraries, it actually is a replace of the data based on the unique key while for others, it is a re-creation (ttcms_cache_add).

ttcms_cache_delete

ttcms_cache_delete( $key, $namespace )

The ttcms_cache_delete function is used to delete a certain item from the cache.

ttcms_cache_flush

ttcms_cache_flush()

The ttcms_cache_flush function purges the cache completely.

ttcms_cache_flush_namespace

ttcms_cache_flush_namespace( $value )

The ttcms_cache_flush_namespace function is used to purge the cache of a particular namespace instead of purging the whole cache.

Example

This example shows you how to cache a database query.

use TriTan\Functions as func;

/**
 * Check the cache for records.
 */
$post = func\ttcms_cache_get($post_id, 'post');

/**
 * If cache is empty, query database, add results to cache and retrieve.
 */
if (!$post) {
    $post = app()->db->table(Config::get('tbl_prefix') . 'post')
            ->where('post_id', (int) $post_id)
            ->first();
    if (!$post) {
        return false;
    }
    func\ttcms_cache_add($post_id, $post, 'post');
}

return $post;

Use a Different Cache Driver

The example below shows you how to hook into the ttcms_cache_driver filter in order to use a different caching driver.

function use_memcache_driver() {
    return 'memcache';
}
app()->hook->{'remove_filter'}('ttcms_cache_driver','__return_false', 10);
app()->hook->{'add_filter'}('ttcms_cache_driver', 'use_memcache_driver', 10);

Memcache is used for the example above because there is a second filter that allows you to more specifically use Memcached (notice the ‘d’) if your server allows it. Below is an extended version of the previous code you can use if you want to use Memcached.

function use_memcache_driver() {
    return 'memcache';
}
app()->hook->{'remove_filter'}('ttcms_cache_driver','__return_false', 10);
app()->hook->{'add_filter'}('ttcms_cache_driver', 'use_memcache_driver', 10);
app()->hook->{'add_filter'}('use_memcached', true);

Cache Expiry Time

As explained earlier in this article, you can extend the expiry time so that caching is more persistent. Below is an example of how you can hook into the ttcms_cache_increase_ttl filter to increase the cache expiry time.

function override_cache_ttl($expire) {
    /**
     * 3600 seconds is equivalent to 1 hour.
     */
    $expire = 3600;
    return $expire;
}
app()->hook->{'remove_filter'}('ttcms_cache_increase_ttl', '__return_null', 10, 1);
app()->hook->{'add_filter'}('ttcms_cache_increase_ttl', 'override_cache_ttl', 10, 1);

Disable Caching

If you need to disable caching, create a new PHP file (i.e. custom.dropin.php) that will go into the dropins folder, and add the following code to that file.

app()->hook->{'add_filter'}('enable_caching', false, 10);

Troubleshooting

If by chance the Filesystem cache seems somewhat slow on your server environment, then try using a different caching driver if your server supports it. If none of them seem to work on your server environment, then use any of the other file based system drivers (JSON, Cookie), and use the above code from the Cache Expiry Time section and set the $expire variable to zero. This will cause the cache to be deleted immediately after request.