Object Cache

Introduction

As of release 6.2.0, eduTrac SIS has a more robust object cache API. Why a change in the cache API? eduTrac SIS has many tables and there is sure to be more in the future. As an institution grows, so does the data. Since there are several screens that are reading data from several tables, it can become computationally expensive to regenerate the data on each request.

Therefore, with object caching built in, you can distribute the database’s workload and improve performance. There are several drivers you can use for caching: APC, Cookie, Filesystem, JSON, Memcache, Memcached, and XCache. The Filesystem cache is enabled by default, but is filterable so that you can enable any of the others if your hosting environment supports it.

By default, caching in eduTrac SIS 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.

Please note that not everything in eduTrac SIS is cached due to the nature of PDO objects.

Listed below are the cache functions available.

etsis_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.

etsis_cache_add

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

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

etsis_cache_get

etsis_cache_get( $key, $namespace )

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

etsis_cache_replace

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

The etsis_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 (etsis_cache_add).

etsis_cache_delete

etsis_cache_delete( $key, $namespace )

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

etsis_cache_flush

etsis_cache_flush()

The etsis_cache_flush function purges the cache completely.

etsis_cache_flush_namespace

etsis_cache_flush_namespace( $value )

The etsis_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

/**
 * Global scope because we need the $app variable.
 */
$app = LitenLiten::getInstance();
/**
 * Select all records from person table.
 */
$nae = $app->db->person();
/**
 * Now lets do some caching magic.
 */
$q = etsis_cache_get('nae', 'nae');
if (empty($q)) {
    $q = $nae->find(function ($data) {
        $array = [];
        foreach ($data as $d) {
            $array[] = $d;
        }
        return $array;
    });
    etsis_cache_add('nae', $q, 'nae');
}

return $q;

Use a Different Cache Driver

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

$app = \Liten\Liten::getInstance();

function use_memcache_driver() {
    return 'memcache';
}
$app->hook->remove_filter('etsis_cache_driver','__return_false', 10);
$app->hook->add_filter('etsis_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.

$app = \Liten\Liten::getInstance();

function use_memcache_driver() {
    return 'memcache';
}
$app->hook->remove_filter('etsis_cache_driver','__return_false', 10);
$app->hook->add_filter('etsis_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 etsis_cache_increase_ttl filter to increase the cache expiry time.

$app = \Liten\Liten::getInstance();

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

Disable Caching

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

$app = \Liten\Liten::getInstance();
$app->hook->add_filter('enable_caching', false, 10);

Troubleshooting

If by chance the Filesystem cache seems somewhat slow on your server environment, you can enable the Benchmark function located in General Settings, and then try using a different caching driver if your server supports it. By doing this, you can benchmark the results to see which caching system works best for your server environment.

If none of them seem to work on your server environment, then use any of the file based system drivers (Filesystem, JSON, Cookie), 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.