{"id":29150213,"url":"https://github.com/humanmade/hm-swr-cache","last_synced_at":"2026-05-19T03:13:07.910Z","repository":{"id":243680846,"uuid":"811880553","full_name":"humanmade/hm-swr-cache","owner":"humanmade","description":"Soft expiry cache plugin","archived":false,"fork":false,"pushed_at":"2024-09-27T15:08:13.000Z","size":40,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-06-30T16:14:52.616Z","etag":null,"topics":["background-updating","cache-expiration","caching-strategy","soft-expiring-cache","stale-data","stale-while-revalidate","wordpress","wordpress-plugin"],"latest_commit_sha":null,"homepage":"https://github.com/humanmade/hm-swr-cache","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/humanmade.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-06-07T13:48:08.000Z","updated_at":"2024-10-24T14:07:20.000Z","dependencies_parsed_at":"2024-06-10T16:33:39.579Z","dependency_job_id":"e3f758f6-7e4c-413f-aea9-0c1a98efb876","html_url":"https://github.com/humanmade/hm-swr-cache","commit_stats":null,"previous_names":["humanmade/hm-swr-cache"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/humanmade/hm-swr-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-swr-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-swr-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-swr-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-swr-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/humanmade","download_url":"https://codeload.github.com/humanmade/hm-swr-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/humanmade%2Fhm-swr-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262864258,"owners_count":23376461,"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":["background-updating","cache-expiration","caching-strategy","soft-expiring-cache","stale-data","stale-while-revalidate","wordpress","wordpress-plugin"],"created_at":"2025-06-30T23:09:50.847Z","updated_at":"2026-05-19T03:13:02.877Z","avatar_url":"https://github.com/humanmade.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HM SWR Cache\n\nA soft expiring cache with background updating is a specialized caching strategy that allows cached data to remain available even after the expiration period. With regular caching, data becomes unavailable or leads to a 'cache miss' as soon as it expires. This can be costly, particularly with complex data fetching operations that require significant time.\n\nIn a soft expiring cache, when the cache period expires, the cached data remains accessible while a background update operation occurs. During this time, the user or application continues to see the stale cached data until the new updated data is ready. This approach ensures data is always available for end consumers with minimized latency from cache rebuild, providing a continuously smooth experience. This is also called a `stale-while-revalidate` cache strategy, thus the name.\n\nNote: As the cache is updated via a `wp_schedule_single_event()` call, using a scalable job system such as \n[Cavalcade](https://github.com/humanmade/Cavalcade) is recommended as the [default WordPress pseudo-cron](https://developer.wordpress.org/plugins/cron/) slows\nthe page load running the scheduled event.\n\n## Requirements\n\n* PHP 8.1+ is supported (technically it might run on PHP 7.1+).\n* WordPress 6.4.2+ is supported (technically it might run on older versions, your experience is welcome).\n\n## Installation\n\nJust install the plugin like any other plugin. Installation as a MU plugin is supported as well, and via composer\n\n```shell\ncomposer config repositories.hm-swr-cache vcs git@github.com:humanmade/hm-swr-cache.git\ncomposer require humanmade/hm-swr-cache:^0.3 --no-update\ncomposer update humanmade/hm-swr-cache --prefer-source\n```\n\nThese commands configure Composer to use a custom Git repository for the `hm-swr-cache` package, add this package as a requirement without immediately installing it, and finally update and install the package from the source repository.\n\n## Usage\n\nAll that is needed is for you to create a callback function that will be called with `$callback_args` as the first \nparameter by a scheduled task that is triggered when the cache expires after `$cache_expiry` seconds.\n\nThe following code goes where you need to use the cached data. On the first request the cache will be empty and \nscheduled to be generated, so your implementation needs handle that event. After the first generation, there will either be \nstale or current data to display:\n\n```php\n// false is returned if the data isn't in the cache yet\n$data = SwrCache\\get(\n\t$cache_key,\n\t$cache_group,\n\t__NAMESPACE__ . '\\\\my_callback',\n\t$callback_args,\n\t$cache_expiry\n);\n\nif ( ! $data) {\n\t// handle initial empty cache load\n}\n```\n\nThe callback is what you need to implement to generate the cacheable data, an array in the example below. \nYou must return a `WP_Error` object in case of issues, this will generate an RuntimeException in the cron event, \nresulting in a cron log entry. \n```php\nfunction my_callback( array $args ) : WP_Error|array {\n\t// Example validation handling and cache generation.\n\tif ( ! isset( $args['url'] ) ) {\n\t\treturn new WP_Error( 'missing_arg', 'url' );\n\t}\n\t$data = example_api_request( $args );\n\tif ( failed_validation( $data ) ) {\n\t\treturn new WP_Error( 'invalid_data', 'Data failed validation', $data );\n\t}\n\treturn $data;\n}\n\n```\n\nInternally a self-expiring and self-managed lock system ensures the callback is just processed once, avoiding race \nconditions. If, for whatever reason (for example due to a fatal error) the callback doesn't complete, then the lock self-expires in 1 minute.\n\n## Flushing the cache\n\nEngineers can clear the cache as well as any locks by calling the `cache_delete_group( $cache_group )` function, \nwhich might be useful after saving a settings page for example. The cachegroup should first be \nregistrered for flushing using `register_cache_group( $cache_group )` before it can be flushed.\n\n## Difference with other plugins\n\n1. [WP-TLC-Transients](https://github.com/markjaquith/WP-TLC-Transients) In contrast to _WP-TLC-Transients_, _HM Swr Cache_  is not hitting  the database in  any circumstance as  both the lock, cached data and cache expiry are all stored using `wp_cache` functions. The syntax is also quite different.\n\n\nI think that's it!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhumanmade%2Fhm-swr-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhumanmade%2Fhm-swr-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhumanmade%2Fhm-swr-cache/lists"}