{"id":13534725,"url":"https://github.com/stevegrunwell/wp-cache-remember","last_synced_at":"2025-04-05T20:09:03.943Z","repository":{"id":29443222,"uuid":"121707550","full_name":"stevegrunwell/wp-cache-remember","owner":"stevegrunwell","description":"Helper for the WordPress object cache and transients.","archived":false,"fork":false,"pushed_at":"2022-07-08T18:20:25.000Z","size":46,"stargazers_count":145,"open_issues_count":0,"forks_count":10,"subscribers_count":7,"default_branch":"develop","last_synced_at":"2025-03-29T19:07:26.880Z","etag":null,"topics":["composer-package","wordpress","wordpress-object-cache","wordpress-plugin","wordpress-transients"],"latest_commit_sha":null,"homepage":"https://stevegrunwell.com/blog/micro-libraries-wordpress/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stevegrunwell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-16T02:25:36.000Z","updated_at":"2024-09-29T00:00:53.000Z","dependencies_parsed_at":"2022-07-13T13:40:29.265Z","dependency_job_id":null,"html_url":"https://github.com/stevegrunwell/wp-cache-remember","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fwp-cache-remember","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fwp-cache-remember/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fwp-cache-remember/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stevegrunwell%2Fwp-cache-remember/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stevegrunwell","download_url":"https://codeload.github.com/stevegrunwell/wp-cache-remember/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393572,"owners_count":20931813,"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":["composer-package","wordpress","wordpress-object-cache","wordpress-plugin","wordpress-transients"],"created_at":"2024-08-01T08:00:37.651Z","updated_at":"2025-04-05T20:09:03.902Z","avatar_url":"https://github.com/stevegrunwell.png","language":"PHP","readme":"# WP Cache Remember\n\n[![Build Status](https://travis-ci.org/stevegrunwell/wp-cache-remember.svg?branch=develop)](https://travis-ci.org/stevegrunwell/wp-cache-remember)\n[![Coverage Status](https://coveralls.io/repos/github/stevegrunwell/wp-cache-remember/badge.svg?branch=develop)](https://coveralls.io/github/stevegrunwell/wp-cache-remember?branch=develop)\n[![GitHub release](https://img.shields.io/github/release/stevegrunwell/wp-cache-remember.svg)](https://github.com/stevegrunwell/wp-cache-remember/releases)\n\nWP Cache Remember is a simple WordPress include to introduce convenient new caching functions.\n\nWell-built WordPress plugins know when to take advantage of the object cache and/or transients, but they often end up with code that looks like this:\n\n```php\nfunction do_something() {\n    $cache_key = 'some-cache-key';\n    $cached    = wp_cache_get( $cache_key );\n\n    // Return the cached value.\n    if ( $cached ) {\n        return $cached;\n    }\n\n    // Do all the work to calculate the value.\n    $value = a_whole_lotta_processing();\n\n    // Cache the value.\n    wp_cache_set( $cache_key, $value );\n\n    return $value;\n}\n```\n\nThat pattern works well, but there's a lot of repeated code. This package draws inspiration from [Laravel's `Cache::remember()` method](https://laravel.com/docs/5.6/cache#cache-usage); using `wp_cache_remember()`, the same code from above becomes:\n\n```php\nfunction do_something() {\n    return wp_cache_remember( 'some-cache-key', function () {\n        return a_whole_lotta_processing();\n    } );\n}\n```\n\n## Installation\n\nThe best way to install this package is [via Composer](https://getcomposer.org/):\n\n```sh\n$ composer require stevegrunwell/wp-cache-remember\n```\n\nThe package ships with the [`composer/installers` package](https://github.com/composer/installers), enabling you to control where you'd like the package to be installed. For example, if you're using WP Cache Remember in a WordPress plugin, you might store the file in an `includes/` directory. To accomplish this, add the following to your plugin's `composer.json` file:\n\n```json\n{\n    \"extra\": {\n        \"installer-paths\": {\n            \"includes/{$name}/\": [\"stevegrunwell/wp-cache-remember\"]\n        }\n    }\n}\n```\n\nThen, from within your plugin, simply include or require the file:\n\n```php\nrequire_once __DIR__ . '/includes/wp-cache-remember/wp-cache-remember.php';\n```\n\n### Using as a plugin\n\nIf you'd prefer, the package also includes the necessary file headers to be used as a WordPress plugin.\n\nAfter downloading or cloning the package, move `wp-cache-remember.php` into either your `wp-content/mu-plugins/` (preferred) or `wp-content/plugins/` directory. If you chose the regular plugins directory, you'll need to activate the plugin manually via the Plugins \u0026rsaquo; Installed Plugins page within WP Admin.\n\n### Bundling within a plugin or theme\n\nWP Cache Remember has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially.\n\nEach function declaration is wrapped in appropriate `function_exists()` checks, ensuring that multiple copies of the library can co-exist in the same WordPress environment.\n\n## Usage\n\nWP Cache Remember provides the following functions for WordPress:\n\n* [`wp_cache_remember()`](#wp_cache_remember)\n* [`wp_cache_forget()`](#wp_cache_forget)\n* [`remember_transient()`](#remember_transient)\n* [`forget_transient()`](#forget_transient)\n* [`remember_site_transient()`](#remember_site_transient)\n* [`forget_site_transient()`](#forget_site_transient)\n\nEach function checks the response of the callback for a `WP_Error` object, ensuring you're not caching temporary errors for long periods of time. PHP Exceptions will also not be cached.\n\n### wp_cache_remember()\n\nRetrieve a value from the object cache. If it doesn't exist, run the `$callback` to generate and cache the value.\n\n#### Parameters\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $key\u003c/dt\u003e\n    \u003cdd\u003eThe cache key.\u003c/dd\u003e\n    \u003cdt\u003e(callable) $callback\u003c/dt\u003e\n    \u003cdd\u003eThe callback used to generate and cache the value.\u003c/dd\u003e\n    \u003cdt\u003e(string) $group\u003c/dt\u003e\n    \u003cdd\u003eOptional. The cache group. Default is empty.\u003c/dd\u003e\n    \u003cdt\u003e(int) $expire\u003c/dt\u003e\n    \u003cdd\u003eOptional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\nfunction get_latest_posts() {\n    return wp_cache_remember( 'latest_posts', function () {\n        return new WP_Query( array(\n            'posts_per_page' =\u003e 5,\n            'orderby'        =\u003e 'post_date',\n            'order'          =\u003e 'desc',\n        ) );\n    }, 'my-cache-group', HOUR_IN_SECONDS );\n}\n```\n\n### wp_cache_forget()\n\nRetrieve and subsequently delete a value from the object cache.\n\n#### Parameters\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $key\u003c/dt\u003e\n    \u003cdd\u003eThe cache key.\u003c/dd\u003e\n    \u003cdt\u003e(string) $group\u003c/dt\u003e\n    \u003cdd\u003eOptional. The cache group. Default is empty.\u003c/dd\u003e\n    \u003cdt\u003e(mixed) $default\u003c/dt\u003e\n    \u003cdd\u003eOptional. The default value to return if the given key doesn't exist in the object cache. Default is null.\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\nfunction show_error_message() {\n    $error_message = wp_cache_forget( 'form_errors', 'my-cache-group', false );\n\n    if ( $error_message ) {\n        echo 'An error occurred: ' . $error_message;\n    }\n}\n```\n\n### remember_transient()\n\nRetrieve a value from transients. If it doesn't exist, run the `$callback` to generate and cache the value.\n\n#### Parameters\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $key\u003c/dt\u003e\n    \u003cdd\u003eThe cache key.\u003c/dd\u003e\n    \u003cdt\u003e(callable) $callback\u003c/dt\u003e\n    \u003cdd\u003eThe callback used to generate and cache the value.\u003c/dd\u003e\n    \u003cdt\u003e(int) $expire\u003c/dt\u003e\n    \u003cdd\u003eOptional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).\u003c/dd\u003e\n\u003c/dl\u003e\n\n#### Example\n\n```php\nfunction get_tweets() {\n    $user_id = get_current_user_id();\n    $key     = 'latest_tweets_' . $user_id;\n\n    return remember_transient( $key, function () use ( $user_id ) {\n        return get_latest_tweets_for_user( $user_id );\n    }, 15 * MINUTE_IN_SECONDS );\n}\n```\n\n### forget_transient()\n\nRetrieve and subsequently delete a value from the transient cache.\n\n#### Parameters\n\n\u003cdl\u003e\n    \u003cdt\u003e(string) $key\u003c/dt\u003e\n    \u003cdd\u003eThe cache key.\u003c/dd\u003e\n    \u003cdt\u003e(mixed) $default\u003c/dt\u003e\n    \u003cdd\u003eOptional. The default value to return if the given key doesn't exist in transients. Default is null.\u003c/dd\u003e\n\u003c/dl\u003e\n\n### remember_site_transient()\n\nRetrieve a value from site transients. If it doesn't exist, run the `$callback` to generate and cache the value.\n\nThis function shares arguments and behavior with [`remember_transient()`](#remember_transient), but works network-wide when using WordPress Multisite.\n\n### forget_site_transient()\n\nRetrieve and subsequently delete a value from the site transient cache.\n\nThis function shares arguments and behavior with [`forget_transient()`](#forget_transient), but works network-wide when using WordPress Multisite.\n\n## License\n\nCopyright 2018 Steve Grunwell\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["Caching Helping Plugins"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegrunwell%2Fwp-cache-remember","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstevegrunwell%2Fwp-cache-remember","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstevegrunwell%2Fwp-cache-remember/lists"}