websk / php-cache
Cache abstraction with optional Memcache, Memcached, and Redis backends
Requires
- php: ^8.3
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
Suggests
- ext-memcache: Required to use the Memcache cache engine
- ext-memcached: Required to use the Memcached cache engine
- predis/predis: Required to use the Redis cache engine; tested with ^3.7
Provides
None
Conflicts
None
Replaces
None
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/container1.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()returnsfalsefor a cache miss or unavailable backend.- Consequently, a stored boolean
falsecannot be distinguished from a cache miss. - TTL
0means 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