silviooosilva/cacheer-php

CacheerPHP is a minimalist package for caching in PHP, offering a simple interface for storing and retrieving cached data using multiple backends.


README

Maintainer Packagist Dependency Version Latest Version Quality Score Packagist Downloads

CacheerPHP is a minimalist PHP caching library. Version 4 brings a more robust API, optional compression and encryption and support for multiple backends including files, databases and Redis.

Features

  • Multiple storage drivers: file system, databases (MySQL, PostgreSQL, SQLite), Redis and in-memory arrays.
  • Customizable expiration: define precise TTL (Time To Live) values.
  • Automatic and manual flushing: clean cache directories with flushAfter or on demand.
  • Namespaces: group entries by category for easier management.
  • Flexible output formatting: return cached data as JSON, arrays, strings or objects.
  • Compression & encryption: reduce storage footprint and secure cache contents.
  • OptionBuilder: fluent builders to configure File, Redis and Database drivers without typos (supports default TTL via expirationTime and auto-flush via flushAfter).
  • Advanced logging and statistics: monitor hits/misses and average times (coming soon).

Requirements

  • PHP 8.0+
  • Optional extensions: PDO drivers for MySQL, PostgreSQL or SQLite
  • Redis server and predis/predis if using the Redis driver

Installation

Install via Composer:

composer require silviooosilva/cacheer-php

Configuration

Copy the example environment file and adjust the settings for your environment:

cp .env.example .env

Environment variables control database and Redis connections. See Configuration for the full list.

Quick start

require_once __DIR__ . '/vendor/autoload.php';

use Silviooosilva\CacheerPhp\Cacheer;

$key   = 'user_profile_1234';
$value = ['id' => 123, 'name' => 'John Doe'];

// Configure cache and driver statically
Cacheer::setConfig()->setTimeZone('UTC');
Cacheer::setDriver()->useArrayDriver();

// Static usage with boolean return
Cacheer::putCache($key, $value);
if (Cacheer::has($key)) {
    $cached = Cacheer::getCache($key);
    var_dump($cached);
}

// Dynamic usage and isSuccess()
$cache = new Cacheer([
    'cacheDir' => __DIR__ . '/cache',
]);
$cache->has($key);
if ($cache->isSuccess()) {
    $cached = $cache->getCache($key);
    var_dump($cached);
} else {
    echo $cache->getMessage();
}

// Alternatively, check the state via isSuccess()
$cache->has($key);
if ($cache->isSuccess()) {
    $cached = $cache->getCache($key);
    var_dump($cached);
}

Documentation

Runnable PHP samples live in the Examples/ directory.

Testing

After installing dependencies, run the test suite with:

vendor/bin/phpunit

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

CacheerPHP is open-sourced software licensed under the MIT license.

Support

If this project helps you, consider buying the maintainer a coffee.

silviooosilva