stougeiro / cache
Performant cache implementation package providing file-based and SQLite-backed handlers. Features optimized I/O with line-based serialization, WAL-mode SQLite, and modular design for PHP applications.
Fund package maintenance!
Requires
- php: >=8.2
- stougeiro/cache-contract: ^1.0
Requires (Dev)
- pestphp/pest: ^3.8
- phpstan/phpstan: ^2.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-14 01:24:51 UTC
README
Cache
A small, extensible cache library for PHP that abstracts storage behind a unified API. It ships with two handlers — FileCache and SQLite — both implementing TTL expiration, safe serialization, and predictable behavior. Swap handlers without touching your application code.
✨ Features
-
Unified API
A single CacheInterface powering all handlers, keeping your application fully decoupled from the underlying storage engine. -
Swappable Handlers
Switch between FileCache and SQLite simply by changing the configuration — no application code needs to be modified. -
TTL-Based Expiration
Each entry automatically expires; stale items are removed on access to keep storage clean and predictable. -
Safe Serialization
Corrupted or unreadable values are safely discarded, preventing exceptions and inconsistent cache states. -
File Sharding
The file-based handler distributes cache entries into MD5‑based subdirectories, improving lookup performance and reducing filesystem contention. -
Graceful Degradation
Read/write failures or corrupted entries fall back to default values without breaking application flow. -
Extensible Architecture
Implement new handlers viaCacheHandlerInterfaceand register them inside thecreateHandler()method of theCacheclass.
📦 Installation
Install via Composer:
composer require stougeiro/cache
🚀 Usage Example
Basic Usage
use STDW\Cache\Cache; use STDW\Cache\CacheConfig; $config = [ 'handler' => 'file', // or 'sqlite' 'storage' => __DIR__ . '/cache', ]; $cache = new Cache( new CacheConfig($config)); // Store a value for 5 minutes (300 seconds) $cache->set('username', 'sidney', ttl: 300); // Retrieve it $user = $cache->get('username'); // "sidney" // Check existence if ($cache->has('username')) { echo "Cached!"; } // Delete a single entry $cache->delete('username'); // Clear all cache entries $cache->clear();
🧠 Why?
Because cache libraries should be fast, simple, and extensible without being over-engineered. This package provides two production-ready handlers with different tradeoffs — file-based for simplicity, SQLite for concurrency — behind a single interface that can be swapped without changing application code.
The goal is to offer a cache layer that:
- avoids unnecessary abstractions,
- stays predictable and easy to debug,
- works in any environment (CLI, web, microservices),
- and can be extended with custom storage engines when needed.
🤝 Contributions
Contributions are welcome. Feel free to open issues or submit pull requests.