Search by

websk / php-cache

websk

Cache abstraction with optional Memcache, Memcached, and Redis backends

Package info

github.com/websk/php-cache

pkg:composer/websk/php-cache

Statistics

Installs: 204

Dependents: 5

Suggesters: 0

Stars: 1

Open Issues: 0

3.0.0 2026-10-10 06:00 UTC

This package is auto-updated.

Last update: 2026-10-10 06:01:27 UTC


README

A small cache abstraction for PHP applications with optional Memcache, Memcached, and Redis backends.

Requirements

  • PHP 8.3 or newer, including PHP 8.5
  • psr/container 1.1 or 2.0
  • One backend client selected by the application

The base package does not install a backend client. Install only the backend used by the application:

Engine Additional requirement
WebSK\Cache\Engines\Memcache PHP memcache extension
WebSK\Cache\Engines\Memcached PHP memcached extension
WebSK\Cache\Engines\Redis composer require predis/predis:^3.7

Installation

composer require websk/php-cache

Install and enable either ext-memcache or ext-memcached through the PHP extension manager available in the deployment environment. For Redis, install Predis in the application:

composer require predis/predis:^3.7

Configuration

Select the engine class and provide its servers:

use WebSK\Cache\Engines\Memcached;

$config = [
    'settings' => [
        'cache' => [
            'engine' => Memcached::class,
            'cache_key_prefix' => 'websk',
            'servers' => [
                [
                    'host' => 'memcached',
                    'port' => 11211,
                ],
            ],
        ],
    ],
];

Each server must have a string host and an integer port. An empty cache_key_prefix uses default when cache keys are generated.

When the Redis engine receives one server, it creates a normal Predis connection. Multiple servers use Predis client-side clustering unless explicit cluster, replication, or aggregate options are passed to the Redis constructor.

Container registration

The service provider requires a container that implements both Psr\Container\ContainerInterface and ArrayAccess. Settings are read from the PSR-11 settings entry, while the cache service factory is registered through array access.

use WebSK\Cache\CacheServiceProvider;

CacheServiceProvider::register($container);

$cache = CacheServiceProvider::getCacheService($container);
$cache->set('user-42', ['name' => 'Alice'], 300);
$user = $cache->get('user-42');

CacheWrapper can be used after assigning the same PSR-11 container:

use WebSK\Cache\CacheWrapper;

CacheWrapper::setContainer($container);
$value = CacheWrapper::get('user-42');

Cache behavior

  • get() returns false for a cache miss or unavailable backend.
  • Consequently, a stored boolean false cannot be distinguished from a cache miss.
  • TTL 0 means no expiration.
  • Negative TTL values are rejected by every engine.
  • Memcache and Memcached accept relative TTL values below 30 days; values of 30 days or more are rejected by this library.
  • Redis serializes values before storing them and accepts any non-negative TTL.
  • Keys are hashed with MD5 after the configured prefix is applied.

Development

Run syntax checks, PHPUnit, and PHPStan level max with:

composer check