Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegrunwell committed Apr 14, 2018
2 parents 086eeef + 34920f1 commit 1d3f623
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 8 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* PHPUnit bootstrap file
*
* @package Wp_Cache_Remember
* @package SteveGrunwell\WPCacheRemember
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
Expand Down
28 changes: 27 additions & 1 deletion tests/test-object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Tests for the object cache functions.
*
* @package Wp_Cache_Remember
* @package SteveGrunwell\WPCacheRemember
*/

/**
Expand All @@ -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();
Expand Down
28 changes: 27 additions & 1 deletion tests/test-site-transients.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Tests for the site transient functions.
*
* @package Wp_Cache_Remember
* @package SteveGrunwell\WPCacheRemember
*/

/**
Expand All @@ -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();
Expand Down
28 changes: 27 additions & 1 deletion tests/test-transients.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Tests for the transient functions.
*
* @package Wp_Cache_Remember
* @package SteveGrunwell\WPCacheRemember
*/

/**
Expand All @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions wp-cache-remember.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 1d3f623

Please sign in to comment.