├── CHANGELOG.md ├── LICENSE.txt ├── README.md └── wp-cache-remember.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 | ## [1.1.2] - 2022-05-26 9 | 10 | * Update PHP compatiblity checks ([#6], props @jrfnl) 11 | * Update test builds ([#6], props @jrfnl) 12 | * Allow recent versions of [composer/installers](https://github.com/composer/installers) and [dealerdirect/phpcodesniffer-composer-installer](https://github.com/PHPCSStandards/composer-installer) packages ([#7], props @macbookandrew) 13 | 14 | ## [1.1.1] - 2018-06-14 15 | 16 | * Lower the minimum version of [composer/installers](https://github.com/composer/installers) to `^1.0` ([#2], props @aaemnnosttv). 17 | 18 | ## [1.1.0] - 2018-04-14 19 | 20 | * Bypass the caching operation if a callback either throws an Exception or returns a `WP_Error` object ([#1]). 21 | * Add a formal changelog to the project, following the [Keep a Changelog standard](http://keepachangelog.com/en/1.0.0/). 22 | 23 | ## [1.0.0] - 2018-02-16 24 | 25 | Initial public release of the package, including the following functions: 26 | 27 | * `wp_cache_remember()` 28 | * `wp_cache_forget()` 29 | * `remember_transient()` 30 | * `forget_transient()` 31 | * `remember_site_transient()` 32 | * `forget_site_transient()` 33 | 34 | [Unreleased]: https://github.com/stevegrunwell/wp-cache-remember/compare/master...develop 35 | [1.1.1]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.1.1 36 | [1.1.0]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.1.0 37 | [1.0.0]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.0.0 38 | [#1]: https://github.com/stevegrunwell/wp-cache-remember/pull/1 39 | [#2]: https://github.com/stevegrunwell/wp-cache-remember/issues/2 40 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2018 Steve Grunwell 2 | 3 | Permission 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WP Cache Remember 2 | 3 | [](https://travis-ci.org/stevegrunwell/wp-cache-remember) 4 | [](https://coveralls.io/github/stevegrunwell/wp-cache-remember?branch=develop) 5 | [](https://github.com/stevegrunwell/wp-cache-remember/releases) 6 | 7 | WP Cache Remember is a simple WordPress include to introduce convenient new caching functions. 8 | 9 | Well-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: 10 | 11 | ```php 12 | function do_something() { 13 | $cache_key = 'some-cache-key'; 14 | $cached = wp_cache_get( $cache_key ); 15 | 16 | // Return the cached value. 17 | if ( $cached ) { 18 | return $cached; 19 | } 20 | 21 | // Do all the work to calculate the value. 22 | $value = a_whole_lotta_processing(); 23 | 24 | // Cache the value. 25 | wp_cache_set( $cache_key, $value ); 26 | 27 | return $value; 28 | } 29 | ``` 30 | 31 | That 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: 32 | 33 | ```php 34 | function do_something() { 35 | return wp_cache_remember( 'some-cache-key', function () { 36 | return a_whole_lotta_processing(); 37 | } ); 38 | } 39 | ``` 40 | 41 | ## Installation 42 | 43 | The best way to install this package is [via Composer](https://getcomposer.org/): 44 | 45 | ```sh 46 | $ composer require stevegrunwell/wp-cache-remember 47 | ``` 48 | 49 | The 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: 50 | 51 | ```json 52 | { 53 | "extra": { 54 | "installer-paths": { 55 | "includes/{$name}/": ["stevegrunwell/wp-cache-remember"] 56 | } 57 | } 58 | } 59 | ``` 60 | 61 | Then, from within your plugin, simply include or require the file: 62 | 63 | ```php 64 | require_once __DIR__ . '/includes/wp-cache-remember/wp-cache-remember.php'; 65 | ``` 66 | 67 | ### Using as a plugin 68 | 69 | If you'd prefer, the package also includes the necessary file headers to be used as a WordPress plugin. 70 | 71 | After 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 › Installed Plugins page within WP Admin. 72 | 73 | ### Bundling within a plugin or theme 74 | 75 | WP Cache Remember has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially. 76 | 77 | Each function declaration is wrapped in appropriate `function_exists()` checks, ensuring that multiple copies of the library can co-exist in the same WordPress environment. 78 | 79 | ## Usage 80 | 81 | WP Cache Remember provides the following functions for WordPress: 82 | 83 | * [`wp_cache_remember()`](#wp_cache_remember) 84 | * [`wp_cache_forget()`](#wp_cache_forget) 85 | * [`remember_transient()`](#remember_transient) 86 | * [`forget_transient()`](#forget_transient) 87 | * [`remember_site_transient()`](#remember_site_transient) 88 | * [`forget_site_transient()`](#forget_site_transient) 89 | 90 | Each 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. 91 | 92 | ### wp_cache_remember() 93 | 94 | Retrieve a value from the object cache. If it doesn't exist, run the `$callback` to generate and cache the value. 95 | 96 | #### Parameters 97 | 98 |