项目作者: stevegrunwell

项目描述 :
Helper for the WordPress object cache and transients.
高级语言: PHP
项目地址: git://github.com/stevegrunwell/wp-cache-remember.git
创建时间: 2018-02-16T02:25:36Z
项目社区:https://github.com/stevegrunwell/wp-cache-remember

开源协议:MIT License

下载


WP Cache Remember

Build Status
Coverage Status
GitHub release

WP Cache Remember is a simple WordPress include to introduce convenient new caching functions.

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:

  1. function do_something() {
  2. $cache_key = 'some-cache-key';
  3. $cached = wp_cache_get( $cache_key );
  4. // Return the cached value.
  5. if ( $cached ) {
  6. return $cached;
  7. }
  8. // Do all the work to calculate the value.
  9. $value = a_whole_lotta_processing();
  10. // Cache the value.
  11. wp_cache_set( $cache_key, $value );
  12. return $value;
  13. }

That pattern works well, but there’s a lot of repeated code. This package draws inspiration from Laravel’s Cache::remember() method; using wp_cache_remember(), the same code from above becomes:

  1. function do_something() {
  2. return wp_cache_remember( 'some-cache-key', function () {
  3. return a_whole_lotta_processing();
  4. } );
  5. }

Installation

The best way to install this package is via Composer:

  1. $ composer require stevegrunwell/wp-cache-remember

The package ships with the composer/installers package, 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:

  1. {
  2. "extra": {
  3. "installer-paths": {
  4. "includes/{$name}/": ["stevegrunwell/wp-cache-remember"]
  5. }
  6. }
  7. }

Then, from within your plugin, simply include or require the file:

  1. require_once __DIR__ . '/includes/wp-cache-remember/wp-cache-remember.php';

Using as a plugin

If you’d prefer, the package also includes the necessary file headers to be used as a WordPress plugin.

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.

Bundling within a plugin or theme

WP Cache Remember has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially.

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.

Usage

WP Cache Remember provides the following functions for WordPress:

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.

wp_cache_remember()

Retrieve a value from the object cache. If it doesn’t exist, run the $callback to generate and cache the value.

Parameters


(string) $key

The cache key.

(callable) $callback

The callback used to generate and cache the value.

(string) $group

Optional. The cache group. Default is empty.

(int) $expire

Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).

Example

  1. function get_latest_posts() {
  2. return wp_cache_remember( 'latest_posts', function () {
  3. return new WP_Query( array(
  4. 'posts_per_page' => 5,
  5. 'orderby' => 'post_date',
  6. 'order' => 'desc',
  7. ) );
  8. }, 'my-cache-group', HOUR_IN_SECONDS );
  9. }

wp_cache_forget()

Retrieve and subsequently delete a value from the object cache.

Parameters


(string) $key

The cache key.

(string) $group

Optional. The cache group. Default is empty.

(mixed) $default

Optional. The default value to return if the given key doesn’t exist in the object cache. Default is null.

Example

  1. function show_error_message() {
  2. $error_message = wp_cache_forget( 'form_errors', 'my-cache-group', false );
  3. if ( $error_message ) {
  4. echo 'An error occurred: ' . $error_message;
  5. }
  6. }

remember_transient()

Retrieve a value from transients. If it doesn’t exist, run the $callback to generate and cache the value.

Parameters


(string) $key

The cache key.

(callable) $callback

The callback used to generate and cache the value.

(int) $expire

Optional. The number of seconds before the cache entry should expire. Default is 0 (as long as possible).

Example

  1. function get_tweets() {
  2. $user_id = get_current_user_id();
  3. $key = 'latest_tweets_' . $user_id;
  4. return remember_transient( $key, function () use ( $user_id ) {
  5. return get_latest_tweets_for_user( $user_id );
  6. }, 15 * MINUTE_IN_SECONDS );
  7. }

forget_transient()

Retrieve and subsequently delete a value from the transient cache.

Parameters


(string) $key

The cache key.

(mixed) $default

Optional. The default value to return if the given key doesn’t exist in transients. Default is null.

remember_site_transient()

Retrieve a value from site transients. If it doesn’t exist, run the $callback to generate and cache the value.

This function shares arguments and behavior with remember_transient(), but works network-wide when using WordPress Multisite.

forget_site_transient()

Retrieve and subsequently delete a value from the site transient cache.

This function shares arguments and behavior with forget_transient(), but works network-wide when using WordPress Multisite.

License

Copyright 2018 Steve Grunwell

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.