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. ryanhellyer/stale-cache

ActiveLibrary

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

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

1.0(1y ago)2061—0%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 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)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

33

—

LowBetter than 75% of packages

Maintenance42

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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

Unknown

Total

1

Last Release

490d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/344653ee76e4fc90cb406062cd8a6fc04977571f5eb09af7ae58736249d65f98?d=identicon)[ryanhellyer](/maintainers/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)
```

PHPackages © 2026

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