About the Library
Stale Cache brings modern caching patterns to WordPress. Inspired by Laravel's cache system, it provides a clean, testable abstraction over WordPress transients with support for stale-while-revalidate, cache stampede protection, and multiple backends.
Unlike WordPress transients, Stale Cache serves stale data while regenerating fresh content in the background — your users never wait for a cache miss.
Core Concepts
- Stale-while-revalidate: Serve cached content immediately while asynchronously refreshing in the background.
- Cache stampede protection: Probabilistic early expiration prevents thundering herds when a popular key expires.
- Multiple backends: Works with WordPress transients, Redis, Memcached, or file-based caching.
Usage
The API mirrors Laravel's cache facade, making it instantly familiar to any Laravel developer working in WordPress:
use RyanHellyer\StaleCache\StaleCache;
$data = StaleCache::get(
'my_cache_key',
[5, 3600], // [stale_time, cache_duration, lock_duration (optional)]
function () {
return get_expensive_data();
}
);
Installation
Install via Composer
composer require ryanhellyer/stale-cache