From 6cb5ef6c814aab4a84506c4d66cf1b12f62c0c15 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 17 Feb 2018 01:25:57 +0000 Subject: [PATCH 1/6] Add a formal CHANGELOG.md --- CHANGELOG.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5cb1804 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ +# 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.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.0.0]: https://github.com/stevegrunwell/wp-cache-remember/releases/tag/v1.0.0 From 2fb2c8db2fa774f08714f8c9a30eefe383386303 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 17 Feb 2018 01:28:41 +0000 Subject: [PATCH 2/6] Add a badge with the current release version to the readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 49f8bf5..70f1ada 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. From 40dc228e9b31a20bc52ed353dc44d10e683bdb6a Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 14 Apr 2018 18:07:18 +0000 Subject: [PATCH 3/6] Fix @package tags in the test files --- tests/bootstrap.php | 2 +- tests/test-object-cache.php | 2 +- tests/test-site-transients.php | 2 +- tests/test-transients.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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..7bc95ba 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 */ /** diff --git a/tests/test-site-transients.php b/tests/test-site-transients.php index 43ff633..649d79b 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 */ /** diff --git a/tests/test-transients.php b/tests/test-transients.php index c3b66a5..38e0982 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 */ /** From 2ce88d36e7993a84a4bf734019663d8c7f25c03e Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 14 Apr 2018 18:22:00 +0000 Subject: [PATCH 4/6] If a callback throws an Exception or returns a WP_Error object, the value should not be cached. --- tests/test-object-cache.php | 26 ++++++++++++++++++++++++++ tests/test-site-transients.php | 26 ++++++++++++++++++++++++++ tests/test-transients.php | 26 ++++++++++++++++++++++++++ wp-cache-remember.php | 12 +++++++++--- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/tests/test-object-cache.php b/tests/test-object-cache.php index 7bc95ba..3b32473 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -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 649d79b..842f6b7 100644 --- a/tests/test-site-transients.php +++ b/tests/test-site-transients.php @@ -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 38e0982..871da87 100644 --- a/tests/test-transients.php +++ b/tests/test-transients.php @@ -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..0dea6dc 100644 --- a/wp-cache-remember.php +++ b/wp-cache-remember.php @@ -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; } From 58d0b46ab6eee770c80ff3283fa6164083f30ac3 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 14 Apr 2018 18:26:17 +0000 Subject: [PATCH 5/6] Document the exclusion of WP_Error objects and exceptions from the cache --- CHANGELOG.md | 4 ++++ README.md | 2 ++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cb1804..4c31918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ 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). +## [Unreleased] + +* Bypass the caching operation if a callback either throws an Exception or returns a `WP_Error` object. + ## [1.0.0] - 2018-02-16 Initial public release of the package, including the following functions: diff --git a/README.md b/README.md index 70f1ada..d409375 100644 --- a/README.md +++ b/README.md @@ -87,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. From 34920f1603b5ca5811167e0c15e7aced161852a2 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Sat, 14 Apr 2018 18:41:50 +0000 Subject: [PATCH 6/6] Version bump + changelog updates for the 1.1.0 release --- CHANGELOG.md | 7 +++++-- wp-cache-remember.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c31918..780e422 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,10 @@ 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). -## [Unreleased] +## [1.1.0] - 2018-04-14 -* Bypass the caching operation if a callback either throws an Exception or returns a `WP_Error` object. +* 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 @@ -21,4 +22,6 @@ Initial public release of the package, including the following functions: * `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/wp-cache-remember.php b/wp-cache-remember.php index 0dea6dc..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 */