PHPackages                             alleyinteractive/cache-collector - 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. alleyinteractive/cache-collector

ActiveWordpress-plugin

alleyinteractive/cache-collector
================================

Dynamic cache key collector for easy purging

v1.1.0(1y ago)319[3 PRs](https://github.com/alleyinteractive/cache-collector/pulls)GPL-2.0-or-laterPHPPHP ^8.1

Since Nov 9Pushed 1y ago21 watchersCompare

[ Source](https://github.com/alleyinteractive/cache-collector)[ Packagist](https://packagist.org/packages/alleyinteractive/cache-collector)[ Docs](https://github.com/alleyinteractive/cache-collector)[ RSS](/packages/alleyinteractive-cache-collector/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (3)Dependencies (5)Versions (10)Used By (0)

Cache Collector
===============

[](#cache-collector)

[![Coding Standards](https://github.com/alleyinteractive/cache-collector/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/alleyinteractive/cache-collector/actions/workflows/coding-standards.yml)[![Testing Suite](https://github.com/alleyinteractive/cache-collector/actions/workflows/unit-test.yml/badge.svg)](https://github.com/alleyinteractive/cache-collector/actions/workflows/unit-test.yml)

Dynamic cache key collector for easy purging.

Background
----------

[](#background)

One common problem with large WordPress sites that utilize Memcache is the problems that arise when trying to purge cache keys that are dynamically generated. For example, if a cache key is the hash of a remote request. You would need to calculate the hashed cache key to properly purge it from the cache. Another common problem would be trying to purge all the cache keys in a specific group (Memcache doesn't support group purging).

Cache Collector solves this by storing cache/transient keys in collections. These collections can then be purged in a single command. Here's a real-world use case:

When viewing a post, the post's related posts are fetched from a remote source and displayed to the user. This operation is expensive due to the remote request and needs to be cached. When the post is updated, the related post cache needs to be flushed as well.

Enter Cache Collector. When the post is updated, the related post cache key is added to a collection. When the post is updated, the cache key that is connected to the post will automatically be purged.

To flip this around, say the remote data source is having issues and you need to flush all the related post cache keys. You can do this by purging the "related posts" cache collection. This stores all the cache keys for all related posts. In one command you can purge an entire cache group without having to calculate the cache key for each.

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

[](#installation)

You can install the package via composer:

```
composer require alleyinteractive/cache-collector
```

Usage
-----

[](#usage)

Activate the plugin in WordPress and use the below methods to interface with the cache collector.

### Registering Keys

[](#registering-keys)

**One important note when registering cache keys:** registering a key should only be done when the cache/transient is stored. When the key is stored the system will set an expiration date for the key to be eventually purged from the collection if unused. To prevent continually updating the keys in the collection and degrading site performance, the key should only be registered when the cache/transient is stored. The cache collector will eventually remove the key from the collection when it expires.

TL;DR: Register the key only when the cache/transient is stored. Don't register it on every page load.

### Register a Key in a Cache Collection

[](#register-a-key-in-a-cache-collection)

```
cache_collector_register_key( string $collection, string $key, string $group = '', int $ttl = 0, string $type = 'cache' );

// Example using named arguments.
cache_collector_register_key(
	collection: 'related_posts',
	key: $related_posts_cache_key,
	group: 'related_posts',
	ttl: 3600,
	type: 'cache',
);
```

The plugin also provides `cache_collector_register_transient_key()` and `cache_collector_register_cache_key()` to make it easier to register keys for a transient/cache without having to specify the `$type` argument.

```
cache_collector_register_transient_key( string $collection, string $transient, int $ttl = 0 );
cache_collector_register_cache_key( string $collection, string $key, string $group = '', int $ttl = 0 );
```

### Purging a Cache Collection

[](#purging-a-cache-collection)

Purge all the cache entries in a collection.

```
cache_collector_purge( string $collection );
```

### Registering a Key Related to a Post

[](#registering-a-key-related-to-a-post)

A post cache collection is a collection of cache keys related to a post. When a post is updated, the post's cache collection is automatically purged. This allows you to purge all of the cache keys related to a post at once.

```
cache_collector_register_post_key( \WP_Post|int $post, string $key, string $group = '', string $type = 'cache' );

// Example using named arguments.
cache_collector_register_post_key(
	post: $post,
	key: $related_posts_cache_key,
	group: 'related_posts',
	type: 'cache',
);
```

### Purging a Post's Cache Collection

[](#purging-a-posts-cache-collection)

Purge a cache collection related to a post.

```
cache_collector_purge_post( \WP_Post|int $post_id );
```

### Registering a Key Related to a Term

[](#registering-a-key-related-to-a-term)

```
cache_collector_register_term_key( \WP_Term|int $term, string $key, string $group = '', string $type = 'cache' );

// Example using named arguments.
cache_collector_register_term_key(
	term: $term,
	key: $related_posts_cache_key,
	group: 'related_posts',
	type: 'cache',
);
```

### Purging a Term's Cache Collection

[](#purging-a-terms-cache-collection)

```
cache_collector_purge_term( \WP_Term|int $term );
```

Full Example
------------

[](#full-example)

The following example shows how to use the cache collector to register a cache key for future purging.

```
$arguments = [
	'limit' => 100,
	'term' => '...',
	'fields' => [ ... ],
];

$data = wp_cache_get( md5( $arguments ), 'my_cache_group' );

if ( false === $data ) {
	$data = remote_data_fetch( $arguments );
	wp_cache_set( md5( $arguments ), $data, 'my_cache_group' );

	// Register the cache key for the collection.
	cache_collector_register_cache_key( 'my_collection', md5( $arguments ), 'my_cache_group' );
}
```

Now we can purge the cache collection whenever we need to:

```
cache_collector_purge( 'my_collection' );
```

We can also purge this with the WP-CLI command included with the plugin:

```
wp cache-collector purge my_collection
```

Testing
-------

[](#testing)

Run `composer test` to run tests against PHPUnit and the PHP code in the plugin.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

This project is actively maintained by [Alley Interactive](https://github.com/alleyinteractive). Like what you see? [Come work with us](https://alley.co/careers/).

- [Sean Fisher](https://github.com/srtfisher)
- [All Contributors](../../contributors)

License
-------

[](#license)

The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 83.1% 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 ~297 days

Total

3

Last Release

662d ago

PHP version history (2 changes)v1.0.0PHP ^8.0

v1.1.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/338d27065b1074f2d66d049d742f22996dd137eef6f91bc8f75350ceee1e8ef2?d=identicon)[srtfisher](/maintainers/srtfisher)

---

Top Contributors

[![srtfisher](https://avatars.githubusercontent.com/u/346399?v=4)](https://github.com/srtfisher "srtfisher (59 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (6 commits)")

---

Tags

wordpresswordpress-pluginalleyinteractivecache-collector

### Embed Badge

![Health badge](/badges/alleyinteractive-cache-collector/health.svg)

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

###  Alternatives

[alleyinteractive/wp-block-converter

Convert HTML into Gutenberg Blocks with PHP

62321.0k1](/packages/alleyinteractive-wp-block-converter)[alleyinteractive/wp-404-caching

Full Page Cache for WordPress 404s

12260.3k](/packages/alleyinteractive-wp-404-caching)[alleyinteractive/wp-curate

Plugin to curate homepages and other landing pages

10154.3k](/packages/alleyinteractive-wp-curate)[alleyinteractive/wp-rest-api-guard

Restrict and control access to the REST API

134.2k](/packages/alleyinteractive-wp-rest-api-guard)[alleyinteractive/feed-consumer

Ingest external feeds and other data sources into WordPress

114.8k](/packages/alleyinteractive-feed-consumer)

PHPackages © 2026

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