diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..780e422 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [1.1.0] - 2018-04-14 + +* Bypass the caching operation if a callback either throws an Exception or returns a `WP_Error` object ([#1]). +* Add a formal changelog to the project, following the [Keep a Changelog standard](http://keepachangelog.com/en/1.0.0/). + +## [1.0.0] - 2018-02-16 + +Initial public release of the package, including the following functions: + +* `wp_cache_remember()` +* `wp_cache_forget()` +* `remember_transient()` +* `forget_transient()` +* `remember_site_transient()` +* `forget_site_transient()` + +[Unreleased]: https://github.com/stevegrunwell/wp-cache-remember/compare/master...develop +[1.1.0]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.1.0 +[1.0.0]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.0.0 +[#1]: https://github.com/stevegrunwell/wp-cache-remember/pull/1 diff --git a/README.md b/README.md index 49f8bf5..d409375 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Build Status](https://travis-ci.org/stevegrunwell/wp-cache-remember.svg?branch=develop)](https://travis-ci.org/stevegrunwell/wp-cache-remember) [![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) +[![GitHub release](https://img.shields.io/github/release/stevegrunwell/wp-cache-remember.svg)](https://github.com/stevegrunwell/wp-cache-remember/releases) WP Cache Remember is a simple WordPress include to introduce convenient new caching functions. @@ -86,6 +87,8 @@ WP Cache Remember provides the following functions for WordPress: * [`remember_site_transient()`](#remember_site_transient) * [`forget_site_transient()`](#forget_site_transient) +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. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 681c239..b8fc0d6 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,7 +2,7 @@ /** * PHPUnit bootstrap file * - * @package Wp_Cache_Remember + * @package SteveGrunwell\WPCacheRemember */ $_tests_dir = getenv( 'WP_TESTS_DIR' ); diff --git a/tests/test-object-cache.php b/tests/test-object-cache.php index 42b85aa..3b32473 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -2,7 +2,7 @@ /** * Tests for the object cache functions. * - * @package Wp_Cache_Remember + * @package SteveGrunwell\WPCacheRemember */ /** @@ -26,6 +26,32 @@ function test_remembers_value() { $this->assertEquals( $value, wp_cache_get( $key ) ); } + function test_does_not_cache_exceptions() { + $key = 'some-cache-key-' . uniqid(); + + try { + wp_cache_remember( $key, function () { + throw new Exception( 'Something went wrong!' ); + } ); + + } catch ( Exception $e ) { + $this->assertFalse( wp_cache_get( $key ), 'Expected the exception to not be cached.' ); + return; + } + + $this->fail( 'Did not receive expected exception!' ); + } + + function test_does_not_cache_wp_errors() { + $key = 'some-cache-key-' . uniqid(); + + wp_cache_remember( $key, function () { + return new WP_Error( 'code', 'Something went wrong!' ); + } ); + + $this->assertFalse( wp_cache_get( $key ), 'Expected the WP_Error to not be cached.' ); + } + function test_remember_pulls_from_cache() { $key = 'some-cache-key-' . uniqid(); $value = uniqid(); diff --git a/tests/test-site-transients.php b/tests/test-site-transients.php index 43ff633..842f6b7 100644 --- a/tests/test-site-transients.php +++ b/tests/test-site-transients.php @@ -2,7 +2,7 @@ /** * Tests for the site transient functions. * - * @package Wp_Cache_Remember + * @package SteveGrunwell\WPCacheRemember */ /** @@ -26,6 +26,32 @@ function test_remembers_value() { $this->assertEquals( $value, get_site_transient( $key ) ); } + function test_does_not_remember_exceptions() { + $key = 'some-cache-key-' . uniqid(); + + try { + remember_site_transient( $key, function () { + throw new Exception( 'Something went wrong!' ); + } ); + + } catch ( Exception $e ) { + $this->assertFalse( get_site_transient( $key ), 'Expected the exception to not be cached.' ); + return; + } + + $this->fail( 'Did not receive expected exception!' ); + } + + function test_does_not_remember_wp_errors() { + $key = 'some-cache-key-' . uniqid(); + + remember_site_transient( $key, function () { + return new WP_Error( 'code', 'Something went wrong!' ); + } ); + + $this->assertFalse( get_site_transient( $key ), 'Expected the WP_Error to not be cached.' ); + } + function test_remember_pulls_from_cache() { $key = 'some-cache-key-' . uniqid(); $value = uniqid(); diff --git a/tests/test-transients.php b/tests/test-transients.php index c3b66a5..871da87 100644 --- a/tests/test-transients.php +++ b/tests/test-transients.php @@ -2,7 +2,7 @@ /** * Tests for the transient functions. * - * @package Wp_Cache_Remember + * @package SteveGrunwell\WPCacheRemember */ /** @@ -26,6 +26,32 @@ function test_remembers_value() { $this->assertEquals( $value, get_transient( $key ) ); } + function test_does_not_remember_exceptions() { + $key = 'some-cache-key-' . uniqid(); + + try { + remember_transient( $key, function () { + throw new Exception( 'Something went wrong!' ); + } ); + + } catch ( Exception $e ) { + $this->assertFalse( get_transient( $key ), 'Expected the exception to not be cached.' ); + return; + } + + $this->fail( 'Did not receive expected exception!' ); + } + + function test_does_not_remember_wp_errors() { + $key = 'some-cache-key-' . uniqid(); + + remember_transient( $key, function () { + return new WP_Error( 'code', 'Something went wrong!' ); + } ); + + $this->assertFalse( get_transient( $key ), 'Expected the WP_Error to not be cached.' ); + } + function test_remember_pulls_from_cache() { $key = 'some-cache-key-' . uniqid(); $value = uniqid(); diff --git a/wp-cache-remember.php b/wp-cache-remember.php index f23f5dc..520e2c5 100644 --- a/wp-cache-remember.php +++ b/wp-cache-remember.php @@ -5,7 +5,7 @@ * Description: Helper for the WordPress object cache and transients. * Author: Steve Grunwell * Author URI: https://stevegrunwell.com - * Version: 1.0.0 + * Version: 1.1.0 * * @package SteveGrunwell\WPCacheRemember */ @@ -33,7 +33,9 @@ function wp_cache_remember( $key, $callback, $group = '', $expire = 0 ) { $value = $callback(); - wp_cache_set( $key, $value, $group, $expire ); + if ( ! is_wp_error( $value ) ) { + wp_cache_set( $key, $value, $group, $expire ); + } return $value; } @@ -85,7 +87,9 @@ function remember_transient( $key, $callback, $expire = 0 ) { $value = $callback(); - set_transient( $key, $value, $expire ); + if ( ! is_wp_error( $value ) ) { + set_transient( $key, $value, $expire ); + } return $value; } @@ -135,7 +139,9 @@ function remember_site_transient( $key, $callback, $expire = 0 ) { $value = $callback(); - set_site_transient( $key, $value, $expire ); + if ( ! is_wp_error( $value ) ) { + set_site_transient( $key, $value, $expire ); + } return $value; }