Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a caching layer in bu_navigation_get_posts #48

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
language: php

sudo: false
dist: trusty

language: php

php:
- 5.6
- 7.0
- 7.1
notifications:
email:
on_success: never
on_failure: change

env:
- WP_VERSION=nightly
- WP_VERSION=latest
- WP_VERSION=4.7
- WP_VERSION=4.6
branches:
only:
- /.*/

cache:
directories:
- vendor
- $HOME/.composer/cache

before_install:
- composer self-update

install:
- composer install --prefer-dist
matrix:
include:
- php: 7.2
env: WP_VERSION=latest
- php: 7.2
env: WP_VERSION=4.9.10
- php: 7.0
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=4.9.10

before_script:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then
composer global require "phpunit/phpunit=5.7.*"
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
phpenv config-rm xdebug.ini
else
composer global require "phpunit/phpunit=4.8.*"
echo "xdebug.ini does not exist"
fi
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
composer global require "phpunit/phpunit=4.8.*|5.7.*"
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
fi

- git config --global user.email "[email protected]"
- git config --global user.name "Travis CI"
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION

script:
- |
if [[ ! -z "$WP_VERSION" ]] ; then
phpunit
WP_MULTISITE=1 phpunit
fi

after_script:
- ./bin/codeclimate.sh

addons:
codeclimate:
repo_token: CODECLIMATE_REPO_TOKEN
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
phpcs
fi
3 changes: 3 additions & 0 deletions admin/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ public function save() {
// @todo notify user of error messages from WP_Error objects
$this->plugin->log( '%s - Errors encountered during navman save: %s', __METHOD__, print_r( $errors, true ) );
}

// Update the `last_changed` key value with the current time.
wp_cache_set( 'last_changed', microtime(), 'posts' );
}

return $saved;
Expand Down
49 changes: 37 additions & 12 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-false}

WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}

download() {
if [ `which curl` ]; then
Expand All @@ -23,8 +25,15 @@ download() {
fi
}

if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
WP_TESTS_TAG="tags/$WP_VERSION"
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
WP_TESTS_TAG="tags/${WP_VERSION%??}"
else
WP_TESTS_TAG="tags/$WP_VERSION"
fi
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
Expand All @@ -50,18 +59,34 @@ install_wp() {
mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p /tmp/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
mkdir -p $TMPDIR/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip
unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/
mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
# https serves multiple offers, whereas http serves single.
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
LATEST_VERSION=${WP_VERSION%??}
else
# otherwise, scan the releases and get the most up to date minor version of the major release
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
fi
if [[ -z "$LATEST_VERSION" ]]; then
local ARCHIVE_NAME="wordpress-$WP_VERSION"
else
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
fi
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
Expand All @@ -80,7 +105,7 @@ install_test_suite() {
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/images/ $WP_TESTS_DIR/data/images
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
Expand Down Expand Up @@ -124,4 +149,4 @@ install_db() {

install_wp
install_test_suite
install_db
install_db
27 changes: 26 additions & 1 deletion includes/library.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,26 @@ function bu_navigation_get_posts( $args = '' ) {
'include_links' => true,
'suppress_filter_posts' => false,
'suppress_urls' => false,
);
);

$r = wp_parse_args( $args, $defaults );

// Get last changed date for the WP core `posts` cache group.
$last_changed = wp_cache_get_last_changed( 'posts' );

// Create the key to use for storing the results of the following query.
$cache_key = 'get_posts:' . md5( wp_json_encode( $r ) );

// Check if the results of the following query are in cache.
$cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' );

// If a cached value exists and its `last_changed` property matches
// the last changed date for the WP core `posts` cache group,
// return the cached query results.
if ( $cached_posts && $cached_posts['last_changed'] === $last_changed ) {
return $cached_posts['query'];
}

// Start building the query
$where = $orderby = '';

Expand Down Expand Up @@ -532,6 +549,14 @@ function bu_navigation_get_posts( $args = '' ) {
$posts = $items;
}

// Cache the `last_changed` value and the query results.
$cache_value = array(
'last_changed' => $last_changed,
'query' => $posts,
);

wp_cache_set( $cache_key, $cache_value, 'bu-navigation-persistent' );

return $posts;

}
Expand Down