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.

Maintainers

Package info

github.com/CacheerPHP/CacheerPHP

Homepage

pkg:composer/silviooosilva/cacheer-php

Statistics

Installs: 93

Dependents: 0

Suggesters: 0

Stars: 28

Open Issues: 0


README

CacheerPHP Logo

Maintainer Packagist Dependency Version Latest Version Quality Score Packagist Downloads

CacheerPHP is a minimalist PHP caching library with multiple backends (file, database, Redis and array), optional compression/encryption and a small but complete API.

Features

  • Multiple storage drivers: file system, databases (MySQL, PostgreSQL, SQLite), Redis and in-memory arrays.
  • TTL, namespaces and tags for organization and invalidation.
  • Auto-flush support with flushAfter.
  • Optional compression and encryption.
  • Formatter helper for JSON/array/object output.

Requirements

  • PHP 8.0+
  • PDO drivers for database backends
  • Redis server when using the Redis driver

Installation

composer require silviooosilva/cacheer-php

Configuration

Copy the example environment file and adjust as needed:

cp .env.example .env

Minimum variables:

DB_CONNECTION=sqlite
REDIS_HOST=127.0.0.1
REDIS_PORT=6379

For MySQL/PostgreSQL:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cacheer_db
DB_USERNAME=root
DB_PASSWORD=secret

Quick start

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

use Silviooosilva\CacheerPhp\Cacheer;

Cacheer::setConfig()->setTimeZone('UTC');
Cacheer::setDriver()->useArrayDriver();

Cacheer::putCache('user:1', ['id' => 1, 'name' => 'John']);
if (Cacheer::has('user:1')) {
    $user = Cacheer::getCache('user:1');
}

File driver with options:

$cache = new Cacheer([
    'cacheDir' => __DIR__ . '/cache',
    'loggerPath' => __DIR__ . '/cacheer.log',
    'expirationTime' => '1 hour',
    'flushAfter' => '1 day',
]);

Drivers

Cacheer::setDriver()->useFileDriver();
Cacheer::setDriver()->useDatabaseDriver();
Cacheer::setDriver()->useRedisDriver();
Cacheer::setDriver()->useArrayDriver();

Return contract

  • putCache, appendCache, clearCache, flushCache, renewCache, putMany, tag, flushTag return bool.
  • has returns bool.
  • getCache returns mixed|null (or CacheDataFormatter when formatter is enabled).
  • getMany returns array (or CacheDataFormatter when formatter is enabled).
  • getAll returns array (or CacheDataFormatter when formatter is enabled).
  • getMessage returns string; isSuccess returns bool.

Formatter usage:

$cache->useFormatter();
$json = $cache->getCache('user:1')->toJson();

Testing

vendor/bin/phpunit

Documentation

Full documentation is available at CacheerPHP Documentation.

Contributing

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

License

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

Support

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

silviooosilva