waffle-commons / cache
Cache component for Waffle framework.
Requires
- php: ^8.5
- predis/predis: ^2.4
- waffle-commons/contracts: 0.1.0-beta2.1
Requires (Dev)
- carthage-software/mago: ^1.29
- cyclonedx/cyclonedx-php-composer: ^6.2
- php-mock/php-mock-phpunit: ^2.15
- phpunit/phpunit: ^12.5
- vimeo/psalm: ^6.16
This package is auto-updated.
Last update: 2026-05-30 19:47:05 UTC
README
Waffle Cache Component
Release:
v0.1.0-beta2ย |ยCHANGELOG.mdPSR Compliance: PSR-6 (Psr\Cache) + PSR-16 (Psr\SimpleCache)
PSR-6 and PSR-16 compliant cache implementation for the Waffle Framework. Designed for FrankenPHP resident worker mode โ every adapter is stateless across requests, fail-secure, and zero-baseline under Mago static analysis.
๐ฆ Installation
composer require waffle-commons/cache
๐งฑ Adapters
| Adapter | Use case | Worker scope |
|---|---|---|
ArrayCache |
In-memory, ultra-fast. Lifetime = worker request cycle. | Single worker |
FileCache |
Persistent filesystem cache with strict permission handling (no /tmp foot-guns). |
Per host |
RedisCache |
Distributed cache across multiple FrankenPHP workers via the Predis client. | Cluster-wide |
All adapters implement both Psr\SimpleCache\CacheInterface (PSR-16) and route through the same KeyValidator. CachePool provides the PSR-6 CacheItemPoolInterface view on top of any adapter.
๐ก๏ธ Hardening features
- JSON-only payloads (Beta 1):
FileCacheandRedisCacheserialize entries withjson_encode/json_decodeโ never PHPunserialize. This eliminates the PHP Object Injection โ RCE vector (OWASP A08) that native deserialization exposes in a long-lived worker process. JSON parse failures fail-secure (treated as a cache miss). Trade-off: only JSON-encodable values round-trip โ objects come back asstdClass/arrays. - Stampede protection (
StampedeAwareTrait): probabilistic early expiration to prevent thundering-herd misses under high load. - Strict key validation (
KeyValidator): enforces the PSR-16 key character set; rejects invalid keys withInvalidCacheKeyException. - Stateless adapters: no per-process mutable state โ safe for FrankenPHP worker reuse.
- Fail-secure exceptions:
CacheException,CacheBackendUnavailableException,InvalidCacheKeyExceptionโ all rooted in thewaffle-commons/contractsexception hierarchy.
๐ PHP 8.5 features used
- Typed constants for adapter defaults.
- Asymmetric visibility (
public private(set)) on adapter configuration. - Property hooks for validating constructor inputs at the source.
readonlyclasses for value objects (CacheItem).
๐ Quick start
use Waffle\Commons\Cache\Adapter\ArrayCache; use Waffle\Commons\Cache\Pool\CachePool; $psr16 = new ArrayCache(); $psr16->set('user:42', ['name' => 'Ada'], ttl: 60); $psr6 = new CachePool($psr16); $item = $psr6->getItem('user:42');
Factory-based wiring (preferred in framework code) is provided by CacheFactory.
๐งญ Architectural boundary (mago guard)
An active dependency perimeter is enforced on every CI run by vendor/bin/mago guard (bundled into composer mago; zero baselines). The rules live in mago.toml under [guard.perimeter] โ a forbidden use statement fails the build, not a reviewer.
Production code under Waffle\Commons\Cache may depend only on:
Waffle\Commons\Cache\**โ itselfWaffle\Commons\Contracts\**โ the shared contracts package, the only Waffle dependency permittedPredis\**โ the Predis client behind theRedisCacheadapterPsr\**โ PSR interfaces (PSR-6 / PSR-16)@global+Psl\**โ PHP core and the PHP Standard Library
Test code under WaffleTests\Commons\Cache is unrestricted (@all). Structural rules are guarded too: interfaces must be named *Interface, Exception\** classes must end in *Exception, and any Enum\** namespace may hold only enum declarations.
Contract-first, component-agnostic by construction: components compose through waffle-commons/contracts, never directly through one another.
๐งช Testing
docker exec -w /waffle-commons/cache waffle-dev composer tests
PHPUnit 11+ suite targets >= 95% line coverage.
๐ค Contributing
See CONTRIBUTING.md. All contributions must respect the Mago Purge Protocol (zero baselines) and the FrankenPHP statelessness contract.
๐ License
MIT โ see LICENSE.md.