PHPackages                             ryanhellyer/stale-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. ryanhellyer/stale-cache

ActiveLibrary[Caching](/categories/caching)

ryanhellyer/stale-cache
=======================

A PHP implementation of the stale-while-revalidate caching pattern for WordPress

1.1(1mo ago)2069↓77.8%GPL-2.0-or-laterPHPPHP &gt;=8.2

Since Jan 5Pushed 1y ago2 watchersCompare

[ Source](https://github.com/ryanhellyer/stale-cache)[ Packagist](https://packagist.org/packages/ryanhellyer/stale-cache)[ RSS](/packages/ryanhellyer-stale-cache/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (6)Versions (4)Used By (0)

StaleCache
==========

[](#stalecache)

A PHP implementation of the stale-while-revalidate caching pattern for WordPress, designed to improve performance and reduce load on expensive operations.

Inspired by the recent implementation of `Cache::flexible()` in Laravel.

Features
--------

[](#features)

- **Stale-While-Revalidate Pattern**: Serves stale content while asynchronously refreshing cache in the background
- **Race Condition Prevention**: Uses locking mechanism to prevent multiple simultaneous cache updates
- **WordPress Integration**: Built on WordPress transients for reliable cache storage
- **Async Updates**: Leverages FastCGI finish request for non-blocking cache updates (for when using PHP-FPM)
- **Type Safety**: Written in strict PHP with full type declarations

Installation
------------

[](#installation)

```
composer require ryanhellyer/stale-cache
```

Usage
-----

[](#usage)

Basic usage example:

```
$result = StaleCache::get(
    'my_cache_key',
    [
        5,      // Stale time in seconds
        3600,   // Cache duration in seconds
        60      // Lock duration in seconds (optional)
    ],
    function() {
        // Your expensive operation here
        return getExpensiveData();
    }
);
```

### Configuration Parameters

[](#configuration-parameters)

- **Stale Time**: How long the cache is considered fresh (in seconds)
- **Cache Duration**: Total time to keep the cache (in seconds)
- **Lock Duration**: How long to hold the refresh lock (defaults to 1 hour)

### Cache States

[](#cache-states)

The cache can be in one of three states:

1. **Fresh**: Content is served directly from cache
2. **Stale**: Content is served from cache while a background refresh is triggered
3. **Missing**: Content is generated synchronously and cached

### Performance Considerations

[](#performance-considerations)

- Uses `fastcgi_finish_request()` when available for non-blocking updates
- Implements locking to prevent cache stampede
- Serves stale content rather than blocking on regeneration

Development
-----------

[](#development)

### Code Style &amp; Quality

[](#code-style--quality)

This project follows PSR-12 coding standards and uses several tools to maintain code quality:

#### PHP CodeSniffer

[](#php-codesniffer)

Check coding standards:

```
composer phpcs
```

Automatically fix coding standards violations:

```
composer phpcs-fix
```

#### PHPStan

[](#phpstan)

Run static analysis:

```
composer phpstan
```

The project uses PHPStan Level 8 (maximum) for strict type checking and analysis.

### Testing

[](#testing)

Run the test suite:

```
composer test
```

License
-------

[](#license)

This project is licensed under the GPL v2 license.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance61

Regular maintenance activity

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~479 days

Total

2

Last Release

55d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/430544?v=4)[Ryan Hellyer](/maintainers/ryanhellyer)[@ryanhellyer](https://github.com/ryanhellyer)

---

Top Contributors

[![ryanhellyer](https://avatars.githubusercontent.com/u/430544?v=4)](https://github.com/ryanhellyer "ryanhellyer (21 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ryanhellyer-stale-cache/health.svg)

```
[![Health](https://phpackages.com/badges/ryanhellyer-stale-cache/health.svg)](https://phpackages.com/packages/ryanhellyer-stale-cache)
```

###  Alternatives

[barryvdh/laravel-httpcache

HttpCache for Laravel

502404.4k10](/packages/barryvdh-laravel-httpcache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
