sanovskiy/simple-cache

A simple and transparent caching library for PHP with multiple driver support

1.0.0 2025-08-21 18:46 UTC

This package is auto-updated.

Last update: 2025-08-21 19:09:51 UTC


README

A simple and transparent caching library for PHP with multiple driver support.

Features

  • Lightweight and easy-to-use API
  • Multiple drivers: File, Memcached, Redis, Database, Mock
  • Built-in support for the "remember" pattern
  • Mock driver for testing

Installation

composer require sanovskiy/simple-cache

Requirements

  • PHP 8.0 or higher
  • For File driver: No additional requirements
  • For Memcached driver: ext-memcached (version >= 3.0 recommended)
  • For Redis driver: ext-redis (version >= 5.0 recommended)
  • For Database driver: ext-pdo with appropriate database driver (e.g., ext-pdo-mysql)
  • For Mock driver: No additional requirements

Configuration

Files

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\FileCacheDriver;

$cache = new Cache([
    'driver' => FileCacheDriver::class,
    'config' => [
        'directory' => '/var/cache/',
        'file_extension' => '.cache'
    ]
]);

Memcached

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\MemcachedCacheDriver;
$cache = new Cache([
    'driver' => MemcachedCacheDriver::class,
    'config' => [
        'servers' => [['127.0.0.1', 11211]],
        'prefix' => 'app_'
    ]
]);

Redis

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\RedisCacheDriver;
$cache = new Cache([
    'driver' => RedisCacheDriver::class,
    'config' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'prefix' => 'app_',
        'database' => 0
    ]
]);

SQL database

use Sanovskiy\SimpleCache\Cache;
use Sanovskiy\SimpleCache\Drivers\DatabaseCacheDriver;
$cache = new Cache([
    'driver' => DatabaseCacheDriver::class,
    'config' => [
        'dsn' => 'mysql:host=localhost;dbname=cache_db',
        'username' => 'user',
        'password' => 'pass',
        'table' => 'cache'
    ]
]);

Note: The DatabaseCacheDriver automatically creates cache table with columns key, value, expires_at, created_at, and updated_at (with an index on expires_at) if it doesn't exist. Ensure the database user has CREATE TABLE and INDEX permissions.

Mock cache for testing

$cache = new Cache([
    'driver' => \Sanovskiy\SimpleCache\Drivers\MockCacheDriver::class,
    'config' => [
        'emulate_failures' => false, // If true will emulate failure at every operation
        'log_operations' => true
    ]
]);

Usage

Basic usage

$cache->set('key', 'value', 3600);
$value = $cache->get('key', 'default');
$has = $cache->has('key');
$cache->delete('key');
$cache->clear();

Using the "remember" pattern:

$data = $cache->remember('expensive_data', function() {
    return expensiveOperation();
}, 1800);

Drivers

Simple Cache supports the following drivers:

  • File: Stores cache in files on the filesystem.
    • Config: directory (path to cache directory), file_extension (e.g., .cache)
  • Memcached: Uses a Memcached server for distributed caching.
    • Config: servers (array of [host, port]), prefix (key prefix)
  • Redis: Uses a Redis server for high-performance caching.
    • Config: host, port, prefix, database, password, timeout
  • Database: Stores cache in an SQL database (e.g., MySQL, PostgreSQL).
    • Config: dsn, username, password, table
  • Mock: In-memory driver for testing, does not persist data.
    • Config: emulate_failures (bool), log_operations (bool)

Testing

To run tests locally, ensure Memcached, Redis, and MySQL are running and PHP extensions (ext-memcached, ext-redis, ext-pdo_mysql) are installed. Then run:

composer install
./vendor/bin/phpunit tests/

To run the tests using Docker Compose:

docker-compose up --build

To generate a code coverage report:

docker-compose run php sh -c "composer install && ./vendor/bin/phpunit tests/ --coverage-html coverage"

Why Simple Cache?

Simple Cache is designed to be a lightweight, transparent, and easy-to-use caching library for PHP. Unlike other caching libraries that come with complex configurations or heavy dependencies, Simple Cache focuses on:

  • Simplicity: Intuitive API with minimal setup.
  • Flexibility: Supports multiple drivers (File, Memcached, Redis, Database, Mock) out of the box.
  • Testability: Includes a Mock driver for easy unit testing.
  • Lightweight: Minimal dependencies for better performance in small to medium projects.

Roadmap

  • Add support for PSR-6 (Caching Interface) in future versions
  • Add support for additional drivers (e.g., APCu)

Contributing

Found a bug or have an idea? Open an issue or submit a pull request on GitHub.

License

This library is licensed under the MIT License