{"id":19107796,"url":"https://github.com/alleyinteractive/cache-collector","last_synced_at":"2025-07-17T07:04:39.216Z","repository":{"id":63320918,"uuid":"558543462","full_name":"alleyinteractive/cache-collector","owner":"alleyinteractive","description":"Dynamic cache key collector for easy purging","archived":false,"fork":false,"pushed_at":"2024-07-18T20:33:12.000Z","size":291,"stargazers_count":3,"open_issues_count":4,"forks_count":0,"subscribers_count":22,"default_branch":"develop","last_synced_at":"2025-06-22T01:48:42.673Z","etag":null,"topics":["wordpress","wordpress-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alleyinteractive.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-27T19:03:22.000Z","updated_at":"2024-07-18T20:32:54.000Z","dependencies_parsed_at":"2024-07-19T01:39:17.479Z","dependency_job_id":null,"html_url":"https://github.com/alleyinteractive/cache-collector","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":"alleyinteractive/create-wordpress-plugin","purl":"pkg:github/alleyinteractive/cache-collector","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alleyinteractive%2Fcache-collector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alleyinteractive%2Fcache-collector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alleyinteractive%2Fcache-collector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alleyinteractive%2Fcache-collector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alleyinteractive","download_url":"https://codeload.github.com/alleyinteractive/cache-collector/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alleyinteractive%2Fcache-collector/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265575476,"owners_count":23790776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["wordpress","wordpress-plugin"],"created_at":"2024-11-09T04:14:01.041Z","updated_at":"2025-07-17T07:04:39.195Z","avatar_url":"https://github.com/alleyinteractive.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cache Collector\n\n[![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)\n[![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)\n\nDynamic cache key collector for easy purging.\n\n## Background\n\nOne common problem with large WordPress sites that utilize Memcache is the\nproblems that arise when trying to purge cache keys that are dynamically\ngenerated. For example, if a cache key is the hash of a remote request. You\nwould need to calculate the hashed cache key to properly purge it from the\ncache. Another common problem would be trying to purge all the cache keys in a\nspecific group (Memcache doesn't support group purging).\n\nCache Collector solves this by storing cache/transient keys in collections.\nThese collections can then be purged in a single command. Here's a real-world\nuse case:\n\nWhen viewing a post, the post's related posts are fetched from a remote source\nand displayed to the user. This operation is expensive due to the remote request\nand needs to be cached. When the post is updated, the related post cache needs\nto be flushed as well.\n\nEnter Cache Collector. When the post is updated, the related post cache key is\nadded to a collection. When the post is updated, the cache key that is connected\nto the post will automatically be purged.\n\nTo flip this around, say the remote data source is having issues and you need to\nflush all the related post cache keys. You can do this by purging the \"related\nposts\" cache collection. This stores all the cache keys for all related posts.\nIn one command you can purge an entire cache group without having to calculate\nthe cache key for each.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require alleyinteractive/cache-collector\n```\n\n## Usage\n\nActivate the plugin in WordPress and use the below methods to interface with the\ncache collector.\n\n### Registering Keys\n\n**One important note when registering cache keys:** registering a key should\nonly be done when the cache/transient is stored. When the key is stored the\nsystem will set an expiration date for the key to be eventually purged from the\ncollection if unused. To prevent continually updating the keys in the collection\nand degrading site performance, the key should only be registered when the\ncache/transient is stored. The cache collector will eventually remove the key from\nthe collection when it expires.\n\nTL;DR: Register the key only when the cache/transient is stored. Don't register\nit on every page load.\n\n### Register a Key in a Cache Collection\n\n```php\ncache_collector_register_key( string $collection, string $key, string $group = '', int $ttl = 0, string $type = 'cache' );\n\n// Example using named arguments.\ncache_collector_register_key(\n\tcollection: 'related_posts',\n\tkey: $related_posts_cache_key,\n\tgroup: 'related_posts',\n\tttl: 3600,\n\ttype: 'cache',\n);\n```\n\nThe plugin also provides `cache_collector_register_transient_key()` and\n`cache_collector_register_cache_key()` to make it easier to register keys for a\ntransient/cache without having to specify the `$type` argument.\n\n```php\ncache_collector_register_transient_key( string $collection, string $transient, int $ttl = 0 );\ncache_collector_register_cache_key( string $collection, string $key, string $group = '', int $ttl = 0 );\n```\n\n### Purging a Cache Collection\n\nPurge all the cache entries in a collection.\n\n```php\ncache_collector_purge( string $collection );\n```\n\n### Registering a Key Related to a Post\n\nA post cache collection is a collection of cache keys related to a post. When a\npost is updated, the post's cache collection is automatically purged. This\nallows you to purge all of the cache keys related to a post at once.\n\n```php\ncache_collector_register_post_key( \\WP_Post|int $post, string $key, string $group = '', string $type = 'cache' );\n\n// Example using named arguments.\ncache_collector_register_post_key(\n\tpost: $post,\n\tkey: $related_posts_cache_key,\n\tgroup: 'related_posts',\n\ttype: 'cache',\n);\n```\n\n### Purging a Post's Cache Collection\n\nPurge a cache collection related to a post.\n\n```php\ncache_collector_purge_post( \\WP_Post|int $post_id );\n```\n\n### Registering a Key Related to a Term\n\n```php\ncache_collector_register_term_key( \\WP_Term|int $term, string $key, string $group = '', string $type = 'cache' );\n\n// Example using named arguments.\ncache_collector_register_term_key(\n\tterm: $term,\n\tkey: $related_posts_cache_key,\n\tgroup: 'related_posts',\n\ttype: 'cache',\n);\n```\n\n### Purging a Term's Cache Collection\n\n```php\ncache_collector_purge_term( \\WP_Term|int $term );\n```\n\n## Full Example\n\nThe following example shows how to use the cache collector to register a cache\nkey for future purging.\n\n```php\n$arguments = [\n\t'limit' =\u003e 100,\n\t'term' =\u003e '...',\n\t'fields' =\u003e [ ... ],\n];\n\n$data = wp_cache_get( md5( $arguments ), 'my_cache_group' );\n\nif ( false === $data ) {\n\t$data = remote_data_fetch( $arguments );\n\twp_cache_set( md5( $arguments ), $data, 'my_cache_group' );\n\n\t// Register the cache key for the collection.\n\tcache_collector_register_cache_key( 'my_collection', md5( $arguments ), 'my_cache_group' );\n}\n```\n\nNow we can purge the cache collection whenever we need to:\n\n```php\ncache_collector_purge( 'my_collection' );\n```\n\nWe can also purge this with the WP-CLI command included with the plugin:\n\n```bash\nwp cache-collector purge my_collection\n```\n\n## Testing\n\nRun `composer test` to run tests against PHPUnit and the PHP code in the plugin.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Credits\n\nThis project is actively maintained by [Alley\nInteractive](https://github.com/alleyinteractive). Like what you see? [Come work\nwith us](https://alley.co/careers/).\n\n- [Sean Fisher](https://github.com/srtfisher)\n- [All Contributors](../../contributors)\n\n## License\n\nThe GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falleyinteractive%2Fcache-collector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falleyinteractive%2Fcache-collector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falleyinteractive%2Fcache-collector/lists"}