Cache Systems

Latch is integrated with a caching system for expensive CMS operations, including database queries, rendered form HTML, extension file paths, and tag groups.

Cache Modes

You can choose the cache backend through the Admin Dashboard under Performance -> Cache Mode.

Latch provides the following modes out of the box.

Basic

The simplest mode and the default installed mode. Cached data is stored in a PHP array for the lifetime of the current request.

This mode is compatible with any server that can run Latch, but it does not persist across requests.

Filesystem

Cached data is stored in the root cache directory as serialized cache files.

When this mode initializes, it creates the cache directory when needed and writes an .htaccess file that denies direct access. Confirm that your server honors .htaccess files, or apply equivalent web server rules.

APCu

Uses PHP's APC User Cache.

This is the fastest included mode, but it requires the apcu PHP extension. If APCu is unavailable, the cacher marks itself uninitialized.

Config File

When saving Cache Mode through the dashboard, Latch attempts to write a LATCH_CACHER_PATH constant to config.php, for example:

define("LATCH_CACHER_PATH", "lib/cachers/filesystem.php");

This constant is important because the cache backend is loaded before the full Latch model is available. If caching behaves unexpectedly, check config.php and verify that LATCH_CACHER_PATH points to the expected cacher file.

Clearing Cache

Clearing the cache updates the Cache -> Last Updated setting and marks the current request as cache-cleared. Custom cachers should call parent::clear() so this shared behavior remains intact.

Creating New Cachers

Extension developers can provide new cachers. Each cache mode is a Latch\Cache\Cacher class that extends Latch\Cache\BaseCacher.

The base class lives in:

app/classes/cache.php

Core cachers live in:

lib/cachers

An extension cacher can live in:

extensions/my_cacher/lib/cachers/my-cacher.php

init()

Runs after the cacher is constructed. Use it for backend setup that requires the Latch model to be available.

fetch(string $key)

Retrieves data from the cache. Return the cached data when found, or null when not found.

Include the parent fetch guard at the top of your method:

if (parent::fetch($key) === false) return null;

This checks whether cache was cleared earlier in the current request.

store(string $key, $value, int|null $ttl = null): bool

Writes a cache value.

If your backend supports TTL, use the default TTL when the caller does not provide one:

$ttl = $ttl ?? parent::default_ttl();

The default TTL comes from the Performance -> Default Cache TTL setting and falls back to 3600 seconds.

delete(string $key): void

Deletes one cache key.

clear(): void

Wipes the entire cache backend.

Call the parent method first:

parent::clear();