infocyph / cachelayer
PSR-6/PSR-16 cache layer with local, distributed, and cloud adapters.
Requires
- php: >=8.3
- opis/closure: ^4.5
- psr/cache: ^3.0
- psr/simple-cache: ^3.0
Requires (Dev)
- infocyph/phpforge: dev-main
- mongodb/mongodb: ^1.20 || ^2.0
Suggests
- ext-apcu: For APCu-based caching (in-memory, per-process)
- ext-cassandra: For ScyllaDB/Cassandra usage via ScyllaDbCacheAdapter
- ext-mbstring: Recommended for development tools output formatting (Pest/Termwind)
- ext-memcached: For Memcached-based caching (distributed, RAM)
- ext-mongodb: For MongoDB usage via MongoDbCacheAdapter
- ext-pdo: For PDO-based SQL caching (MySQL/MariaDB/PostgreSQL/etc.)
- ext-pdo_mysql: For MySQL/MariaDB usage via Cache::pdo(...)
- ext-pdo_pgsql: For PostgreSQL usage via Cache::pdo(...)
- ext-pdo_sqlite: For default SQLite usage via Cache::pdo(...) or Cache::sqlite(...)
- ext-redis: For Redis-based caching (persistent, networked)
- ext-sysvshm: For shared-memory caching via SharedMemoryCacheAdapter
- mongodb/mongodb: For MongoDB caching via MongoDbCacheAdapter
This package is auto-updated.
Last update: 2026-05-11 11:26:50 UTC
README
CacheLayer is a standalone cache toolkit for modern PHP applications. It provides a unified API over PSR-6 and PSR-16 with local, distributed, and cloud adapters.
Project Background
CacheLayer was separated from the existing Intermix project to improve package visibility, maintenance focus, and faster feature enrichment for caching.
Features
- Unified
Cachefacade implementing PSR-6, PSR-16,ArrayAccess, andCountable - Adapter support for APCu, File, PHP Files, Memcached, Redis, Valkey, Redis Cluster, PDO (SQLite default), Shared Memory, MongoDB, and ScyllaDB
- Tiered cache composition via
Cache::tiered()(L1/L2/... descriptors or pool instances) - Tagged invalidation with versioned tags:
setTagged(),invalidateTag(),invalidateTags() - Stampede-safe
remember()with pluggable lock providers - Per-adapter metrics counters and export hooks
- Payload compression controls
- Value serializer helpers for closures/resources
- Memoization helpers:
memoize(),remember(),once()
Requirements
- PHP 8.3+
- Composer
Optional extensions/packages depend on adapter choice:
ext-apcuext-redisext-memcachedext-pdo+ driver (pdo_sqlite,pdo_pgsql,pdo_mysql, ...)ext-sysvshmmongodb/mongodbext-cassandra
Installation
composer require infocyph/cachelayer
Usage
use Infocyph\CacheLayer\Cache\Cache; $cache = Cache::pdo('app'); // defaults to sqlite file under sys temp cachelayer/pdo $cache->setTagged('user:1', ['name' => 'Ada'], ['users'], 300); $user = $cache->remember('user:1', function ($item) { $item->expiresAfter(300); return ['name' => 'Ada']; }, tags: ['users']); $cache->invalidateTag('users'); $metrics = $cache->exportMetrics();
Tiered Flow (L1 -> L2 -> DB)
use Infocyph\CacheLayer\Cache\Cache; $cache = Cache::tiered([ ['driver' => 'apcu', 'namespace' => 'app'], // L1 ['driver' => 'valkey', 'namespace' => 'app', 'dsn' => 'valkey://127.0.0.1:6379'], // L2 ], writeToL1: false); // optional L1 write-through $value = $cache->remember('user:42', function ($item) use ($pdo) { $item->expiresAfter(300); $stmt = $pdo->prepare('SELECT payload FROM users_cache_source WHERE id = ?'); $stmt->execute([42]); return $stmt->fetchColumn(); });
Request flow:
- check APCu (L1)
- check Redis/Valkey (L2)
- query DB on miss
- write L2
- optionally write L1 (controlled by
writeToL1)
Security Hardening
CacheLayer includes optional payload/serialization hardening controls:
$cache ->configurePayloadSecurity( integrityKey: 'replace-with-strong-secret', maxPayloadBytes: 8_388_608, ) ->configureSerializationSecurity( allowClosurePayloads: false, allowObjectPayloads: false, );
You can also set:
CACHELAYER_PAYLOAD_INTEGRITY_KEYCACHELAYER_MAX_PAYLOAD_BYTES
See SECURITY.md for deployment guidance and threat model notes.
Documentation
https://docs.infocyph.com/projects/CacheLayer
Testing
composer test:code
Or run the full test pipeline:
composer test:all
Contributing
Contributions are welcome.
- Open an issue for bug reports or feature discussions
- Open a pull request with focused changes and tests
- Keep coding style and static checks passing before submitting
- Follow the Code of Conduct
License
MIT License. See LICENSE.