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)
- captainhook/captainhook: ^5.29.2
- laravel/pint: ^1.29
- pestphp/pest: ^4.5
- pestphp/pest-plugin-drift: ^4.1
- phpbench/phpbench: ^1.6
- phpstan/phpstan: ^2.1
- rector/rector: ^2.4.1
- squizlabs/php_codesniffer: ^4.0.1
- symfony/var-dumper: ^7.3 || ^8.0.8
- tomasvotruba/cognitive-complexity: ^1.1
- vimeo/psalm: ^6.16.1
Suggests
- ext-apcu: For APCu-based caching (in-memory, per-process)
- ext-mbstring: Recommended for development tools output formatting (Pest/Termwind)
- ext-memcached: For Memcached-based caching (distributed, RAM)
- 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
- aws/aws-sdk-php: For DynamoDB and S3 adapters
- mongodb/mongodb: For MongoDB caching via MongoDbCacheAdapter
This package is auto-updated.
Last update: 2026-04-13 04:49:53 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, Redis Cluster, PDO (SQLite default), Shared Memory, MongoDB, DynamoDB, and S3
- 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/mongodbaws/aws-sdk-php
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();
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
License
MIT License. See LICENSE.