Search by

coercive / cache

Coercive

Coercive Utility Cache

Package info

github.com/Coercive/Cache

Homepage

pkg:composer/coercive/cache

Statistics

Installs: 1 324

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

0.2.0 2026-10-09 21:52 UTC

This package is auto-updated.

Last update: 2026-10-09 21:54:51 UTC


README

Simple JSON file cache.

Compatible PHP 7.4 → 8.5

Get

composer require coercive/cache

Tests

composer install
vendor/bin/phpunit

PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.

JSON

use Coercive\Utility\Cache\Json;

# Load your cache instance for a defined project
$json = new Json('/temp/my_project_directory');

# Set a global default expire delay (DateInterval format, default P7D)
$json = new Json('/temp/my_project_directory', 'P1D');
# OR
$json->setExpireDelay('P1D');

# The cache is DISABLED by default : enable it (creates the directory if needed)
# Returns false (and stays disabled) if the directory can't be used
$json->enable();

# Retrieve your data (null if not found / expired / cache disabled)
$data = $json->get('data-key');

# Verify if data exists (even if the stored value is null)
$json->has('data-key');

# Save your data
$json->set('data-key', ['data1', 'data2']);

# Save your data with specific expire delay
$json->set('data-key', ['data1', 'data2'], 'PT15M');

# Retrieve your data, or compute and save it if not found
# (if the cache is disabled, the callback is always called)
$data = $json->remember('data-key', function () {
    return ['data1', 'data2'];
}, 'PT15M');

# Delete specific data
$json->delete('data-key');

# Delete expired / corrupted data (and orphan temp files)
$json->purge();

# Empty cache (only the cache files, other files in the directory are kept)
$json->clear();

# You can enable/disable the cache
$json->disable();
$json->enable();
$json->setStatus(bool);
$json->isEnable();

Keys are cleaned to build the filename : every sequence of characters other than a-z0-9 becomes _ (so my-key and my key share the same entry).

Debug

# Use debug method to handle exceptions
$json->debug(function(Exception $e) {
    echo $e->getMessage();
    // do something
});

# You can also expose all exceptions
var_dump($json->getExceptions());