Cache

The Cache addon stores key=>value pairs as files with expiration dates. The cache is not automatically cleaned up.

There are two caches:

  1. The 'main' cache which is used when accessing the cache addon.
  2. The 'resources' cache which is used by the Resources addon when storing compiled CSS and JS files. (This uses an instance of the cache addon)

Related: Cache Addon, Resources

Docs

  • Configure Caching
  • Store & Retrieve values
  • Cache Location
  • Clear the cache

Configure Caching

The main cache can be configured after the main server packag is initialized. Example:

<?php  
$lia = new \Lia();  
$main = \Lia\Package\Server::main($lia);   
  
$cache = \Lia\Addon\Cache::from($lia);  
$cache->dir = __DIR__.'/cache/'; // in liaison directory by defualt  
$cache->enabled = false; // true by default  

Store & Retrieve values

Each value is stored in its own file, and an associated meta file is created to track expiry.

Using $cache = \Lia\Addon\Cache::from($lia)

  • $cache->cache_file('some-name', 'some-value', $max_age_in_seconds); STORE a value
  • $cache->get_cache_file_content('some-name'); RETRIEVE a value. Returns false if value is not stored or if it is expired.
  • $cache->get_cache_file_path('some-name') get absolute path to the cache file or FALSE if the file does not exist or is expired.

Cache Location

  1. 'main' cache is at vendor/taeluf/liaison/src/cache/
  2. 'resources' cache is at vendor/taeluf/liaison/src/cache-resources/

Clear the cache

You may want to set a daily CRON task to clear the cache. If you use the PHP cache clearing method during a request, the operation could delay a response, especially if there are many cache files..

CLI:

  • vendor/bin/lia clear-cache deletes ALL cached files for both the main cache and the resources cache.
  • vendor/bin/lia clear-cache --stale deletes expired files only

PHP:

  • \Lia\Addon\Cache::from($lia)->delete_stale_files() deletes expired files from main cache.
  • \Lia\Addon\Cache::from($lia)->delete_all_cache_files() deletes all files from the main cache.
  • \Lia\Addon\Resources::from($lia)->cache->delete_stale_files() deletes expired files from resources cache.
  • \Lia\Addon\Resources::from($lia)->cache->delete_all_cache_files() deletes all files from the resources cache.

NOTE: Deleting cache files also deletes improper files, such as those that do not have an associated meta information file.