diff --git a/.editorconfig b/.editorconfig index ac9b144..c1392f5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,8 @@ indent_size = 2 indent_style = space trim_trailing_whitespace = true -[**.php] +[*.php] +indent_style = tab + +[{composer.json,phpcs.ruleset.xml}] indent_size = 4 -indent_style = space diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index df99563..2bcb6b3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,8 +21,6 @@ jobs: - '6' - '7' php-version: - - php: '7.3' - phpunit: 9.3.9 - php: '7.4' phpunit: 9.3.9 - php: '8.0' diff --git a/composer.json b/composer.json index 45cb5da..1eb1fec 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,43 @@ { + "name": "tarosky/wp-redis-patch", + "type": "wordpress-dropin", + "description": "", + "homepage": "https://github.com/tarosky/wp-redis-patch", + "license": "GPL-2.0", + "authors": [ + { + "name": "Harai Akihiro", + "email": "akihiro.harai@tarosky.co.jp", + "homepage": "https://tarosky.co.jp" + } + ], + "minimum-stability": "stable", + "require": { + "php": ">=7.4" + }, "require-dev": { - "giorgiosironi/eris": "^0.13.0" + "giorgiosironi/eris": "^0.13.0", + "squizlabs/php_codesniffer": "*", + "wp-coding-standards/wpcs": "*", + "phpcompatibility/php-compatibility": "*", + "dealerdirect/phpcodesniffer-composer-installer": "*", + "phpcompatibility/phpcompatibility-wp": "*" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "lint": [ + "phpcs --standard=phpcs.ruleset.xml $(find ./ -name '*.php')" + ], + "fix": [ + "phpcbf --standard=phpcs.ruleset.xml $(find ./ -name '*.php')" + ] + }, + "support": { + "issues": "https://github.com/tarosky/wp-redis-patch/issues" } } diff --git a/composer.lock b/composer.lock index 5c7dd5f..01ecf15 100644 --- a/composer.lock +++ b/composer.lock @@ -4,9 +4,87 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "eb0599cad5e1073e7eeb7ac7daca4f51", + "content-hash": "cf93192d7f2fc8e39b2626b677af7bc7", "packages": [], "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/composer-installer.git", + "reference": "4be43904336affa5c2f70744a348312336afd0da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/composer-installer/zipball/4be43904336affa5c2f70744a348312336afd0da", + "reference": "4be43904336affa5c2f70744a348312336afd0da", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.4", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" + }, + "require-dev": { + "composer/composer": "*", + "ext-json": "*", + "ext-zip": "*", + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0", + "yoast/phpunit-polyfills": "^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/composer-installer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcbf", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "support": { + "issues": "https://github.com/PHPCSStandards/composer-installer/issues", + "source": "https://github.com/PHPCSStandards/composer-installer" + }, + "time": "2023-01-05T11:28:13+00:00" + }, { "name": "giorgiosironi/eris", "version": "0.13.0", @@ -69,6 +147,439 @@ "source": "https://github.com/giorgiosironi/eris/tree/0.13.0" }, "time": "2021-12-17T21:51:49+00:00" + }, + { + "name": "phpcompatibility/php-compatibility", + "version": "9.3.5", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" + }, + "conflict": { + "squizlabs/php_codesniffer": "2.6.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "homepage": "https://github.com/wimg", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" + } + ], + "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", + "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", + "keywords": [ + "compatibility", + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibility/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibility" + }, + "time": "2019-12-27T09:44:58+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-paragonie", + "version": "1.3.2", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", + "reference": "bba5a9dfec7fcfbd679cfaf611d86b4d3759da26" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/bba5a9dfec7fcfbd679cfaf611d86b4d3759da26", + "reference": "bba5a9dfec7fcfbd679cfaf611d86b4d3759da26", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7", + "paragonie/random_compat": "dev-master", + "paragonie/sodium_compat": "dev-master" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "paragonie", + "phpcs", + "polyfill", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie" + }, + "time": "2022-10-25T01:46:02+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-wp", + "version": "2.1.4", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git", + "reference": "b6c1e3ee1c35de6c41a511d5eb9bd03e447480a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/b6c1e3ee1c35de6c41a511d5eb9bd03e447480a5", + "reference": "b6c1e3ee1c35de6c41a511d5eb9bd03e447480a5", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0", + "phpcompatibility/phpcompatibility-paragonie": "^1.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "phpcs", + "standards", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityWP/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityWP" + }, + "time": "2022-10-24T09:00:36+00:00" + }, + { + "name": "phpcsstandards/phpcsextra", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHPCSExtra.git", + "reference": "746c3190ba8eb2f212087c947ba75f4f5b9a58d5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSExtra/zipball/746c3190ba8eb2f212087c947ba75f4f5b9a58d5", + "reference": "746c3190ba8eb2f212087c947ba75f4f5b9a58d5", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "phpcsstandards/phpcsutils": "^1.0.8", + "squizlabs/php_codesniffer": "^3.7.1" + }, + "require-dev": { + "php-parallel-lint/php-console-highlighter": "^1.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", + "phpcsstandards/phpcsdevcs": "^1.1.6", + "phpcsstandards/phpcsdevtools": "^1.2.1", + "phpunit/phpunit": "^4.5 || ^5.0 || ^6.0 || ^7.0" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-stable": "1.x-dev", + "dev-develop": "1.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/PHPCSExtra/graphs/contributors" + } + ], + "description": "A collection of sniffs and standards for use with PHP_CodeSniffer.", + "keywords": [ + "PHP_CodeSniffer", + "phpcbf", + "phpcodesniffer-standard", + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCSStandards/PHPCSExtra/issues", + "source": "https://github.com/PHPCSStandards/PHPCSExtra" + }, + "time": "2023-09-20T22:06:18+00:00" + }, + { + "name": "phpcsstandards/phpcsutils", + "version": "1.0.8", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", + "reference": "69465cab9d12454e5e7767b9041af0cd8cd13be7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/69465cab9d12454e5e7767b9041af0cd8cd13be7", + "reference": "69465cab9d12454e5e7767b9041af0cd8cd13be7", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7 || ^1.0", + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.7.1 || 4.0.x-dev@dev" + }, + "require-dev": { + "ext-filter": "*", + "php-parallel-lint/php-console-highlighter": "^1.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", + "phpcsstandards/phpcsdevcs": "^1.1.6", + "yoast/phpunit-polyfills": "^1.0.5 || ^2.0.0" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-stable": "1.x-dev", + "dev-develop": "1.x-dev" + } + }, + "autoload": { + "classmap": [ + "PHPCSUtils/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/PHPCSUtils/graphs/contributors" + } + ], + "description": "A suite of utility functions for use with PHP_CodeSniffer", + "homepage": "https://phpcsutils.com/", + "keywords": [ + "PHP_CodeSniffer", + "phpcbf", + "phpcodesniffer-standard", + "phpcs", + "phpcs3", + "standards", + "static analysis", + "tokens", + "utility" + ], + "support": { + "docs": "https://phpcsutils.com/", + "issues": "https://github.com/PHPCSStandards/PHPCSUtils/issues", + "source": "https://github.com/PHPCSStandards/PHPCSUtils" + }, + "time": "2023-07-16T21:39:41+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.7.2", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ed8e00df0a83aa96acf703f8c2979ff33341f879", + "reference": "ed8e00df0a83aa96acf703f8c2979ff33341f879", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2023-02-22T23:07:41+00:00" + }, + { + "name": "wp-coding-standards/wpcs", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "reference": "b4caf9689f1a0e4a4c632679a44e638c1c67aff1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/b4caf9689f1a0e4a4c632679a44e638c1c67aff1", + "reference": "b4caf9689f1a0e4a4c632679a44e638c1c67aff1", + "shasum": "" + }, + "require": { + "ext-filter": "*", + "ext-libxml": "*", + "ext-tokenizer": "*", + "ext-xmlreader": "*", + "php": ">=5.4", + "phpcsstandards/phpcsextra": "^1.1.0", + "phpcsstandards/phpcsutils": "^1.0.8", + "squizlabs/php_codesniffer": "^3.7.2" + }, + "require-dev": { + "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", + "phpcompatibility/php-compatibility": "^9.0", + "phpcsstandards/phpcsdevtools": "^1.2.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "ext-iconv": "For improved results", + "ext-mbstring": "For improved results" + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Contributors", + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", + "keywords": [ + "phpcs", + "standards", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues", + "source": "https://github.com/WordPress/WordPress-Coding-Standards", + "wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki" + }, + "funding": [ + { + "url": "https://opencollective.com/thewpcc/contribute/wp-php-63406", + "type": "custom" + } + ], + "time": "2023-09-14T07:06:09+00:00" } ], "aliases": [], @@ -76,7 +587,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": ">=7.4" + }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.6.0" } diff --git a/object-cache.php b/object-cache.php index bcd9544..dfb27a1 100644 --- a/object-cache.php +++ b/object-cache.php @@ -5,8 +5,8 @@ * Modified by Tarosky INC. */ -if (!defined('WP_CACHE_KEY_SALT')) { - define('WP_CACHE_KEY_SALT', ''); +if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) { + define( 'WP_CACHE_KEY_SALT', '' ); } /** @@ -25,10 +25,10 @@ * Default 0 (no expiration). * @return bool True on success, false if cache key and group already exist. */ -function wp_cache_add($key, $data, $group = '', $expire = 0) { - global $wp_object_cache; +function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { + global $wp_object_cache; - return $wp_object_cache->add($key, $data, $group, (int) $expire); + return $wp_object_cache->add( $key, $data, $group, (int) $expire ); } /** @@ -45,7 +45,7 @@ function wp_cache_add($key, $data, $group = '', $expire = 0) { * @return true Always returns true. */ function wp_cache_close() { - return true; + return true; } /** @@ -61,10 +61,10 @@ function wp_cache_close() { * @param string $group Optional. The group the key is in. Default empty. * @return int|false The item's new value on success, false on failure. */ -function wp_cache_decr($key, $offset = 1, $group = '') { - global $wp_object_cache; +function wp_cache_decr( $key, $offset = 1, $group = '' ) { + global $wp_object_cache; - return $wp_object_cache->decr($key, $offset, $group); + return $wp_object_cache->decr( $key, $offset, $group ); } /** @@ -79,10 +79,10 @@ function wp_cache_decr($key, $offset = 1, $group = '') { * @param string $group Optional. Where the cache contents are grouped. Default empty. * @return bool True on successful removal, false on failure. */ -function wp_cache_delete($key, $group = '') { - global $wp_object_cache; +function wp_cache_delete( $key, $group = '' ) { + global $wp_object_cache; - return $wp_object_cache->delete($key, $group); + return $wp_object_cache->delete( $key, $group ); } /** @@ -96,9 +96,9 @@ function wp_cache_delete($key, $group = '') { * @return bool True on success, false on failure. */ function wp_cache_flush() { - global $wp_object_cache; + global $wp_object_cache; - return $wp_object_cache->flush(); + return $wp_object_cache->flush(); } /** @@ -117,10 +117,10 @@ function wp_cache_flush() { * Disambiguates a return of false, a storable value. Default null. * @return mixed|false The cache contents on success, false on failure to retrieve contents. */ -function wp_cache_get($key, $group = '', $force = false, &$found = null) { - global $wp_object_cache; +function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { + global $wp_object_cache; - return $wp_object_cache->get($key, $group, $force, $found); + return $wp_object_cache->get( $key, $group, $force, $found ); } /** @@ -137,10 +137,10 @@ function wp_cache_get($key, $group = '', $force = false, &$found = null) { * from the persistent cache. Default false. * @return array Array of values organized into groups. */ -function wp_cache_get_multiple($keys, $group = '', $force = false) { - global $wp_object_cache; +function wp_cache_get_multiple( $keys, $group = '', $force = false ) { + global $wp_object_cache; - return $wp_object_cache->get_multiple($keys, $group, $force); + return $wp_object_cache->get_multiple( $keys, $group, $force ); } /** @@ -156,10 +156,10 @@ function wp_cache_get_multiple($keys, $group = '', $force = false) { * @param string $group Optional. The group the key is in. Default empty. * @return int|false The item's new value on success, false on failure. */ -function wp_cache_incr($key, $offset = 1, $group = '') { - global $wp_object_cache; +function wp_cache_incr( $key, $offset = 1, $group = '' ) { + global $wp_object_cache; - return $wp_object_cache->incr($key, $offset, $group); + return $wp_object_cache->incr( $key, $offset, $group ); } /** @@ -170,7 +170,7 @@ function wp_cache_incr($key, $offset = 1, $group = '') { * @global WP_Object_Cache $wp_object_cache */ function wp_cache_init() { - $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); + $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); } /** @@ -189,10 +189,10 @@ function wp_cache_init() { * Default 0 (no expiration). * @return bool False if original value does not exist, true if contents were replaced */ -function wp_cache_replace($key, $data, $group = '', $expire = 0) { - global $wp_object_cache; +function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { + global $wp_object_cache; - return $wp_object_cache->replace($key, $data, $group, (int) $expire); + return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); } /** @@ -213,10 +213,10 @@ function wp_cache_replace($key, $data, $group = '', $expire = 0) { * Default 0 (no expiration). * @return bool True on success, false on failure. */ -function wp_cache_set($key, $data, $group = '', $expire = 0) { - global $wp_object_cache; +function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { + global $wp_object_cache; - return $wp_object_cache->set($key, $data, $group, (int) $expire); + return $wp_object_cache->set( $key, $data, $group, (int) $expire ); } /** @@ -231,10 +231,10 @@ function wp_cache_set($key, $data, $group = '', $expire = 0) { * * @param int $blog_id Site ID. */ -function wp_cache_switch_to_blog($blog_id) { - global $wp_object_cache; +function wp_cache_switch_to_blog( $blog_id ) { + global $wp_object_cache; - $wp_object_cache->switch_to_blog($blog_id); + $wp_object_cache->switch_to_blog( $blog_id ); } /** @@ -247,10 +247,10 @@ function wp_cache_switch_to_blog($blog_id) { * * @param string|string[] $groups A group or an array of groups to add. */ -function wp_cache_add_global_groups($groups) { - global $wp_object_cache; +function wp_cache_add_global_groups( $groups ) { + global $wp_object_cache; - $wp_object_cache->add_global_groups($groups); + $wp_object_cache->add_global_groups( $groups ); } /** @@ -263,10 +263,10 @@ function wp_cache_add_global_groups($groups) { * * @param string|string[] $groups A group or an array of groups to add. */ -function wp_cache_add_non_persistent_groups($groups) { - global $wp_object_cache; +function wp_cache_add_non_persistent_groups( $groups ) { + global $wp_object_cache; - $wp_object_cache->add_non_persistent_groups($groups); + $wp_object_cache->add_non_persistent_groups( $groups ); } /** @@ -286,7 +286,7 @@ function wp_cache_add_non_persistent_groups($groups) { * @deprecated 3.5.0 wp_cache_switch_to_blog() */ function wp_cache_reset() { - _deprecated_function(__FUNCTION__, '3.5.0', 'wp_cache_switch_to_blog()'); + _deprecated_function( __FUNCTION__, '3.5.0', 'wp_cache_switch_to_blog()' ); } /** @@ -309,1189 +309,1194 @@ function wp_cache_reset() { * ['group0' => ['key0' => 'value0', 'key1' => 'value1', 'key2' => 'value2'], 'group1' => ['key0' => 'value0']] * Values not found in cache will be missing along with the corresponding keys. */ -function wp_cache_tarosky_get_multiple($groups, $force = false) { - global $wp_object_cache; +function wp_cache_tarosky_get_multiple( $groups, $force = false ) { + global $wp_object_cache; - return $wp_object_cache->normalized_get_multiple($groups, $force); + return $wp_object_cache->normalized_get_multiple( $groups, $force ); } -if (!defined('WP_CACHE_VERSION_KEY_SALT')) { - define('WP_CACHE_VERSION_KEY_SALT', 'version:'); +if ( ! defined( 'WP_CACHE_VERSION_KEY_SALT' ) ) { + define( 'WP_CACHE_VERSION_KEY_SALT', 'version:' ); } class WP_Object_Cache_RedisCallException extends Exception { - protected $method; - protected $args; - - public static function create($method, $args, $previous) { - $arg_str = var_export($args, true); - $e = new self( - sprintf( - 'Redis call failed: method: %s, args: %s, previous: %s', - $method, - mb_strimwidth($arg_str, 0, 1024, '...') . '[' . mb_strlen($arg_str) . ' chars]', - $previous->getMessage() - ), - $previous->getCode(), - $previous - ); - - $e->method = $method; - $e->args = $args; - - return $e; - } - - public function getMethod() { - return $this->method; - } - - public function getArgs() { - return $this->args; - } + protected $method; + protected $args; + + public static function create( $method, $args, $previous ) { + $arg_str = var_export( $args, true ); + $e = new self( + sprintf( + 'Redis call failed: method: %s, args: %s, previous: %s', + $method, + mb_strimwidth( $arg_str, 0, 1024, '...' ) . '[' . mb_strlen( $arg_str ) . ' chars]', + $previous->getMessage() + ), + $previous->getCode(), + $previous + ); + + $e->method = $method; + $e->args = $args; + + return $e; + } + + public function getMethod() { + return $this->method; + } + + public function getArgs() { + return $this->args; + } } -class WP_Object_Cache { - /** - * Holds the cached objects - */ - public $cache = []; - - /** - * The amount of times a request was made to Redis - */ - public $redis_calls = []; - - /** - * List of global groups - */ - public $global_groups = []; - - /** - * List of non-persistent groups - */ - public $non_persistent_groups = []; - - /** - * The blog prefix to prepend to keys in non-global groups. - */ - public $blog_prefix; - - /** - * Whether or not Redis is connected - */ - public $is_redis_connected = false; - - /** - * Whether or not the object cache thinks Redis needs a flush - */ - public $do_redis_failback_flush = false; - - /** - * The last triggered error - */ - public $last_triggered_error = ''; - - /** - * Redis client - */ - public $redis; - - public $host; - public $port; - public $timeout; - public $retry_interval; - public $max_retries; - - /** - * Sets the list of global groups. - * - * @param array $groups List of groups that are global. - */ - public function add_global_groups($groups) { - $groups = (array) $groups; - - $groups = array_fill_keys($groups, true); - $this->global_groups = array_merge($this->global_groups, $groups); - } - - /** - * Sets the list of non-persistent groups. - * - * @param array $groups List of groups that are non-persistent. - */ - public function add_non_persistent_groups($groups) { - $groups = (array) $groups; - - $groups = array_fill_keys($groups, true); - $this->non_persistent_groups = array_merge($this->non_persistent_groups, $groups); - } - - /** - * Switch the interal blog id. - * - * This changes the blog id used to create keys in blog specific groups. - * - * @param int $blog_id Blog ID - */ - public function switch_to_blog($blog_id) { - $blog_id = (int) $blog_id; - $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; - } - - /** - * Check whether there's a value in the internal object cache. - * - * @param string $key - * @param string $group - * @return boolean - */ - private function isset_internal($key, $group) { - $key = $this->key($key, $group); - return array_key_exists($key, $this->cache); - } - - /** - * Get a value from the internal object cache - * - * @param string $key - * @param string $group - * @return mixed - */ - private function get_internal($key, $group) { - $value = null; - $key = $this->key($key, $group); - if (array_key_exists($key, $this->cache)) { - $value = $this->cache[$key]; - } - if (is_object($value)) { - return clone $value; - } - return $value; - } - - /** - * Set a value to the internal object cache - * - * @param string $key - * @param string $group - * @param mixed $value - */ - private function set_internal($key, $group, $value) { - $key = $this->key($key, $group); - $this->cache[$key] = $value; - } - - /** - * Unset a value from the internal object cache - * - * @param string $key - * @param string $group - */ - private function unset_internal($key, $group) { - $key = $this->key($key, $group); - if (array_key_exists($key, $this->cache)) { - unset($this->cache[$key]); - } - } - - /** - * Does this group use persistent storage? - * - * @param string $group Cache group. - * @return bool true if the group is persistent, false if not. - */ - private function should_persist($group) { - return empty($this->non_persistent_groups[$group]); - } - - /** - * Wrapper method for connecting to Redis, which lets us retry the connection - */ - private function connect_redis() { - try { - $this->redis = $this->prepare_client_connection(); - } catch (Exception $e) { - $this->exception_handler($e); - $this->is_redis_connected = false; - return false; - } - - $this->is_redis_connected = $this->redis->isConnected(); - return $this->is_redis_connected; - } - - /** - * Check the error message and return true if the call is retriable. - * - * @param string $error Message to compare - * @return bool whether $error is retriable - */ - public function is_retriable_error_message($error) { - $retriable_error_messages = [ - 'socket error on read socket', - 'Connection closed', - 'Redis server went away', - ]; - - foreach ($retriable_error_messages as $msg) { - if (strpos($error, $msg) !== false) { - return true; - } - } - return false; - } - - /** - * Handles exceptions by triggering a php error. - * - * @param Exception $exception - * @return null - */ - protected function exception_handler($exception) { - try { - $this->last_triggered_error = 'WP Redis: ' . $exception->getMessage() . ": \n" . $exception->getTraceAsString(); - // Be friendly to developers debugging production servers by triggering an error +class WP_Object_Cache { // phpcs:ignore + /** + * Holds the cached objects + */ + public $cache = array(); + + /** + * The amount of times a request was made to Redis + */ + public $redis_calls = array(); + + /** + * List of global groups + */ + public $global_groups = array(); + + /** + * List of non-persistent groups + */ + public $non_persistent_groups = array(); + + /** + * The blog prefix to prepend to keys in non-global groups. + */ + public $blog_prefix; + + /** + * Whether or not Redis is connected + */ + public $is_redis_connected = false; + + /** + * Whether or not the object cache thinks Redis needs a flush + */ + public $do_redis_failback_flush = false; + + /** + * The last triggered error + */ + public $last_triggered_error = ''; + + /** + * Redis client + */ + public $redis; + + public $host; + public $port; + public $timeout; + public $retry_interval; + public $max_retries; + + /** + * Sets the list of global groups. + * + * @param array $groups List of groups that are global. + */ + public function add_global_groups( $groups ) { + $groups = (array) $groups; + + $groups = array_fill_keys( $groups, true ); + $this->global_groups = array_merge( $this->global_groups, $groups ); + } + + /** + * Sets the list of non-persistent groups. + * + * @param array $groups List of groups that are non-persistent. + */ + public function add_non_persistent_groups( $groups ) { + $groups = (array) $groups; + + $groups = array_fill_keys( $groups, true ); + $this->non_persistent_groups = array_merge( $this->non_persistent_groups, $groups ); + } + + /** + * Switch the interal blog id. + * + * This changes the blog id used to create keys in blog specific groups. + * + * @param int $blog_id Blog ID + */ + public function switch_to_blog( $blog_id ) { + $blog_id = (int) $blog_id; + $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; + } + + /** + * Check whether there's a value in the internal object cache. + * + * @param string $key + * @param string $group + * @return boolean + */ + private function isset_internal( $key, $group ) { + $key = $this->key( $key, $group ); + return array_key_exists( $key, $this->cache ); + } + + /** + * Get a value from the internal object cache + * + * @param string $key + * @param string $group + * @return mixed + */ + private function get_internal( $key, $group ) { + $value = null; + $key = $this->key( $key, $group ); + if ( array_key_exists( $key, $this->cache ) ) { + $value = $this->cache[ $key ]; + } + if ( is_object( $value ) ) { + return clone $value; + } + return $value; + } + + /** + * Set a value to the internal object cache + * + * @param string $key + * @param string $group + * @param mixed $value + */ + private function set_internal( $key, $group, $value ) { + $key = $this->key( $key, $group ); + $this->cache[ $key ] = $value; + } + + /** + * Unset a value from the internal object cache + * + * @param string $key + * @param string $group + */ + private function unset_internal( $key, $group ) { + $key = $this->key( $key, $group ); + if ( array_key_exists( $key, $this->cache ) ) { + unset( $this->cache[ $key ] ); + } + } + + /** + * Does this group use persistent storage? + * + * @param string $group Cache group. + * @return bool true if the group is persistent, false if not. + */ + private function should_persist( $group ) { + return empty( $this->non_persistent_groups[ $group ] ); + } + + /** + * Wrapper method for connecting to Redis, which lets us retry the connection + */ + private function connect_redis() { + try { + $this->redis = $this->prepare_client_connection(); + } catch ( Exception $e ) { + $this->exception_handler( $e ); + $this->is_redis_connected = false; + return false; + } + + $this->is_redis_connected = $this->redis->isConnected(); + return $this->is_redis_connected; + } + + /** + * Check the error message and return true if the call is retriable. + * + * @param string $error Message to compare + * @return bool whether $error is retriable + */ + public function is_retriable_error_message( $error ) { + $retriable_error_messages = array( + 'socket error on read socket', + 'Connection closed', + 'Redis server went away', + ); + + foreach ( $retriable_error_messages as $msg ) { + if ( strpos( $error, $msg ) !== false ) { + return true; + } + } + return false; + } + + /** + * Handles exceptions by triggering a php error. + * + * @param Exception $exception + * @return null + */ + protected function exception_handler( $exception ) { + try { + $this->last_triggered_error = 'WP Redis: ' . $exception->getMessage() . ": \n" . $exception->getTraceAsString(); + // Be friendly to developers debugging production servers by triggering an error // @codingStandardsIgnoreStart trigger_error($this->last_triggered_error, E_USER_WARNING); // @codingStandardsIgnoreEnd - } catch (PHPUnit_Framework_Error_Warning $e) { - // PHPUnit throws an Exception when `trigger_error()` is called. - // To ensure our tests (which expect Exceptions to be caught) continue to run, - // we catch the PHPUnit exception and inspect the RedisException message - } - } - - /** - * Whether or not wakeup flush is enabled - * - * @return bool - */ - private function is_redis_failback_flush_enabled() { - if (defined('WP_INSTALLING') && WP_INSTALLING) { - return false; - } elseif (defined('WP_REDIS_DISABLE_FAILBACK_FLUSH') && WP_REDIS_DISABLE_FAILBACK_FLUSH) { - return false; - } - return true; - } - - private static $lua_scripts = [ - 'decr-by-nover' => [], - 'incr-by-nover' => [], - 'set' => [], - ]; - - public static function initialize() { - foreach (self::$lua_scripts as $name => &$value) { - $script = file_get_contents(TAROSKY_WP_REDIS_PATCH_LUA_DIR . "/$name.lua"); - $sha1 = sha1($script); - $value = [ - 'script' => $script, - 'hash' => $sha1, - ]; - } - } - - public function is_connected() { - return $this->call_redis('isConnected'); - } - - private static function debug($message, ...$params) { - if (defined('TAROSKY_WP_REDIS_PATCH_DEBUG') && TAROSKY_WP_REDIS_PATCH_DEBUG) { - error_log("[WP_Object_Cache debug]$message: " . var_export($params, true)); - } - } - - private static function error($message, ...$params) { - error_log("[WP_Object_Cache error]$message: " . var_export($params, true)); - error_log( - "[WP_Object_Cache error]stacktrace: " . - var_export(debug_backtrace(), true), - ); - } - - public function ensureLua($redis_client) { - $files = array_values(self::$lua_scripts); - $hashes = array_map(function ($s) { - return $s['hash']; - }, $files); - $scripts = array_map(function ($s) { - return $s['script']; - }, $files); - $existences = $redis_client->script('exists', ...$hashes); - - array_map( - function ($script, $exists, $hash, $file) use ($redis_client) { - if ($exists) { - return; - } - - $res_hash = $redis_client->script('load', $script); - if ($hash !== $res_hash) { - self::error("SHA1 hashes don't match", [ - 'name' => $file, - 'calculated' => $hash, - 'returned' => $res_hash, - ]); - $this->call_redis('close'); - } - self::debug('loaded a Lua script', $file); - return; - }, - $scripts, - $existences, - $hashes, - $files, - ); - } - - private static function clone($data) { - return is_object($data) ? clone $data : $data; - } - - /** - * Utility function to generate the redis key for a given key and group. - * - * @param string $key The cache key. - * @param string $group The cache group. - * @return string A properly prefixed redis cache key. - */ - public function key($key = '', $group = 'default') { - if (empty($group)) { - $group = 'default'; - } - - if (!empty($this->global_groups[$group])) { - $prefix = $this->global_prefix; - } else { - $prefix = $this->blog_prefix; - } - - if ($prefix === null) { - $prefix = ''; - } - - return WP_CACHE_KEY_SALT . - json_encode([strval($prefix), strval($group), strval($key)]); - } - - /** - * Constructs a PHPRedis client. - * - * @param array $client_parameters Parameters used to construct a Redis client. - * @return Redis Redis client. - */ - public function prepare_client_connection() { - $redis_client = new Redis(); - - $redis_client->connect( - $this->host, // host - $this->port, // port - $this->timeout / 1000, // timeout - null, // reserved - 100, // retry_interval - $this->timeout / 1000, // read_timeout - ); - - $this->ensureLua($redis_client); - - return $redis_client; - } - - private $versioned_redis_keys = []; - private $versions = []; - - public static function encode_redis_string($data) { - return is_numeric($data) && intval($data) === $data - ? $data - : igbinary_serialize($data); - } - - public static function decode_redis_get($data) { - if ($data === false) { - return [false, false]; - } - - set_error_handler(function () use ($data) { - throw new Exception("failed to unserialize: '$data'"); - }, E_WARNING); - try { - $result = is_numeric($data) ? intval($data) : igbinary_unserialize($data); - } catch (Exception $e) { - self::error('decode failure', $e->getMessage()); - return [false, false]; - } finally { - restore_error_handler(); - } - return [$result, true]; - } - - public static function decode_redis_del($result) { - if (!is_numeric($result)) { - return 0; - } - return intval($result); - } - - private function init_versioned_redis_keys() { - global $redis_server_versioning_keys; - foreach ($redis_server_versioning_keys as $gkey => $group) { - foreach (array_keys($group) as $key) { - $this->versioned_redis_keys[$this->key($key, $gkey)] = true; - } - } - } - - private static function is_ignored($key, $group) { - // Ignoring works as if the corresponding cache weren't there. - global $redis_server_ignored_keys; - if (!$redis_server_ignored_keys) { - $redis_server_ignored_keys = []; - } - $iks = $redis_server_ignored_keys; - return array_key_exists($group, $iks) && - array_key_exists($key, $iks[$group]) && - $iks[$group][$key]; - } - - - /** - * Sets up object properties; PHP 5 style constructor - * - * @return null|WP_Object_Cache If cache is disabled, returns null. - */ - public function __construct() { - global $blog_id, $table_prefix, $wpdb, $redis_server; - - $this->multisite = is_multisite(); - $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; - - $this->host = $redis_server['host']; - $this->port = $redis_server['port']; - $this->timeout = $redis_server['timeout'] ?? 0; - $this->retry_interval = $redis_server['retry_interval'] ?? 1000; - $this->max_retries = $redis_server['max_retries'] ?? 3; - - $this->connect_redis(); - - if ($this->is_redis_failback_flush_enabled() && !empty($wpdb)) { - if ($this->multisite) { - $table = $wpdb->sitemeta; - $col1 = 'meta_key'; - $col2 = 'meta_value'; - } else { - $table = $wpdb->options; - $col1 = 'option_name'; - $col2 = 'option_value'; - } + } catch ( PHPUnit_Framework_Error_Warning $e ) { + // PHPUnit throws an Exception when `trigger_error()` is called. + // To ensure our tests (which expect Exceptions to be caught) continue to run, + // we catch the PHPUnit exception and inspect the RedisException message + } + } + + /** + * Whether or not wakeup flush is enabled + * + * @return bool + */ + private function is_redis_failback_flush_enabled() { + if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) { + return false; + } elseif ( defined( 'WP_REDIS_DISABLE_FAILBACK_FLUSH' ) && WP_REDIS_DISABLE_FAILBACK_FLUSH ) { + return false; + } + return true; + } + + private static $lua_scripts = array( + 'decr-by-nover' => array(), + 'incr-by-nover' => array(), + 'set' => array(), + ); + + public static function initialize() { + foreach ( self::$lua_scripts as $name => &$value ) { + $script = file_get_contents( TAROSKY_WP_REDIS_PATCH_LUA_DIR . "/$name.lua" ); + $sha1 = sha1( $script ); + $value = array( + 'script' => $script, + 'hash' => $sha1, + ); + } + } + + public function is_connected() { + return $this->call_redis( 'isConnected' ); + } + + private static function debug( $message, ...$params ) { + if ( defined( 'TAROSKY_WP_REDIS_PATCH_DEBUG' ) && TAROSKY_WP_REDIS_PATCH_DEBUG ) { + error_log( "[WP_Object_Cache debug]$message: " . var_export( $params, true ) ); + } + } + + private static function error( $message, ...$params ) { + error_log( "[WP_Object_Cache error]$message: " . var_export( $params, true ) ); + error_log( + '[WP_Object_Cache error]stacktrace: ' . + var_export( debug_backtrace(), true ), // phpcs:ignore + ); + } + + public function ensureLua( $redis_client ) { + $files = array_values( self::$lua_scripts ); + $hashes = array_map(function ( $s ) { + return $s['hash']; + }, $files); + $scripts = array_map(function ( $s ) { + return $s['script']; + }, $files); + $existences = $redis_client->script( 'exists', ...$hashes ); + + array_map( + function ( $script, $exists, $hash, $file ) use ( $redis_client ) { + if ( $exists ) { + return; + } + + $res_hash = $redis_client->script( 'load', $script ); + if ( $hash !== $res_hash ) { + self::error("SHA1 hashes don't match", array( + 'name' => $file, + 'calculated' => $hash, + 'returned' => $res_hash, + )); + $this->call_redis( 'close' ); + } + self::debug( 'loaded a Lua script', $file ); + return; + }, + $scripts, + $existences, + $hashes, + $files, + ); + } + + private static function clone( $data ) { + return is_object( $data ) ? clone $data : $data; + } + + /** + * Utility function to generate the redis key for a given key and group. + * + * @param string $key The cache key. + * @param string $group The cache group. + * @return string A properly prefixed redis cache key. + */ + public function key( $key = '', $group = 'default' ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( ! empty( $this->global_groups[ $group ] ) ) { + $prefix = $this->global_prefix; + } else { + $prefix = $this->blog_prefix; + } + + if ( null === $prefix ) { + $prefix = ''; + } + + return WP_CACHE_KEY_SALT . + json_encode( array( strval( $prefix ), strval( $group ), strval( $key ) ) ); + } + + /** + * Constructs a PHPRedis client. + * + * @param array $client_parameters Parameters used to construct a Redis client. + * @return Redis Redis client. + */ + public function prepare_client_connection() { + $redis_client = new Redis(); + + $redis_client->connect( + $this->host, // host + $this->port, // port + $this->timeout / 1000, // timeout + null, // reserved + 100, // retry_interval + $this->timeout / 1000, // read_timeout + ); + + $this->ensureLua( $redis_client ); + + return $redis_client; + } + + private $versioned_redis_keys = array(); + private $versions = array(); + + public static function encode_redis_string( $data ) { + return is_numeric( $data ) && intval( $data ) === $data + ? $data + : igbinary_serialize( $data ); + } + + public static function decode_redis_get( $data ) { + if ( false === $data ) { + return array( false, false ); + } + + set_error_handler(function () use ( $data ) { + throw new Exception( "failed to unserialize: '$data'" ); + }, E_WARNING); + try { + $result = is_numeric( $data ) ? intval( $data ) : igbinary_unserialize( $data ); + } catch ( Exception $e ) { + self::error( 'decode failure', $e->getMessage() ); + return array( false, false ); + } finally { + restore_error_handler(); + } + return array( $result, true ); + } + + public static function decode_redis_del( $result ) { + if ( ! is_numeric( $result ) ) { + return 0; + } + return intval( $result ); + } + + private function init_versioned_redis_keys() { + global $redis_server_versioning_keys; + foreach ( $redis_server_versioning_keys as $gkey => $group ) { + foreach ( array_keys( $group ) as $key ) { + $this->versioned_redis_keys[ $this->key( $key, $gkey ) ] = true; + } + } + } + + private static function is_ignored( $key, $group ) { + // Ignoring works as if the corresponding cache weren't there. + global $redis_server_ignored_keys; + if ( ! $redis_server_ignored_keys ) { + $redis_server_ignored_keys = array(); + } + $iks = $redis_server_ignored_keys; + return array_key_exists( $group, $iks ) && + array_key_exists( $key, $iks[ $group ] ) && + $iks[ $group ][ $key ]; + } + + + /** + * Sets up object properties; PHP 5 style constructor + * + * @return null|WP_Object_Cache If cache is disabled, returns null. + */ + public function __construct() { + global $blog_id, $table_prefix, $wpdb, $redis_server; + + $this->multisite = is_multisite(); + $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; + + $this->host = $redis_server['host']; + $this->port = $redis_server['port']; + $this->timeout = $redis_server['timeout'] ?? 0; + $this->retry_interval = $redis_server['retry_interval'] ?? 1000; + $this->max_retries = $redis_server['max_retries'] ?? 3; + + $this->connect_redis(); + + if ( $this->is_redis_failback_flush_enabled() && ! empty( $wpdb ) ) { + if ( $this->multisite ) { + $table = $wpdb->sitemeta; + $col1 = 'meta_key'; + $col2 = 'meta_value'; + } else { + $table = $wpdb->options; + $col1 = 'option_name'; + $col2 = 'option_value'; + } // @codingStandardsIgnoreStart $this->do_redis_failback_flush = (bool) $wpdb->get_results("SELECT {$col2} FROM {$table} WHERE {$col1}='wp_redis_do_redis_failback_flush'"); // @codingStandardsIgnoreEnd - if ($this->is_redis_connected && $this->do_redis_failback_flush) { - $ret = $this->call_redis('flushdb'); - if ($ret) { + if ( $this->is_redis_connected && $this->do_redis_failback_flush ) { + $ret = $this->call_redis( 'flushdb' ); + if ( $ret ) { // @codingStandardsIgnoreStart $wpdb->query("DELETE FROM {$table} WHERE {$col1}='wp_redis_do_redis_failback_flush'"); // @codingStandardsIgnoreEnd - $this->do_redis_failback_flush = false; - } - } - } - - $this->global_prefix = ($this->multisite || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE')) ? '' : $table_prefix; - - $this->init_versioned_redis_keys(); - } - - public function should_version($redis_key) { - return isset($this->versioned_redis_keys[$redis_key]); - } - - public function set_cache_version($redis_key, $version) { - $this->versions[$redis_key] = $version; - } - - public function clear_cache_version($redis_key) { - unset($this->versions[$redis_key]); - } - - public function flush_cache_versions() { - $this->versions = []; - } - - public function get_cache_version($redis_key) { - return isset($this->versions[$redis_key]) ? $this->versions[$redis_key] : null; - } - - public function version_key($key = '', $group = 'default') { - if (empty($group)) { - $group = 'default'; - } - - if (!empty($this->global_groups[$group])) { - $prefix = $this->global_prefix; - } else { - $prefix = $this->blog_prefix; - } - - return WP_CACHE_VERSION_KEY_SALT . - json_encode([strval($prefix), strval($group), strval($key)]); - } - - public function generate_version() { - // Versions are expressed as UUID v4. - return sprintf( - '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - random_int(0, 0xffff), - random_int(0, 0xffff), - random_int(0, 0xffff), - random_int(0, 0x0fff) | 0x4000, - random_int(0, 0x3fff) | 0x8000, - random_int(0, 0xffff), - random_int(0, 0xffff), - random_int(0, 0xffff), - ); - } - - private function call_set($key, $group, $params) { - $redis_key = $params['key']; - - if (!$this->should_version($redis_key)) { - $ps = [$redis_key, $params['value']]; - $opt = []; - if ($params['nx']) { - $opt[] = 'nx'; - } - if ($params['xx']) { - $opt[] = 'xx'; - } - if ($params['ex'] !== null) { - $opt['ex'] = $params['ex']; - } - if ($params['px'] !== null) { - $opt['px'] = $params['px']; - } - if ($opt) { - $ps[] = $opt; - } - return $this->call_redis('set', $ps); - } - - $new_version = $this->generate_version(); - - $result = $this->call_redis( - 'evalSha', - [ - self::$lua_scripts['set']['hash'], - [ - $redis_key, - $this->version_key($key, $group), - $this->get_cache_version($redis_key) ?? '', - $new_version, - $params['value'], - $params['ex'] ?? '', - $params['px'] ?? '', - var_export($params['nx'], true), - var_export($params['xx'], true), - var_export($params['keepttl'], true), - ], - 2, - ] - ); - - if ($result === false) { - $this->clear_cache_version($redis_key); - $this->unset_internal($key, $group); - $this->debug('found inconsistent update during SET operation', $redis_key); - return false; - } - - $this->set_cache_version($redis_key, $new_version); - return $result; - } - - /** - * Sets the data contents into the cache - * - * The cache contents is grouped by the $group parameter followed by the - * $key. This allows for duplicate ids in unique groups. Therefore, naming of - * the group should be used with care and should follow normal function - * naming guidelines outside of core WordPress usage. - * - * The $expire parameter is not used, because the cache will automatically - * expire for each time a page is accessed and PHP finishes. The method is - * more for cache plugins which use files. - * - * @param int|string $key What to call the contents in the cache - * @param mixed $data The contents to store in the cache - * @param string $group Where to group the cache contents - * @param int $expire TTL for the data, in seconds - * @return bool Always returns true - */ - public function set($key, $data, $group = 'default', $expire = 0) { - if (function_exists('wp_suspend_cache_addition') && wp_suspend_cache_addition()) { - return false; - } - - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - return true; - } - - if (!$this->should_persist($group)) { - $this->set_internal($key, $group, self::clone($data)); - return true; - } - - $set_params = $this->new_set_param( - $this->key($key, $group), - self::encode_redis_string($data), - ); - if ($expire) { - $set_params['ex'] = $expire; - } - $succeeded = $this->call_set($key, $group, $set_params); - - if ($succeeded) { - $this->set_internal($key, $group, self::clone($data)); - } else { - $this->unset_internal($key, $group); - } - - return $succeeded; - } - - /** - * Replace the contents in the cache, if contents already exist - * @see WP_Object_Cache::set() - * - * @param int|string $key What to call the contents in the cache - * @param mixed $data The contents to store in the cache - * @param string $group Where to group the cache contents - * @param int $expire When to expire the cache contents - * @return bool False if not exists, true if contents were replaced - */ - public function replace($key, $data, $group = 'default', $expire = 0) { - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - return false; - } - - if (!$this->should_persist($group)) { - return false; - } - - $set_params = $this->new_set_param( - $this->key($key, $group), - self::encode_redis_string($data), - ); - $set_params['xx'] = true; - if ($expire) { - $set_params['ex'] = $expire; - } - $succeeded = $this->call_set($key, $group, $set_params); - - if ($succeeded) { - $this->set_internal($key, $group, self::clone($data)); - } else { - $this->unset_internal($key, $group); - } - - return $succeeded; - } - - /** - * Retrieves the cache contents, if it exists - * - * The contents will be first attempted to be retrieved by searching by the - * key in the cache group. If the cache is hit (success) then the contents - * are returned. - * - * @param int|string $key What the contents in the cache are called - * @param string $group Where the cache contents are grouped - * @param string $force Whether to force a refetch rather than relying on the local cache (default is false) - * @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of false, a storable value. Passed by reference. Default null. - * @return bool|mixed False on failure to retrieve contents or the cache contents on success - */ - public function get($key, $group = 'default', $force = false, &$found = null) { - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - $found = false; - return false; - } - - // Key is set internally, so we can use this value - if ($this->isset_internal($key, $group) && !$force) { - $found = true; - return $this->get_internal($key, $group); - } - - // Not a persistent group, so don't try Redis if the value doesn't exist - // internally - if (!$this->should_persist($group)) { - $found = false; - return false; - } - - try { - $redis_key = $this->key($key, $group); - - if (!$this->should_version($redis_key)) { - list($value, $found) = self::decode_redis_get( - $this->call_redis('get', [$redis_key]), - ); - return $value; - } - - $res = $this->call_redis('mget', [[ - $redis_key, - $this->version_key($key, $group), - ]]); - if ($res === false) { - return false; - } - - [$res2, $actual_version] = $res; - if ($actual_version === false) { - $this->clear_cache_version($redis_key); - } else { - $this->set_cache_version($redis_key, $actual_version); - } - - list($value, $found) = self::decode_redis_get($res2); - return $value; - } finally { - if ($found) { - $this->set_internal($key, $group, $value); - } else { - $this->unset_internal($key, $group); - } - } - } - - private function call_mget($redis_keys, $redis_group_key_indexes) { - if (!$redis_keys) { - return []; - } - - $should_version_flags = []; - $redis_params = []; - - array_map( - function ($redis_key, $group_key) use ( - &$redis_params, - &$should_version_flags - ) { - $should_version = $this->should_version($redis_key); - $should_version_flags[] = $should_version; - $redis_params[] = $redis_key; - if ($should_version) { - $redis_params[] = $this->version_key($group_key[1], $group_key[0]); - } - return; - }, - $redis_keys, - $redis_group_key_indexes, - ); - - $res_count = 0; - $res_array = $this->call_redis('mget', [$redis_params]); - if ($res_array === false) { - return array_fill(0, count($redis_keys), false); - } - $results = []; - foreach ($redis_keys as $i => $redis_key) { - $results[] = $res_array[$res_count++]; - if ($should_version_flags[$i]) { - $version = $res_array[$res_count++]; - if ($version === false) { - $this->clear_cache_version($redis_key); - } else { - $this->set_cache_version($redis_key, $version); - } - } - } - assert($res_count == count($res_array)); - - return $results; - } - - /** - * Retrieves multiple values from the cache in one call. - * - * @param array $keys Array of keys under which the cache contents are stored. - * @param string $group Optional. Where the cache contents are grouped. Default empty. - * @param bool $force Optional. Whether to force an update of the local cache - * from the persistent cache. Default false. - * @return array Array of values organized into groups. - */ - public function get_multiple($keys, $group = 'default', $force = false) { - $normal_res = $this->normalized_get_multiple([$group => $keys], $force); - $group_kv = array_key_exists($group, $normal_res) ? $normal_res[$group] : []; - - $result = []; - foreach ($keys as $key) { - $result[$key] = array_key_exists($key, $group_kv) ? $group_kv[$key] : false; - } - return $result; - } - - public function normalized_get_multiple($groups, $force = false) { - $get_from_cache = function ($key, $group) { - $found = $this->isset_internal($key, $group); - - return [$found ? $this->get_internal($key, $group) : null, $found]; - }; - - if (empty($groups) || !is_array($groups)) { - return false; - } - - $cache = []; - $redis_group_key_indexes = []; - $redis_keys = []; - - foreach ($groups as $group => $keys) { - if (empty($group)) { - $group = 'default'; - } - - if (!$this->should_persist($group)) { - foreach ($keys as $key) { - if ($this->is_ignored($key, $group)) { - continue; - } - list($value, $found) = $get_from_cache($key, $group); - if ($found) { - $cache[$group][$key] = $value; - } - } - continue; - } - - foreach ($keys as $key) { - if ($this->is_ignored($key, $group)) { - continue; - } - - if (!$force && $this->isset_internal($key, $group)) { - $cache[$group][$key] = $this->get_internal($key, $group); - continue; - } - - $redis_keys[] = $this->key($key, $group); - $redis_group_key_indexes[] = [$group, $key]; - } - } - - if ($redis_keys) { - $redis_result = $this->call_mget($redis_keys, $redis_group_key_indexes); - - array_map( - function ($result, $group_key) use (&$cache) { - list($value, $found) = $result; - list($group, $key) = $group_key; - - if ($found) { - $cache[$group][$key] = $value; - $this->set_internal($key, $group, $value); - } - - return null; - }, - array_map([$this, 'decode_redis_get'], $redis_result), - $redis_group_key_indexes, - ); - } - - return $cache; - } - - /** - * Remove the contents of the cache key in the group - * - * If the cache key does not exist in the group and $force parameter is set - * to false, then nothing will happen. The $force parameter is set to false - * by default. - * - * @param int|string $key What the contents in the cache are called - * @param string $group Where the cache contents are grouped - * @param bool $force Optional. Whether to force the unsetting of the cache - * key in the group - * @return bool False if the contents weren't deleted and true on success - */ - public function delete($key, $group = 'default', $force = true) { - // `force` param is ignored. - if (empty($group)) { - $group = 'default'; - } - - try { - if (!$this->should_persist($group)) { - return true; - } - - $redis_key = $this->key($key, $group); - - if (!$this->should_version($redis_key)) { - return self::decode_redis_del($this->call_redis('del', [$redis_key])) == 1; - } - - $this->clear_cache_version($redis_key); - $res = $this->call_redis('del', [$redis_key, $this->version_key($key, $group)]); - return 0 < self::decode_redis_del($res); - } finally { - $this->unset_internal($key, $group); - } - } - - private static function new_set_param($key, $value) { - return [ - 'key' => $key, - 'value' => $value, - 'ex' => null, // EX must be greater than 0. - 'px' => null, // PX must be greater than 0. - 'nx' => false, - 'xx' => false, - 'keepttl' => false, - ]; - } - - /** - * Adds data to the cache if it doesn't already exist. - * - * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. - * @uses WP_Object_Cache::set Sets the data after the checking the cache - * contents existence. - * - * @param int|string $key What to call the contents in the cache - * @param mixed $data The contents to store in the cache - * @param string $group Where to group the cache contents - * @param int $expire When to expire the cache contents - * @return bool False if cache key and group already exist, true on success - */ - public function add($key, $data, $group = 'default', $expire = 0) { - if (function_exists('wp_suspend_cache_addition') && wp_suspend_cache_addition()) { - return false; - } - - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - return true; - } - - if (!$this->should_persist($group)) { - return false; - } - - $set_params = $this->new_set_param( - $this->key($key, $group), - self::encode_redis_string($data), - ); - $set_params['nx'] = true; - if ($expire) { - $set_params['ex'] = $expire; - } - $succeeded = $this->call_set($key, $group, $set_params); - - if ($succeeded) { - $this->set_internal($key, $group, self::clone($data)); - } else { - $this->unset_internal($key, $group); - } - - return $succeeded; - } - - /** - * Decrement numeric cache item's value - * - * @param int|string $key The cache key to increment - * @param int $offset The amount by which to decrement the item's value. Default is 1. - * @param string $group The group the key is in. - * @return false|int False on failure, the item's new value on success. - */ - public function decr($key, $offset = 1, $group = 'default') { - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - return false; - } - - $offset = (int) $offset; - - // If this isn't a persistant group, we have to sort this out ourselves, grumble grumble. - if (!$this->should_persist($group)) { - if (!$this->isset_internal($key, $group)) { - return false; - } - - $existing = $this->get_internal($key, $group); - if (!is_numeric($existing)) { - $existing = 0; - } else { - $existing -= $offset; - } - if ($existing < 0) { - $existing = 0; - } - $this->set_internal($key, $group, $existing); - return $existing; - } - - $result = $this->call_redis( - 'evalSha', - [ - self::$lua_scripts['decr-by-nover']['hash'], - [$this->key($key, $group), $offset], - 1, - ] - ); - - if (is_int($result)) { - $this->set_internal($key, $group, $result); - } - return $result; - } - - /** - * Increment numeric cache item's value - * - * @param int|string $key The cache key to increment - * @param int $offset The amount by which to increment the item's value. Default is 1. - * @param string $group The group the key is in. - * @return false|int False on failure, the item's new value on success. - */ - public function incr($key, $offset = 1, $group = 'default') { - if (empty($group)) { - $group = 'default'; - } - - if ($this->is_ignored($key, $group)) { - return false; - } - - $offset = (int) $offset; - - // If this isn't a persistant group, we have to sort this out ourselves, grumble grumble. - if (!$this->should_persist($group)) { - if (!$this->isset_internal($key, $group)) { - return false; - } - - $existing = $this->get_internal($key, $group); - if (!is_numeric($existing)) { - $existing = 1; - } else { - $existing += $offset; - } - if ($existing < 0) { - $existing = 0; - } - $this->set_internal($key, $group, $existing); - return $existing; - } - - $result = $this->call_redis( - 'evalSha', - [ - self::$lua_scripts['incr-by-nover']['hash'], - [$this->key($key, $group), $offset], - 1, - ] - ); - - if (is_int($result)) { - $this->set_internal($key, $group, $result); - } - return $result; - } - - /** - * Clears the object cache of all data. - * - * @return bool True on success, false on failure. - */ - public function flush() { - $this->flush_cache_versions(); - $this->cache = []; - return $this->call_redis('flushdb'); - } - - protected function call_redis_method($method, $args) { - return call_user_func_array([$this->redis, $method], $args); - } - - /** - * Wrapper method for calls to Redis, which fails gracefully when Redis is unavailable - * - * @param string $method - * @param mixed $args - * @return mixed - */ - public function call_redis($method, $args = [], $retry_count = 0) { - global $wpdb; - - if ($this->is_redis_connected) { - if (!isset($this->redis_calls[$method])) { - $this->redis_calls[$method] = 0; - } - $this->redis_calls[$method]++; - - try { - return $this->call_redis_method($method, $args); - } catch (Exception $e) { - $e2 = WP_Object_Cache_RedisCallException::create($method, $args, $e); - - // This retry mechanism handles failures Redis extension's retry mechanism cannot work with. - if ($this->is_retriable_error_message($e->getMessage()) && $retry_count < $this->max_retries) { - $this->exception_handler($e2); - usleep($this->retry_interval * 1000); - - // Attempt to refresh the connection if it was successfully established once - // $this->is_redis_connected will be set inside connect_redis() - if ($this->connect_redis()) { - return call_user_func_array([$this, 'call_redis'], [$method, $args, $retry_count + 1]); - } - } - - throw $e2; - } - } - - if ($this->is_redis_failback_flush_enabled() && !$this->do_redis_failback_flush && !empty($wpdb)) { - if ($this->multisite) { - $table = $wpdb->sitemeta; - $col1 = 'meta_key'; - $col2 = 'meta_value'; - } else { - $table = $wpdb->options; - $col1 = 'option_name'; - $col2 = 'option_value'; - } + $this->do_redis_failback_flush = false; + } + } + } + + $this->global_prefix = ( $this->multisite || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix; + + $this->init_versioned_redis_keys(); + } + + public function should_version( $redis_key ) { + return isset( $this->versioned_redis_keys[ $redis_key ] ); + } + + public function set_cache_version( $redis_key, $version ) { + $this->versions[ $redis_key ] = $version; + } + + public function clear_cache_version( $redis_key ) { + unset( $this->versions[ $redis_key ] ); + } + + public function flush_cache_versions() { + $this->versions = array(); + } + + public function get_cache_version( $redis_key ) { + return isset( $this->versions[ $redis_key ] ) ? $this->versions[ $redis_key ] : null; + } + + public function version_key( $key = '', $group = 'default' ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( ! empty( $this->global_groups[ $group ] ) ) { + $prefix = $this->global_prefix; + } else { + $prefix = $this->blog_prefix; + } + + return WP_CACHE_VERSION_KEY_SALT . + json_encode( array( strval( $prefix ), strval( $group ), strval( $key ) ) ); + } + + public function generate_version() { + // Versions are expressed as UUID v4. + return sprintf( + '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', + random_int( 0, 0xffff ), + random_int( 0, 0xffff ), + random_int( 0, 0xffff ), + random_int( 0, 0x0fff ) | 0x4000, + random_int( 0, 0x3fff ) | 0x8000, + random_int( 0, 0xffff ), + random_int( 0, 0xffff ), + random_int( 0, 0xffff ), + ); + } + + private function call_set( $key, $group, $params ) { + $redis_key = $params['key']; + + if ( ! $this->should_version( $redis_key ) ) { + $ps = array( $redis_key, $params['value'] ); + $opt = array(); + if ( $params['nx'] ) { + $opt[] = 'nx'; + } + if ( $params['xx'] ) { + $opt[] = 'xx'; + } + if ( null !== $params['ex'] ) { + $opt['ex'] = $params['ex']; + } + if ( null !== $params['px'] ) { + $opt['px'] = $params['px']; + } + if ( $opt ) { + $ps[] = $opt; + } + return $this->call_redis( 'set', $ps ); + } + + $new_version = $this->generate_version(); + + $result = $this->call_redis( + 'evalSha', + array( + self::$lua_scripts['set']['hash'], + array( + $redis_key, + $this->version_key( $key, $group ), + $this->get_cache_version( $redis_key ) ?? '', + $new_version, + $params['value'], + $params['ex'] ?? '', + $params['px'] ?? '', + var_export( $params['nx'], true ), + var_export( $params['xx'], true ), + var_export( $params['keepttl'], true ), + ), + 2, + ) + ); + + if ( false === $result ) { + $this->clear_cache_version( $redis_key ); + $this->unset_internal( $key, $group ); + $this->debug( 'found inconsistent update during SET operation', $redis_key ); + return false; + } + + $this->set_cache_version( $redis_key, $new_version ); + return $result; + } + + /** + * Sets the data contents into the cache + * + * The cache contents is grouped by the $group parameter followed by the + * $key. This allows for duplicate ids in unique groups. Therefore, naming of + * the group should be used with care and should follow normal function + * naming guidelines outside of core WordPress usage. + * + * The $expire parameter is not used, because the cache will automatically + * expire for each time a page is accessed and PHP finishes. The method is + * more for cache plugins which use files. + * + * @param int|string $key What to call the contents in the cache + * @param mixed $data The contents to store in the cache + * @param string $group Where to group the cache contents + * @param int $expire TTL for the data, in seconds + * @return bool Always returns true + */ + public function set( $key, $data, $group = 'default', $expire = 0 ) { + if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { + return false; + } + + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + return true; + } + + if ( ! $this->should_persist( $group ) ) { + $this->set_internal( $key, $group, self::clone( $data ) ); + return true; + } + + $set_params = $this->new_set_param( + $this->key( $key, $group ), + self::encode_redis_string( $data ), + ); + if ( $expire ) { + $set_params['ex'] = $expire; + } + $succeeded = $this->call_set( $key, $group, $set_params ); + + if ( $succeeded ) { + $this->set_internal( $key, $group, self::clone( $data ) ); + } else { + $this->unset_internal( $key, $group ); + } + + return $succeeded; + } + + /** + * Replace the contents in the cache, if contents already exist + * @see WP_Object_Cache::set() + * + * @param int|string $key What to call the contents in the cache + * @param mixed $data The contents to store in the cache + * @param string $group Where to group the cache contents + * @param int $expire When to expire the cache contents + * @return bool False if not exists, true if contents were replaced + */ + public function replace( $key, $data, $group = 'default', $expire = 0 ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + return false; + } + + if ( ! $this->should_persist( $group ) ) { + return false; + } + + $set_params = $this->new_set_param( + $this->key( $key, $group ), + self::encode_redis_string( $data ), + ); + $set_params['xx'] = true; + if ( $expire ) { + $set_params['ex'] = $expire; + } + $succeeded = $this->call_set( $key, $group, $set_params ); + + if ( $succeeded ) { + $this->set_internal( $key, $group, self::clone( $data ) ); + } else { + $this->unset_internal( $key, $group ); + } + + return $succeeded; + } + + /** + * Retrieves the cache contents, if it exists + * + * The contents will be first attempted to be retrieved by searching by the + * key in the cache group. If the cache is hit (success) then the contents + * are returned. + * + * @param int|string $key What the contents in the cache are called + * @param string $group Where the cache contents are grouped + * @param string $force Whether to force a refetch rather than relying on the local cache (default is false) + * @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of false, a storable value. Passed by reference. Default null. + * @return bool|mixed False on failure to retrieve contents or the cache contents on success + */ + public function get( $key, $group = 'default', $force = false, &$found = null ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + $found = false; + return false; + } + + // Key is set internally, so we can use this value + if ( $this->isset_internal( $key, $group ) && ! $force ) { + $found = true; + return $this->get_internal( $key, $group ); + } + + // Not a persistent group, so don't try Redis if the value doesn't exist + // internally + if ( ! $this->should_persist( $group ) ) { + $found = false; + return false; + } + + try { + $redis_key = $this->key( $key, $group ); + + if ( ! $this->should_version( $redis_key ) ) { + list($value, $found) = self::decode_redis_get( + $this->call_redis( 'get', array( $redis_key ) ), + ); + return $value; + } + + $res = $this->call_redis('mget', array( + array( + $redis_key, + $this->version_key( $key, $group ), + ), + )); + if ( false === $res ) { + return false; + } + + [$res2, $actual_version] = $res; + if ( false === $actual_version ) { + $this->clear_cache_version( $redis_key ); + } else { + $this->set_cache_version( $redis_key, $actual_version ); + } + + list($value, $found) = self::decode_redis_get( $res2 ); + return $value; + } finally { + if ( $found ) { + $this->set_internal( $key, $group, $value ); + } else { + $this->unset_internal( $key, $group ); + } + } + } + + private function call_mget( $redis_keys, $redis_group_key_indexes ) { + if ( ! $redis_keys ) { + return array(); + } + + $should_version_flags = array(); + $redis_params = array(); + + array_map( + function ( + $redis_key, + $group_key + ) use ( + &$redis_params, + &$should_version_flags + ) { + $should_version = $this->should_version( $redis_key ); + $should_version_flags[] = $should_version; + $redis_params[] = $redis_key; + if ( $should_version ) { + $redis_params[] = $this->version_key( $group_key[1], $group_key[0] ); + } + return; + }, + $redis_keys, + $redis_group_key_indexes, + ); + + $res_count = 0; + $res_array = $this->call_redis( 'mget', array( $redis_params ) ); + if ( false === $res_array ) { + return array_fill( 0, count( $redis_keys ), false ); + } + $results = array(); + foreach ( $redis_keys as $i => $redis_key ) { + $results[] = $res_array[ $res_count++ ]; + if ( $should_version_flags[ $i ] ) { + $version = $res_array[ $res_count++ ]; + if ( false === $version ) { + $this->clear_cache_version( $redis_key ); + } else { + $this->set_cache_version( $redis_key, $version ); + } + } + } + assert( count( $res_array ) === $res_count ); + + return $results; + } + + /** + * Retrieves multiple values from the cache in one call. + * + * @param array $keys Array of keys under which the cache contents are stored. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param bool $force Optional. Whether to force an update of the local cache + * from the persistent cache. Default false. + * @return array Array of values organized into groups. + */ + public function get_multiple( $keys, $group = 'default', $force = false ) { + $normal_res = $this->normalized_get_multiple( array( $group => $keys ), $force ); + $group_kv = array_key_exists( $group, $normal_res ) ? $normal_res[ $group ] : array(); + + $result = array(); + foreach ( $keys as $key ) { + $result[ $key ] = array_key_exists( $key, $group_kv ) ? $group_kv[ $key ] : false; + } + return $result; + } + + public function normalized_get_multiple( $groups, $force = false ) { + $get_from_cache = function ( $key, $group ) { + $found = $this->isset_internal( $key, $group ); + + return array( $found ? $this->get_internal( $key, $group ) : null, $found ); + }; + + if ( empty( $groups ) || ! is_array( $groups ) ) { + return false; + } + + $cache = array(); + $redis_group_key_indexes = array(); + $redis_keys = array(); + + foreach ( $groups as $group => $keys ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( ! $this->should_persist( $group ) ) { + foreach ( $keys as $key ) { + if ( $this->is_ignored( $key, $group ) ) { + continue; + } + list($value, $found) = $get_from_cache( $key, $group ); + if ( $found ) { + $cache[ $group ][ $key ] = $value; + } + } + continue; + } + + foreach ( $keys as $key ) { + if ( $this->is_ignored( $key, $group ) ) { + continue; + } + + if ( ! $force && $this->isset_internal( $key, $group ) ) { + $cache[ $group ][ $key ] = $this->get_internal( $key, $group ); + continue; + } + + $redis_keys[] = $this->key( $key, $group ); + $redis_group_key_indexes[] = array( $group, $key ); + } + } + + if ( $redis_keys ) { + $redis_result = $this->call_mget( $redis_keys, $redis_group_key_indexes ); + + array_map( + function ( $result, $group_key ) use ( &$cache ) { + list($value, $found) = $result; + list($group, $key) = $group_key; + + if ( $found ) { + $cache[ $group ][ $key ] = $value; + $this->set_internal( $key, $group, $value ); + } + + return null; + }, + array_map( array( $this, 'decode_redis_get' ), $redis_result ), + $redis_group_key_indexes, + ); + } + + return $cache; + } + + /** + * Remove the contents of the cache key in the group + * + * If the cache key does not exist in the group and $force parameter is set + * to false, then nothing will happen. The $force parameter is set to false + * by default. + * + * @param int|string $key What the contents in the cache are called + * @param string $group Where the cache contents are grouped + * @param bool $force Optional. Whether to force the unsetting of the cache + * key in the group + * @return bool False if the contents weren't deleted and true on success + */ + public function delete( $key, $group = 'default', $force = true ) { + // `force` param is ignored. + if ( empty( $group ) ) { + $group = 'default'; + } + + try { + if ( ! $this->should_persist( $group ) ) { + return true; + } + + $redis_key = $this->key( $key, $group ); + + if ( ! $this->should_version( $redis_key ) ) { + return self::decode_redis_del( $this->call_redis( 'del', array( $redis_key ) ) ) === 1; + } + + $this->clear_cache_version( $redis_key ); + $res = $this->call_redis( 'del', array( $redis_key, $this->version_key( $key, $group ) ) ); + return 0 < self::decode_redis_del( $res ); + } finally { + $this->unset_internal( $key, $group ); + } + } + + private static function new_set_param( $key, $value ) { + return array( + 'key' => $key, + 'value' => $value, + 'ex' => null, // EX must be greater than 0. + 'px' => null, // PX must be greater than 0. + 'nx' => false, + 'xx' => false, + 'keepttl' => false, + ); + } + + /** + * Adds data to the cache if it doesn't already exist. + * + * @uses WP_Object_Cache::_exists Checks to see if the cache already has data. + * @uses WP_Object_Cache::set Sets the data after the checking the cache + * contents existence. + * + * @param int|string $key What to call the contents in the cache + * @param mixed $data The contents to store in the cache + * @param string $group Where to group the cache contents + * @param int $expire When to expire the cache contents + * @return bool False if cache key and group already exist, true on success + */ + public function add( $key, $data, $group = 'default', $expire = 0 ) { + if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { + return false; + } + + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + return true; + } + + if ( ! $this->should_persist( $group ) ) { + return false; + } + + $set_params = $this->new_set_param( + $this->key( $key, $group ), + self::encode_redis_string( $data ), + ); + $set_params['nx'] = true; + if ( $expire ) { + $set_params['ex'] = $expire; + } + $succeeded = $this->call_set( $key, $group, $set_params ); + + if ( $succeeded ) { + $this->set_internal( $key, $group, self::clone( $data ) ); + } else { + $this->unset_internal( $key, $group ); + } + + return $succeeded; + } + + /** + * Decrement numeric cache item's value + * + * @param int|string $key The cache key to increment + * @param int $offset The amount by which to decrement the item's value. Default is 1. + * @param string $group The group the key is in. + * @return false|int False on failure, the item's new value on success. + */ + public function decr( $key, $offset = 1, $group = 'default' ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + return false; + } + + $offset = (int) $offset; + + // If this isn't a persistant group, we have to sort this out ourselves, grumble grumble. + if ( ! $this->should_persist( $group ) ) { + if ( ! $this->isset_internal( $key, $group ) ) { + return false; + } + + $existing = $this->get_internal( $key, $group ); + if ( ! is_numeric( $existing ) ) { + $existing = 0; + } else { + $existing -= $offset; + } + if ( $existing < 0 ) { + $existing = 0; + } + $this->set_internal( $key, $group, $existing ); + return $existing; + } + + $result = $this->call_redis( + 'evalSha', + array( + self::$lua_scripts['decr-by-nover']['hash'], + array( $this->key( $key, $group ), $offset ), + 1, + ) + ); + + if ( is_int( $result ) ) { + $this->set_internal( $key, $group, $result ); + } + return $result; + } + + /** + * Increment numeric cache item's value + * + * @param int|string $key The cache key to increment + * @param int $offset The amount by which to increment the item's value. Default is 1. + * @param string $group The group the key is in. + * @return false|int False on failure, the item's new value on success. + */ + public function incr( $key, $offset = 1, $group = 'default' ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->is_ignored( $key, $group ) ) { + return false; + } + + $offset = (int) $offset; + + // If this isn't a persistant group, we have to sort this out ourselves, grumble grumble. + if ( ! $this->should_persist( $group ) ) { + if ( ! $this->isset_internal( $key, $group ) ) { + return false; + } + + $existing = $this->get_internal( $key, $group ); + if ( ! is_numeric( $existing ) ) { + $existing = 1; + } else { + $existing += $offset; + } + if ( $existing < 0 ) { + $existing = 0; + } + $this->set_internal( $key, $group, $existing ); + return $existing; + } + + $result = $this->call_redis( + 'evalSha', + array( + self::$lua_scripts['incr-by-nover']['hash'], + array( $this->key( $key, $group ), $offset ), + 1, + ) + ); + + if ( is_int( $result ) ) { + $this->set_internal( $key, $group, $result ); + } + return $result; + } + + /** + * Clears the object cache of all data. + * + * @return bool True on success, false on failure. + */ + public function flush() { + $this->flush_cache_versions(); + $this->cache = array(); + return $this->call_redis( 'flushdb' ); + } + + protected function call_redis_method( $method, $args ) { + return call_user_func_array( array( $this->redis, $method ), $args ); + } + + /** + * Wrapper method for calls to Redis, which fails gracefully when Redis is unavailable + * + * @param string $method + * @param mixed $args + * @return mixed + */ + public function call_redis( $method, $args = array(), $retry_count = 0 ) { + global $wpdb; + + if ( $this->is_redis_connected ) { + if ( ! isset( $this->redis_calls[ $method ] ) ) { + $this->redis_calls[ $method ] = 0; + } + ++$this->redis_calls[ $method ]; + + try { + return $this->call_redis_method( $method, $args ); + } catch ( Exception $e ) { + $e2 = WP_Object_Cache_RedisCallException::create( $method, $args, $e ); + + // This retry mechanism handles failures Redis extension's retry mechanism cannot work with. + if ( $this->is_retriable_error_message( $e->getMessage() ) && $retry_count < $this->max_retries ) { + $this->exception_handler( $e2 ); + usleep( $this->retry_interval * 1000 ); + + // Attempt to refresh the connection if it was successfully established once + // $this->is_redis_connected will be set inside connect_redis() + if ( $this->connect_redis() ) { + return call_user_func_array( array( $this, 'call_redis' ), array( $method, $args, $retry_count + 1 ) ); + } + } + + throw $e2; + } + } + + if ( $this->is_redis_failback_flush_enabled() && ! $this->do_redis_failback_flush && ! empty( $wpdb ) ) { + if ( $this->multisite ) { + $table = $wpdb->sitemeta; + $col1 = 'meta_key'; + $col2 = 'meta_value'; + } else { + $table = $wpdb->options; + $col1 = 'option_name'; + $col2 = 'option_value'; + } // @codingStandardsIgnoreStart $wpdb->query("INSERT IGNORE INTO {$table} ({$col1},{$col2}) VALUES ('wp_redis_do_redis_failback_flush',1)"); // @codingStandardsIgnoreEnd - $this->do_redis_failback_flush = true; - } + $this->do_redis_failback_flush = true; + } - return false; - } + return false; + } } WP_Object_Cache::initialize(); diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml new file mode 100644 index 0000000..edb12ef --- /dev/null +++ b/phpcs.ruleset.xml @@ -0,0 +1,37 @@ + + + Generally-applicable sniffs for WordPress plugins + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */vendor/* + */work/* + diff --git a/script/build-test b/script/build-test index 2d63a0d..706b71d 100755 --- a/script/build-test +++ b/script/build-test @@ -2,7 +2,7 @@ set -eu -PHP_VERSION=7.3 \ +PHP_VERSION=7.4 \ PHPUNIT_VERSION=9.3.9 \ REDIS_VERSION=5 \ exec docker-compose -f test/docker-compose.yml build diff --git a/script/test b/script/test index 96447ab..e455900 100755 --- a/script/test +++ b/script/test @@ -2,7 +2,7 @@ set -u -export PHP_VERSION=7.3 +export PHP_VERSION=7.4 export PHPUNIT_VERSION=9.3.9 export REDIS_VERSION=5 diff --git a/test/config/phpredis.php b/test/config/phpredis.php index 8620524..4987ff6 100644 --- a/test/config/phpredis.php +++ b/test/config/phpredis.php @@ -1,37 +1,37 @@ ['alloptions' => true], -]; +$redis_server_default_versioning_keys = array( + 'default' => array( 'alloptions' => true ), +); require_once '/code/object-cache.php'; require_once __DIR__ . '/../../vendor/autoload.php'; -foreach (glob('/code/test/helper/{,*/}*.php', GLOB_BRACE) as $file) { - require $file; +foreach ( glob( '/code/test/helper/{,*/}*.php', GLOB_BRACE ) as $file ) { + require $file; } // Stub function -function apply_filters($name, $value, ...$params) { - return $value; +function apply_filters( $name, $value, ...$params ) { + return $value; } function is_multisite() { - return false; + return false; } -function rand_str($len = 32) { - return substr(md5(uniqid(rand())), 0, $len); +function rand_str( $len = 32 ) { + return substr( md5( uniqid( rand() ) ), 0, $len ); } function get_current_blog_id() { - return 269; + return 269; } diff --git a/test/helper/ExhaustiveOCTestCase.php b/test/helper/ExhaustiveOCTestCase.php index 4f07896..782a621 100644 --- a/test/helper/ExhaustiveOCTestCase.php +++ b/test/helper/ExhaustiveOCTestCase.php @@ -3,31 +3,31 @@ use PHPUnit\Framework\TestCase; abstract class ExhaustiveOCTestCase extends TestCase { - protected static $redis; - protected const GROUP = 'default'; - protected const KEY = 'alloptions'; - protected const VAL = 'sample-value'; - protected const VAL_SUP = 'another-value'; - protected $ocs; + protected static $redis; + protected const GROUP = 'default'; + protected const KEY = 'alloptions'; + protected const VAL = 'sample-value'; + protected const VAL_SUP = 'another-value'; + protected $ocs; - public static function setUpBeforeClass(): void { - parent::setUpBeforeClass(); + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); - global $redis_server; - $redis_server = [ - 'host' => 'redis', - 'port' => 6379, - 'timeout' => 1000, - 'retry_interval' => 100, - ]; + global $redis_server; + $redis_server = array( + 'host' => 'redis', + 'port' => 6379, + 'timeout' => 1000, + 'retry_interval' => 100, + ); - self::$redis = new Redis(); - self::$redis->connect('redis'); - } + self::$redis = new Redis(); + self::$redis->connect( 'redis' ); + } - public function setUp(): void { - global $redis_server_default_versioning_keys, $redis_server_versioning_keys; - self::$redis->flushdb(); - $redis_server_versioning_keys = $redis_server_default_versioning_keys; - } + public function setUp(): void { + global $redis_server_default_versioning_keys, $redis_server_versioning_keys; + self::$redis->flushdb(); + $redis_server_versioning_keys = $redis_server_default_versioning_keys; + } } diff --git a/test/helper/ObjectCacheTestCase.php b/test/helper/ObjectCacheTestCase.php index 1b1ec01..b8bb759 100644 --- a/test/helper/ObjectCacheTestCase.php +++ b/test/helper/ObjectCacheTestCase.php @@ -3,146 +3,146 @@ use PHPUnit\Framework\TestCase; abstract class ObjectCacheTestCase extends TestCase { - protected static $redis; - protected const RIVAL_VERSION = 'deadbeef-dead-beef-dead-beefdeadbeef'; - protected const UNUSED_DUMMY_VERSION = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'; - protected const VERSION_FORMAT = '%x-%x-%x-%x-%x'; - protected const GROUP = 'default'; - protected const KEY = 'alloptions'; - protected const VAL = 'sample-value'; - protected const VAL_SUP = 'another-value'; - protected const VAL_SUP_2 = 'yet-another-value'; - protected const IGN_GROUP = 'ignoredkey'; - protected const IGN_KEY = 'ignoredgroup'; - protected $old_version; - protected $next_versioning_keys; - protected $oc1; - protected $oc2; - protected $oc3; - - public static function encode_redis_string($data) { - return is_numeric($data) && intval($data) === $data - ? $data - : igbinary_serialize($data); - } - - public static function setUpBeforeClass(): void { - parent::setUpBeforeClass(); - - global $redis_server; - $redis_server = [ - 'host' => 'redis', - 'port' => 6379, - 'timeout' => 1000, - 'retry_interval' => 100, - ]; - - self::$redis = new Redis(); - self::$redis->connect('redis'); - } - - protected function set_next_versioning_keys($versioning_keys) { - $this->next_versioning_keys = $versioning_keys; - } - - public function setUp(): void { - global $wp_object_cache, - $redis_server_default_versioning_keys, - $redis_server_versioning_keys, - $redis_server_ignored_keys; - self::$redis->flushdb(); - $redis_server_versioning_keys = - $this->next_versioning_keys ?? $redis_server_default_versioning_keys; - $this->next_versioning_keys = null; - $redis_server_ignored_keys = [$this::IGN_GROUP => [$this::IGN_KEY => true]]; - $wp_object_cache = new WP_Object_Cache(); - $this->oc1 = $wp_object_cache; - $this->oc2 = new WP_Object_Cache(); - $this->oc3 = new WP_Object_Cache(); - $this->old_version = self::UNUSED_DUMMY_VERSION; - } - - protected function assertRedisEquals($expected, $key, $group = 'default') { - list($actual, $found) = WP_Object_Cache::decode_redis_get( - self::$redis->get($this->key($key, $group)), - ); - $this->assertTrue($found); - $this->assertEquals($expected, $actual); - } - - protected function setup_ignored_key() { - global $redis_server_ignored_keys; - - $old = $redis_server_ignored_keys; - $redis_server_ignored_keys = []; - wp_cache_set(self::IGN_KEY, self::VAL, self::IGN_GROUP); - $redis_server_ignored_keys = $old; - } - - protected function assertRedisNonExistent($key, $group = 'default') { - $this->assertFalse(self::$redis->get($this->key($key, $group))); - } - - protected function key($key, $group = 'default') { - global $wp_object_cache; - - return $wp_object_cache->key($key, $group); - } - - protected function version_key($key, $group = 'default') { - global $wp_object_cache; - - return $wp_object_cache->version_key($key, $group); - } - - protected function cache() { - global $wp_object_cache; - - return $wp_object_cache->cache; - } - - protected function cache_version($redis_key) { - global $wp_object_cache; - - return $wp_object_cache->get_cache_version($redis_key); - } - - protected function connection_kept() { - global $wp_object_cache; - - return $wp_object_cache->is_connected(); - } - - protected function set_rival_version() { - self::$redis->set($this->version_key(self::KEY), self::RIVAL_VERSION); - } - - protected function set_sup_value() { - self::$redis->set( - $this->key(self::KEY), - self::encode_redis_string(self::VAL_SUP), - ); - } - - protected function set_sup_2_value() { - self::$redis->set( - $this->key(self::KEY), - self::encode_redis_string(self::VAL_SUP_2), - ); - } - - protected function set_value() { - self::$redis->set( - $this->key(self::KEY), - self::encode_redis_string(self::VAL), - ); - } - - protected function get_and_store_version() { - $this->old_version = self::$redis->get($this->version_key(self::KEY)); - } - - protected function del_val() { - self::$redis->del($this->key(self::KEY)); - } + protected static $redis; + protected const RIVAL_VERSION = 'deadbeef-dead-beef-dead-beefdeadbeef'; + protected const UNUSED_DUMMY_VERSION = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'; + protected const VERSION_FORMAT = '%x-%x-%x-%x-%x'; + protected const GROUP = 'default'; + protected const KEY = 'alloptions'; + protected const VAL = 'sample-value'; + protected const VAL_SUP = 'another-value'; + protected const VAL_SUP_2 = 'yet-another-value'; + protected const IGN_GROUP = 'ignoredkey'; + protected const IGN_KEY = 'ignoredgroup'; + protected $old_version; + protected $next_versioning_keys; + protected $oc1; + protected $oc2; + protected $oc3; + + public static function encode_redis_string( $data ) { + return is_numeric( $data ) && intval( $data ) === $data + ? $data + : igbinary_serialize( $data ); + } + + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + + global $redis_server; + $redis_server = array( + 'host' => 'redis', + 'port' => 6379, + 'timeout' => 1000, + 'retry_interval' => 100, + ); + + self::$redis = new Redis(); + self::$redis->connect( 'redis' ); + } + + protected function set_next_versioning_keys( $versioning_keys ) { + $this->next_versioning_keys = $versioning_keys; + } + + public function setUp(): void { + global $wp_object_cache, + $redis_server_default_versioning_keys, + $redis_server_versioning_keys, + $redis_server_ignored_keys; + self::$redis->flushdb(); + $redis_server_versioning_keys = + $this->next_versioning_keys ?? $redis_server_default_versioning_keys; + $this->next_versioning_keys = null; + $redis_server_ignored_keys = array( $this::IGN_GROUP => array( $this::IGN_KEY => true ) ); + $wp_object_cache = new WP_Object_Cache(); + $this->oc1 = $wp_object_cache; + $this->oc2 = new WP_Object_Cache(); + $this->oc3 = new WP_Object_Cache(); + $this->old_version = self::UNUSED_DUMMY_VERSION; + } + + protected function assertRedisEquals( $expected, $key, $group = 'default' ) { + list($actual, $found) = WP_Object_Cache::decode_redis_get( + self::$redis->get( $this->key( $key, $group ) ), + ); + $this->assertTrue( $found ); + $this->assertEquals( $expected, $actual ); + } + + protected function setup_ignored_key() { + global $redis_server_ignored_keys; + + $old = $redis_server_ignored_keys; + $redis_server_ignored_keys = array(); + wp_cache_set( self::IGN_KEY, self::VAL, self::IGN_GROUP ); + $redis_server_ignored_keys = $old; + } + + protected function assertRedisNonExistent( $key, $group = 'default' ) { + $this->assertFalse( self::$redis->get( $this->key( $key, $group ) ) ); + } + + protected function key( $key, $group = 'default' ) { + global $wp_object_cache; + + return $wp_object_cache->key( $key, $group ); + } + + protected function version_key( $key, $group = 'default' ) { + global $wp_object_cache; + + return $wp_object_cache->version_key( $key, $group ); + } + + protected function cache() { + global $wp_object_cache; + + return $wp_object_cache->cache; + } + + protected function cache_version( $redis_key ) { + global $wp_object_cache; + + return $wp_object_cache->get_cache_version( $redis_key ); + } + + protected function connection_kept() { + global $wp_object_cache; + + return $wp_object_cache->is_connected(); + } + + protected function set_rival_version() { + self::$redis->set( $this->version_key( self::KEY ), self::RIVAL_VERSION ); + } + + protected function set_sup_value() { + self::$redis->set( + $this->key( self::KEY ), + self::encode_redis_string( self::VAL_SUP ), + ); + } + + protected function set_sup_2_value() { + self::$redis->set( + $this->key( self::KEY ), + self::encode_redis_string( self::VAL_SUP_2 ), + ); + } + + protected function set_value() { + self::$redis->set( + $this->key( self::KEY ), + self::encode_redis_string( self::VAL ), + ); + } + + protected function get_and_store_version() { + $this->old_version = self::$redis->get( $this->version_key( self::KEY ) ); + } + + protected function del_val() { + self::$redis->del( $this->key( self::KEY ) ); + } } diff --git a/test/helper/ThrowOnGetObjectCache.php b/test/helper/ThrowOnGetObjectCache.php index 48dbe97..b0a5bd4 100644 --- a/test/helper/ThrowOnGetObjectCache.php +++ b/test/helper/ThrowOnGetObjectCache.php @@ -1,23 +1,23 @@ trigger_error_count++; - } + protected function exception_handler( $exception ) { + ++$this->trigger_error_count; + } - protected function call_redis_method($method, $args) { - if ($method === 'get') { - if ($this->error_ifs[$this->call_count++]) { - throw new RedisTestException($this->error_message); - } - } + protected function call_redis_method( $method, $args ) { + if ( 'get' === $method ) { + if ( $this->error_ifs[ $this->call_count++ ] ) { + throw new RedisTestException( $this->error_message ); + } + } - return parent::call_redis_method($method, $args); - } + return parent::call_redis_method( $method, $args ); + } } diff --git a/test/helper/WPRedisTestCase.php b/test/helper/WPRedisTestCase.php index 9ede31b..0ff626c 100644 --- a/test/helper/WPRedisTestCase.php +++ b/test/helper/WPRedisTestCase.php @@ -6,51 +6,51 @@ * Test the persistent object cache using core's cache tests */ abstract class WPRedisTestCase extends TestCase { - public function assertInternalType($type, $actual) { - $this->assertEquals('int', $type); - $this->assertIsInt($actual); - } + public function assertInternalType( $type, $actual ) { + $this->assertEquals( 'int', $type ); + $this->assertIsInt( $actual ); + } - public static function assertRegExp( - string $pattern, - string $string, - string $message = '' - ): void { - self::assertMatchesRegularExpression($pattern, $string); - } + public static function assertRegExp( + string $pattern, + string $str, + string $message = '' + ): void { + self::assertMatchesRegularExpression( $pattern, $str ); + } - public function setExpectedException($ex) { - $this->expectException($ex); - } + public function setExpectedException( $ex ) { + $this->expectException( $ex ); + } - public function flush_cache() { - self::wp_redis_flush_cache(); - } + public function flush_cache() { + self::wp_redis_flush_cache(); + } - private static function wp_redis_flush_cache() { - global $wp_object_cache; + private static function wp_redis_flush_cache() { + global $wp_object_cache; - $wp_object_cache->cache = []; - wp_cache_flush(); - wp_cache_add_global_groups([ - 'users', - 'userlogins', - 'usermeta', - 'user_meta', - 'useremail', - 'userslugs', - 'site-transient', - 'site-options', - 'blog-lookup', - 'blog-details', - 'rss', - 'global-posts', - 'blog-id-cache', - 'networks', - 'sites', - 'site-details', - 'blog_meta', - ]); - wp_cache_add_non_persistent_groups(['comment', 'counts', 'plugins']); - } + $wp_object_cache->cache = array(); + wp_cache_flush(); + wp_cache_add_global_groups(array( + 'users', + 'userlogins', + 'usermeta', + 'user_meta', + 'useremail', + 'userslugs', + 'site-transient', + 'site-options', + 'blog-lookup', + 'blog-details', + 'rss', + 'global-posts', + 'blog-id-cache', + 'networks', + 'sites', + 'site-details', + 'blog_meta', + )); + wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); + } } diff --git a/test/helper/trait/ConnectionTests.php b/test/helper/trait/ConnectionTests.php index 184dd49..6212165 100644 --- a/test/helper/trait/ConnectionTests.php +++ b/test/helper/trait/ConnectionTests.php @@ -1,9 +1,9 @@ examined(); + public function testConnectionKept() { + $this->examined(); - $this->assertTrue($this->connection_kept()); - } + $this->assertTrue( $this->connection_kept() ); + } } diff --git a/test/helper/trait/ExamineCacheAdd.php b/test/helper/trait/ExamineCacheAdd.php index f3656c0..6f25e95 100644 --- a/test/helper/trait/ExamineCacheAdd.php +++ b/test/helper/trait/ExamineCacheAdd.php @@ -1,7 +1,7 @@ is_redis_connected = false; + $wp_object_cache->is_redis_connected = false; - return wp_cache_tarosky_get_multiple([self::GROUP => [self::KEY]]); - } + return wp_cache_tarosky_get_multiple( array( self::GROUP => array( self::KEY ) ) ); + } } diff --git a/test/helper/trait/ExamineGetMVer.php b/test/helper/trait/ExamineGetMVer.php index d6ec03e..ad2688e 100644 --- a/test/helper/trait/ExamineGetMVer.php +++ b/test/helper/trait/ExamineGetMVer.php @@ -1,7 +1,7 @@ [self::KEY]]); - } + protected function examined() { + return wp_cache_tarosky_get_multiple( array( self::GROUP => array( self::KEY ) ) ); + } } diff --git a/test/helper/trait/NoConnectionTests.php b/test/helper/trait/NoConnectionTests.php index cf2ef5c..7926e1b 100644 --- a/test/helper/trait/NoConnectionTests.php +++ b/test/helper/trait/NoConnectionTests.php @@ -1,9 +1,9 @@ examined(); + public function testNoConnectionKept() { + $this->examined(); - $this->assertFalse($this->connection_kept()); - } + $this->assertFalse( $this->connection_kept() ); + } } diff --git a/test/helper/trait/TestExamFails.php b/test/helper/trait/TestExamFails.php index bef169e..a75ae14 100644 --- a/test/helper/trait/TestExamFails.php +++ b/test/helper/trait/TestExamFails.php @@ -1,7 +1,7 @@ assertFalse($this->examined()); - } + public function testExamFails() { + $this->assertFalse( $this->examined() ); + } } diff --git a/test/helper/trait/TestExamGetMNoConnection.php b/test/helper/trait/TestExamGetMNoConnection.php index 77d5844..cb66560 100644 --- a/test/helper/trait/TestExamGetMNoConnection.php +++ b/test/helper/trait/TestExamGetMNoConnection.php @@ -1,10 +1,10 @@ assertEqualsCanonicalizing( - json_encode([], JSON_PRETTY_PRINT), - json_encode($this->examined(), JSON_PRETTY_PRINT), - ); - } + public function testExamGetMNoConnection() { + $this->assertEqualsCanonicalizing( + json_encode( array(), JSON_PRETTY_PRINT ), + json_encode( $this->examined(), JSON_PRETTY_PRINT ), + ); + } } diff --git a/test/helper/trait/TestExamGetMNone.php b/test/helper/trait/TestExamGetMNone.php index a86d98e..770d64d 100644 --- a/test/helper/trait/TestExamGetMNone.php +++ b/test/helper/trait/TestExamGetMNone.php @@ -1,10 +1,10 @@ assertEquals( - json_encode([], JSON_PRETTY_PRINT), - json_encode($this->examined(), JSON_PRETTY_PRINT), - ); - } + public function testExamGetMNone() { + $this->assertEquals( + json_encode( array(), JSON_PRETTY_PRINT ), + json_encode( $this->examined(), JSON_PRETTY_PRINT ), + ); + } } diff --git a/test/helper/trait/TestExamGetMVer.php b/test/helper/trait/TestExamGetMVer.php index 0a545bc..758924e 100644 --- a/test/helper/trait/TestExamGetMVer.php +++ b/test/helper/trait/TestExamGetMVer.php @@ -1,10 +1,10 @@ assertEqualsCanonicalizing( - json_encode([self::GROUP => [self::KEY => self::VAL]], JSON_PRETTY_PRINT), - json_encode($this->examined(), JSON_PRETTY_PRINT), - ); - } + public function testExamGetMVer() { + $this->assertEqualsCanonicalizing( + json_encode( array( self::GROUP => array( self::KEY => self::VAL ) ), JSON_PRETTY_PRINT ), + json_encode( $this->examined(), JSON_PRETTY_PRINT ), + ); + } } diff --git a/test/helper/trait/TestExamGetSucceeds.php b/test/helper/trait/TestExamGetSucceeds.php index 2ddc409..f7bb8e8 100644 --- a/test/helper/trait/TestExamGetSucceeds.php +++ b/test/helper/trait/TestExamGetSucceeds.php @@ -1,7 +1,7 @@ assertEquals(self::VAL, $this->examined()); - } + public function testExamGetSucceeds() { + $this->assertEquals( self::VAL, $this->examined() ); + } } diff --git a/test/helper/trait/TestExamSucceeds.php b/test/helper/trait/TestExamSucceeds.php index da87f03..ad92a4e 100644 --- a/test/helper/trait/TestExamSucceeds.php +++ b/test/helper/trait/TestExamSucceeds.php @@ -1,7 +1,7 @@ assertTrue($this->examined()); - } + public function testExamSucceeds() { + $this->assertTrue( $this->examined() ); + } } diff --git a/test/helper/trait/TestInternalValExists.php b/test/helper/trait/TestInternalValExists.php index a346ebd..f2a2810 100644 --- a/test/helper/trait/TestInternalValExists.php +++ b/test/helper/trait/TestInternalValExists.php @@ -1,10 +1,10 @@ examined(); + public function testInternalValExists() { + $this->examined(); - $redis_key = $this->key(self::KEY); - $this->assertEquals(self::VAL, $this->cache()[$redis_key]); - } + $redis_key = $this->key( self::KEY ); + $this->assertEquals( self::VAL, $this->cache()[ $redis_key ] ); + } } diff --git a/test/helper/trait/TestInternalValNotExist.php b/test/helper/trait/TestInternalValNotExist.php index 47e5c41..efa3e4b 100644 --- a/test/helper/trait/TestInternalValNotExist.php +++ b/test/helper/trait/TestInternalValNotExist.php @@ -1,10 +1,10 @@ examined(); + public function testInternalValNotExist() { + $this->examined(); - $redis_key = $this->key(self::KEY); - $this->assertArrayNotHasKey($redis_key, $this->cache()); - } + $redis_key = $this->key( self::KEY ); + $this->assertArrayNotHasKey( $redis_key, $this->cache() ); + } } diff --git a/test/helper/trait/TestInternalVersionDefeated.php b/test/helper/trait/TestInternalVersionDefeated.php index 93f2784..af61dc7 100644 --- a/test/helper/trait/TestInternalVersionDefeated.php +++ b/test/helper/trait/TestInternalVersionDefeated.php @@ -1,12 +1,12 @@ examined(); + public function testInternalVersionDefeated() { + $this->examined(); - $this->assertEquals( - self::RIVAL_VERSION, - $this->cache_version($this->key(self::KEY)), - ); - } + $this->assertEquals( + self::RIVAL_VERSION, + $this->cache_version( $this->key( self::KEY ) ), + ); + } } diff --git a/test/helper/trait/TestInternalVersionLatest.php b/test/helper/trait/TestInternalVersionLatest.php index f24dc5c..de329cd 100644 --- a/test/helper/trait/TestInternalVersionLatest.php +++ b/test/helper/trait/TestInternalVersionLatest.php @@ -1,11 +1,11 @@ examined(); + public function testInternalVersionLatest() { + $this->examined(); - $redis_key = $this->key(self::KEY); - $version = self::$redis->get($this->version_key(self::KEY)); - $this->assertEquals($version, $this->cache_version($redis_key)); - } + $redis_key = $this->key( self::KEY ); + $version = self::$redis->get( $this->version_key( self::KEY ) ); + $this->assertEquals( $version, $this->cache_version( $redis_key ) ); + } } diff --git a/test/helper/trait/TestInternalVersionNotExist.php b/test/helper/trait/TestInternalVersionNotExist.php index 695bb56..557c9ae 100644 --- a/test/helper/trait/TestInternalVersionNotExist.php +++ b/test/helper/trait/TestInternalVersionNotExist.php @@ -1,10 +1,10 @@ examined(); + public function testInternalVersionNotExist() { + $this->examined(); - $redis_key = $this->key(self::KEY); - $this->assertNull($this->cache_version($redis_key)); - } + $redis_key = $this->key( self::KEY ); + $this->assertNull( $this->cache_version( $redis_key ) ); + } } diff --git a/test/helper/trait/TestInternalVersionOld.php b/test/helper/trait/TestInternalVersionOld.php index 14a5c39..acf9412 100644 --- a/test/helper/trait/TestInternalVersionOld.php +++ b/test/helper/trait/TestInternalVersionOld.php @@ -1,12 +1,12 @@ examined(); + public function testInternalVersionOld() { + $this->examined(); - $this->assertEquals( - $this->old_version, - $this->cache_version($this->key(self::KEY)), - ); - } + $this->assertEquals( + $this->old_version, + $this->cache_version( $this->key( self::KEY ) ), + ); + } } diff --git a/test/helper/trait/TestRedisIgnoredValExists.php b/test/helper/trait/TestRedisIgnoredValExists.php index 12bf5c7..af800c7 100644 --- a/test/helper/trait/TestRedisIgnoredValExists.php +++ b/test/helper/trait/TestRedisIgnoredValExists.php @@ -1,8 +1,8 @@ examined(); - $this->assertRedisEquals(self::VAL, self::IGN_KEY, self::IGN_GROUP); - } + public function testRedisIgnoredValExists() { + $this->examined(); + $this->assertRedisEquals( self::VAL, self::IGN_KEY, self::IGN_GROUP ); + } } diff --git a/test/helper/trait/TestRedisIgnoredValNotExist.php b/test/helper/trait/TestRedisIgnoredValNotExist.php index ffeee29..9aa46fc 100644 --- a/test/helper/trait/TestRedisIgnoredValNotExist.php +++ b/test/helper/trait/TestRedisIgnoredValNotExist.php @@ -1,8 +1,8 @@ examined(); - $this->assertRedisNonExistent(self::IGN_KEY, self::IGN_GROUP); - } + public function testRedisIgnoredValNotExist() { + $this->examined(); + $this->assertRedisNonExistent( self::IGN_KEY, self::IGN_GROUP ); + } } diff --git a/test/helper/trait/TestRedisValExists.php b/test/helper/trait/TestRedisValExists.php index 261d599..9e65843 100644 --- a/test/helper/trait/TestRedisValExists.php +++ b/test/helper/trait/TestRedisValExists.php @@ -1,8 +1,8 @@ examined(); - $this->assertRedisEquals(self::VAL, self::KEY); - } + public function testRedisValExists() { + $this->examined(); + $this->assertRedisEquals( self::VAL, self::KEY ); + } } diff --git a/test/helper/trait/TestRedisValNotExist.php b/test/helper/trait/TestRedisValNotExist.php index fe19680..dab895d 100644 --- a/test/helper/trait/TestRedisValNotExist.php +++ b/test/helper/trait/TestRedisValNotExist.php @@ -1,8 +1,8 @@ examined(); - $this->assertFalse(self::$redis->get($this->key(self::KEY))); - } + public function testRedisValNotExist() { + $this->examined(); + $this->assertFalse( self::$redis->get( $this->key( self::KEY ) ) ); + } } diff --git a/test/helper/trait/TestRedisValSup2Exists.php b/test/helper/trait/TestRedisValSup2Exists.php index df3d52d..74dae7f 100644 --- a/test/helper/trait/TestRedisValSup2Exists.php +++ b/test/helper/trait/TestRedisValSup2Exists.php @@ -1,8 +1,8 @@ examined(); - $this->assertRedisEquals(self::VAL_SUP_2, self::KEY); - } + public function testRedisValSupExists() { + $this->examined(); + $this->assertRedisEquals( self::VAL_SUP_2, self::KEY ); + } } diff --git a/test/helper/trait/TestRedisValSupExists.php b/test/helper/trait/TestRedisValSupExists.php index c2e895b..7851a8e 100644 --- a/test/helper/trait/TestRedisValSupExists.php +++ b/test/helper/trait/TestRedisValSupExists.php @@ -1,8 +1,8 @@ examined(); - $this->assertRedisEquals(self::VAL_SUP, self::KEY); - } + public function testRedisValSupExists() { + $this->examined(); + $this->assertRedisEquals( self::VAL_SUP, self::KEY ); + } } diff --git a/test/helper/trait/TestRedisVersionDefeated.php b/test/helper/trait/TestRedisVersionDefeated.php index ae340c8..85a8d02 100644 --- a/test/helper/trait/TestRedisVersionDefeated.php +++ b/test/helper/trait/TestRedisVersionDefeated.php @@ -1,12 +1,12 @@ examined(); + public function testRedisVersionDefeated() { + $this->examined(); - $this->assertEquals( - self::RIVAL_VERSION, - self::$redis->get($this->version_key(self::KEY)), - ); - } + $this->assertEquals( + self::RIVAL_VERSION, + self::$redis->get( $this->version_key( self::KEY ) ), + ); + } } diff --git a/test/helper/trait/TestRedisVersionNotChanged.php b/test/helper/trait/TestRedisVersionNotChanged.php index d4db3dd..772bc16 100644 --- a/test/helper/trait/TestRedisVersionNotChanged.php +++ b/test/helper/trait/TestRedisVersionNotChanged.php @@ -1,10 +1,10 @@ examined(); + public function testRedisVersionNotChanged() { + $this->examined(); - $version = self::$redis->get($this->version_key(self::KEY)); - $this->assertEquals($this->old_version, $version); - } + $version = self::$redis->get( $this->version_key( self::KEY ) ); + $this->assertEquals( $this->old_version, $version ); + } } diff --git a/test/helper/trait/TestRedisVersionNotExist.php b/test/helper/trait/TestRedisVersionNotExist.php index 3d299e1..2e3cc30 100644 --- a/test/helper/trait/TestRedisVersionNotExist.php +++ b/test/helper/trait/TestRedisVersionNotExist.php @@ -1,9 +1,9 @@ examined(); + public function testRedisVersionNotExist() { + $this->examined(); - $this->assertFalse(self::$redis->get($this->version_key(self::KEY))); - } + $this->assertFalse( self::$redis->get( $this->version_key( self::KEY ) ) ); + } } diff --git a/test/helper/trait/TestRedisVersionRenewed.php b/test/helper/trait/TestRedisVersionRenewed.php index 684d4b2..11c50b0 100644 --- a/test/helper/trait/TestRedisVersionRenewed.php +++ b/test/helper/trait/TestRedisVersionRenewed.php @@ -1,12 +1,12 @@ examined(); + public function testRedisVersionRenewed() { + $this->examined(); - $version = self::$redis->get($this->version_key(self::KEY)); - $this->assertNotEquals(self::RIVAL_VERSION, $version); - $this->assertNotEquals($this->old_version, $version); - $this->assertStringMatchesFormat(self::VERSION_FORMAT, $version); - } + $version = self::$redis->get( $this->version_key( self::KEY ) ); + $this->assertNotEquals( self::RIVAL_VERSION, $version ); + $this->assertNotEquals( $this->old_version, $version ); + $this->assertStringMatchesFormat( self::VERSION_FORMAT, $version ); + } } diff --git a/test/test/tarosky/add-ex/AddExInexistentRacingTest.php b/test/test/tarosky/add-ex/AddExInexistentRacingTest.php index ca528d8..4a826c1 100644 --- a/test/test/tarosky/add-ex/AddExInexistentRacingTest.php +++ b/test/test/tarosky/add-ex/AddExInexistentRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_add() with expiration as an inexistent key before exists() call. class AddExInexistentRacingTest extends AddInexistentRacingTest { - use ExamineCacheAddEx; + use ExamineCacheAddEx; } diff --git a/test/test/tarosky/add-ex/AddExInexistentTest.php b/test/test/tarosky/add-ex/AddExInexistentTest.php index fa8e370..f64a01c 100644 --- a/test/test/tarosky/add-ex/AddExInexistentTest.php +++ b/test/test/tarosky/add-ex/AddExInexistentTest.php @@ -4,5 +4,5 @@ // wp_cache_add() with expiration as an inexistent key. class AddExInexistentTest extends AddInexistentTest { - use ExamineCacheAddEx; + use ExamineCacheAddEx; } diff --git a/test/test/tarosky/add-ex/AddExManagedRacingTest.php b/test/test/tarosky/add-ex/AddExManagedRacingTest.php index 4b620ac..2717703 100644 --- a/test/test/tarosky/add-ex/AddExManagedRacingTest.php +++ b/test/test/tarosky/add-ex/AddExManagedRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_add() with expiration to an existing managed key before exists() call. class AddExManagedRacingTest extends AddManagedRacingTest { - use ExamineCacheAddEx; + use ExamineCacheAddEx; } diff --git a/test/test/tarosky/add-ex/AddExManagedTest.php b/test/test/tarosky/add-ex/AddExManagedTest.php index b0e739f..0edb6bc 100644 --- a/test/test/tarosky/add-ex/AddExManagedTest.php +++ b/test/test/tarosky/add-ex/AddExManagedTest.php @@ -4,5 +4,5 @@ // wp_cache_add() with expiration to an existing managed key. class AddExManagedTest extends AddManagedTest { - use ExamineCacheAddEx; + use ExamineCacheAddEx; } diff --git a/test/test/tarosky/add-ex/AddExUnmanagedTest.php b/test/test/tarosky/add-ex/AddExUnmanagedTest.php index 5ef5b79..55f6d34 100644 --- a/test/test/tarosky/add-ex/AddExUnmanagedTest.php +++ b/test/test/tarosky/add-ex/AddExUnmanagedTest.php @@ -4,5 +4,5 @@ // wp_cache_add() with expiration to an existing unmanaged key. class AddExUnmanagedTest extends AddUnmanagedTest { - use ExamineCacheAddEx; + use ExamineCacheAddEx; } diff --git a/test/test/tarosky/add/AddIgnoredTest.php b/test/test/tarosky/add/AddIgnoredTest.php index 81c0ac4..6e81f1c 100644 --- a/test/test/tarosky/add/AddIgnoredTest.php +++ b/test/test/tarosky/add/AddIgnoredTest.php @@ -2,10 +2,10 @@ // wp_cache_add() an ignored key. class AddIgnoredTest extends ObjectCacheTestCase { - protected function examined() { - return wp_cache_add(self::IGN_KEY, self::VAL, self::IGN_GROUP); - } + protected function examined() { + return wp_cache_add( self::IGN_KEY, self::VAL, self::IGN_GROUP ); + } - use TestExamSucceeds; - use TestRedisIgnoredValNotExist; + use TestExamSucceeds; + use TestRedisIgnoredValNotExist; } diff --git a/test/test/tarosky/add/AddInexistentRacingTest.php b/test/test/tarosky/add/AddInexistentRacingTest.php index ef68558..ff523dc 100644 --- a/test/test/tarosky/add/AddInexistentRacingTest.php +++ b/test/test/tarosky/add/AddInexistentRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_add() as an inexistent key. class AddInexistentRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_sup_value(); - } + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheAdd; - use TestExamFails; - use TestRedisValSupExists; - use TestRedisVersionDefeated; - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Internal cache won't be changed because the data keeps unchanged. - use ConnectionTests; + use ExamineCacheAdd; + use TestExamFails; + use TestRedisValSupExists; + use TestRedisVersionDefeated; + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Internal cache won't be changed because the data keeps unchanged. + use ConnectionTests; } diff --git a/test/test/tarosky/add/AddInexistentTest.php b/test/test/tarosky/add/AddInexistentTest.php index a6c1ab0..c8fda08 100644 --- a/test/test/tarosky/add/AddInexistentTest.php +++ b/test/test/tarosky/add/AddInexistentTest.php @@ -2,11 +2,11 @@ // wp_cache_add() as an inexistent key. class AddInexistentTest extends ObjectCacheTestCase { - use ExamineCacheAdd; - use TestExamSucceeds; - use TestRedisValExists; // Added - use TestRedisVersionRenewed; // Added - use TestInternalVersionLatest; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheAdd; + use TestExamSucceeds; + use TestRedisValExists; // Added + use TestRedisVersionRenewed; // Added + use TestInternalVersionLatest; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/add/AddManagedRacingTest.php b/test/test/tarosky/add/AddManagedRacingTest.php index 41e50ac..68084e7 100644 --- a/test/test/tarosky/add/AddManagedRacingTest.php +++ b/test/test/tarosky/add/AddManagedRacingTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_add() to an existing managed key before exists() call. class AddManagedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_2_value(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_2_value(); + } - use ExamineCacheAdd; - use TestExamFails; - use TestRedisValSup2Exists; - use TestRedisVersionDefeated; - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Removed - use ConnectionTests; + use ExamineCacheAdd; + use TestExamFails; + use TestRedisValSup2Exists; + use TestRedisVersionDefeated; + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Removed + use ConnectionTests; } diff --git a/test/test/tarosky/add/AddManagedTest.php b/test/test/tarosky/add/AddManagedTest.php index 27f533c..e1ac5fb 100644 --- a/test/test/tarosky/add/AddManagedTest.php +++ b/test/test/tarosky/add/AddManagedTest.php @@ -2,18 +2,18 @@ // wp_cache_add() to an existing managed key. class AddManagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + } - use ExamineCacheAdd; - use TestExamFails; - use TestRedisValSupExists; // Not changed - use TestRedisVersionNotChanged; - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Removed - use ConnectionTests; + use ExamineCacheAdd; + use TestExamFails; + use TestRedisValSupExists; // Not changed + use TestRedisVersionNotChanged; + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Removed + use ConnectionTests; } diff --git a/test/test/tarosky/add/AddUnmanagedTest.php b/test/test/tarosky/add/AddUnmanagedTest.php index d6951e5..da85218 100644 --- a/test/test/tarosky/add/AddUnmanagedTest.php +++ b/test/test/tarosky/add/AddUnmanagedTest.php @@ -2,17 +2,17 @@ // wp_cache_add() to an existing unmanaged key. class AddUnmanagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_sup_value(); - } + $this->set_sup_value(); + } - use ExamineCacheAdd; - use TestExamFails; - use TestRedisValSupExists; // Data won't be changed because of the original specification. - use TestRedisVersionNotExist; // Not set - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Not changed - use ConnectionTests; + use ExamineCacheAdd; + use TestExamFails; + use TestRedisValSupExists; // Data won't be changed because of the original specification. + use TestRedisVersionNotExist; // Not set + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Not changed + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteIgnoredTest.php b/test/test/tarosky/delete/DeleteIgnoredTest.php index 18a0ef7..d2b8198 100644 --- a/test/test/tarosky/delete/DeleteIgnoredTest.php +++ b/test/test/tarosky/delete/DeleteIgnoredTest.php @@ -2,16 +2,16 @@ // wp_cache_delete() doesn't ignore ignored keys. class DeleteIgnoredTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->setup_ignored_key(); - } + $this->setup_ignored_key(); + } - protected function examined() { - return wp_cache_delete(self::IGN_KEY, self::IGN_GROUP); - } + protected function examined() { + return wp_cache_delete( self::IGN_KEY, self::IGN_GROUP ); + } - use TestExamSucceeds; - use TestRedisIgnoredValNotExist; + use TestExamSucceeds; + use TestRedisIgnoredValNotExist; } diff --git a/test/test/tarosky/delete/DeleteInexistentRacingTest.php b/test/test/tarosky/delete/DeleteInexistentRacingTest.php index 0c3124b..7ad85bd 100644 --- a/test/test/tarosky/delete/DeleteInexistentRacingTest.php +++ b/test/test/tarosky/delete/DeleteInexistentRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_delete()-ing an inexistent key. class DeleteInexistentRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_sup_value(); - } + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheDelete; - use TestExamSucceeds; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamSucceeds; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteInexistentTest.php b/test/test/tarosky/delete/DeleteInexistentTest.php index ac7a2d4..f477bad 100644 --- a/test/test/tarosky/delete/DeleteInexistentTest.php +++ b/test/test/tarosky/delete/DeleteInexistentTest.php @@ -2,11 +2,11 @@ // wp_cache_delete() an inexistent key. class DeleteInexistentTest extends ObjectCacheTestCase { - use ExamineCacheDelete; - use TestExamFails; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamFails; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteManagedRacingDeleteTest.php b/test/test/tarosky/delete/DeleteManagedRacingDeleteTest.php index 96dca23..d3fda69 100644 --- a/test/test/tarosky/delete/DeleteManagedRacingDeleteTest.php +++ b/test/test/tarosky/delete/DeleteManagedRacingDeleteTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_delete()-ing an existing managed key. class DeleteManagedRacingDeleteTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->del_val(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->del_val(); + } - use ExamineCacheDelete; - use TestExamSucceeds; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamSucceeds; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteManagedRacingSetTest.php b/test/test/tarosky/delete/DeleteManagedRacingSetTest.php index ffe47f8..c39bf52 100644 --- a/test/test/tarosky/delete/DeleteManagedRacingSetTest.php +++ b/test/test/tarosky/delete/DeleteManagedRacingSetTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_delete()-ing an existing managed key. class DeleteManagedRacingSetTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_value(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheDelete; - use TestExamSucceeds; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamSucceeds; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteManagedTest.php b/test/test/tarosky/delete/DeleteManagedTest.php index 87bd0fd..8613143 100644 --- a/test/test/tarosky/delete/DeleteManagedTest.php +++ b/test/test/tarosky/delete/DeleteManagedTest.php @@ -2,18 +2,18 @@ // wp_cache_delete() an existing managed key. class DeleteManagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + } - use ExamineCacheDelete; - use TestExamSucceeds; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamSucceeds; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/delete/DeleteUnmanagedTest.php b/test/test/tarosky/delete/DeleteUnmanagedTest.php index 05723ea..1806e1c 100644 --- a/test/test/tarosky/delete/DeleteUnmanagedTest.php +++ b/test/test/tarosky/delete/DeleteUnmanagedTest.php @@ -2,17 +2,17 @@ // wp_cache_delete() an existing unmanaged key. class DeleteUnmanagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineCacheDelete; - use TestExamSucceeds; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheDelete; + use TestExamSucceeds; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMIgnoredTest.php b/test/test/tarosky/get-m/GetMIgnoredTest.php index 1ee7153..13254a7 100644 --- a/test/test/tarosky/get-m/GetMIgnoredTest.php +++ b/test/test/tarosky/get-m/GetMIgnoredTest.php @@ -2,17 +2,17 @@ // wp_cache_tarosky_get_multiple() an ignored key. class GetMIgnoredTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->setup_ignored_key(); - } + $this->setup_ignored_key(); + } - protected function examined() { - return wp_cache_tarosky_get_multiple([self::GROUP => [self::KEY]]); - } + protected function examined() { + return wp_cache_tarosky_get_multiple( array( self::GROUP => array( self::KEY ) ) ); + } - public function testGetM() { - $this->assertEquals([], $this->examined()); - } + public function testGetM() { + $this->assertEquals( array(), $this->examined() ); + } } diff --git a/test/test/tarosky/get-m/GetMInexistentUnversionedRacingTest.php b/test/test/tarosky/get-m/GetMInexistentUnversionedRacingTest.php index 81c2c08..3ddbd45 100644 --- a/test/test/tarosky/get-m/GetMInexistentUnversionedRacingTest.php +++ b/test/test/tarosky/get-m/GetMInexistentUnversionedRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_tarosky_get_multiple() an inexistent unversioned value. class GetMInexistentUnversionedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; - use TestRedisVersionNotExist; // Not set - use TestInternalVersionNotExist; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; + use TestRedisVersionNotExist; // Not set + use TestInternalVersionNotExist; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMInexistentUnversionedTest.php b/test/test/tarosky/get-m/GetMInexistentUnversionedTest.php index 6d6516e..e76a20e 100644 --- a/test/test/tarosky/get-m/GetMInexistentUnversionedTest.php +++ b/test/test/tarosky/get-m/GetMInexistentUnversionedTest.php @@ -2,16 +2,16 @@ // wp_cache_tarosky_get_multiple() an inexistent unversioned value. class GetMInexistentUnversionedTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); - } + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); + } - use ExamineGetMVer; - use TestExamGetMNone; - use TestRedisValNotExist; - use TestRedisVersionNotExist; // Not set - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMNone; + use TestRedisValNotExist; + use TestRedisVersionNotExist; // Not set + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMInexistentVersionedRacingTest.php b/test/test/tarosky/get-m/GetMInexistentVersionedRacingTest.php index 4fb02e3..3529f9a 100644 --- a/test/test/tarosky/get-m/GetMInexistentVersionedRacingTest.php +++ b/test/test/tarosky/get-m/GetMInexistentVersionedRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_tarosky_get_multiple() an inexistent versioned value. class GetMInexistentVersionedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_value(); - } + $this->set_rival_version(); + $this->set_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; // Not changed - use TestRedisVersionDefeated; // Not changed - use TestInternalVersionDefeated; - use TestInternalValExists; - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; // Not changed + use TestRedisVersionDefeated; // Not changed + use TestInternalVersionDefeated; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMInexistentVersionedTest.php b/test/test/tarosky/get-m/GetMInexistentVersionedTest.php index fa04fe4..286a2ed 100644 --- a/test/test/tarosky/get-m/GetMInexistentVersionedTest.php +++ b/test/test/tarosky/get-m/GetMInexistentVersionedTest.php @@ -2,11 +2,11 @@ // wp_cache_tarosky_get_multiple() an inexistent versioned value. class GetMInexistentVersionedTest extends ObjectCacheTestCase { - use ExamineGetMVer; - use TestExamGetMNone; - use TestRedisValNotExist; // Not changed - use TestRedisVersionNotExist; // Not changed - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Not set - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMNone; + use TestRedisValNotExist; // Not changed + use TestRedisVersionNotExist; // Not changed + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Not set + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMManagedUnversionedRacingTest.php b/test/test/tarosky/get-m/GetMManagedUnversionedRacingTest.php index d8ca0d4..f129385 100644 --- a/test/test/tarosky/get-m/GetMManagedUnversionedRacingTest.php +++ b/test/test/tarosky/get-m/GetMManagedUnversionedRacingTest.php @@ -2,19 +2,19 @@ // Race condition occurred during wp_cache_tarosky_get_multiple() to an existing managed unversioned value. class GetMManagedUnversionedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->set_sup_value(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->set_sup_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValSupExists; // Not changed - use TestRedisVersionNotExist; // Not set - use TestInternalVersionNotExist; - use TestInternalValExists; // Not changed - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValSupExists; // Not changed + use TestRedisVersionNotExist; // Not set + use TestInternalVersionNotExist; + use TestInternalValExists; // Not changed + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMManagedUnversionedTest.php b/test/test/tarosky/get-m/GetMManagedUnversionedTest.php index c978dc5..da1a626 100644 --- a/test/test/tarosky/get-m/GetMManagedUnversionedTest.php +++ b/test/test/tarosky/get-m/GetMManagedUnversionedTest.php @@ -2,18 +2,18 @@ // wp_cache_tarosky_get_multiple() to an existing managed unversioned value. class GetMManagedUnversionedTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - } + wp_cache_set( self::KEY, self::VAL ); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; - use TestRedisVersionNotExist; // Not updated - use TestInternalVersionNotExist; - use TestInternalValExists; // Not updated - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; + use TestRedisVersionNotExist; // Not updated + use TestInternalVersionNotExist; + use TestInternalValExists; // Not updated + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMManagedVersionedRacingForceTest.php b/test/test/tarosky/get-m/GetMManagedVersionedRacingForceTest.php index 021872d..8324b55 100644 --- a/test/test/tarosky/get-m/GetMManagedVersionedRacingForceTest.php +++ b/test/test/tarosky/get-m/GetMManagedVersionedRacingForceTest.php @@ -3,23 +3,23 @@ // Race condition occurred during forcibly wp_cache_tarosky_get_multiple() to // an existing managed versioned value. class GetMManagedVersionedRacingForceTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_value(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_value(); + } - protected function examined() { - return wp_cache_tarosky_get_multiple([self::GROUP => [self::KEY]], true); - } + protected function examined() { + return wp_cache_tarosky_get_multiple( array( self::GROUP => array( self::KEY ) ), true ); + } - use TestExamGetMVer; - use TestRedisValExists; - use TestRedisVersionDefeated; - use TestInternalVersionDefeated; - use TestInternalValExists; - use ConnectionTests; + use TestExamGetMVer; + use TestRedisValExists; + use TestRedisVersionDefeated; + use TestInternalVersionDefeated; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMManagedVersionedRacingTest.php b/test/test/tarosky/get-m/GetMManagedVersionedRacingTest.php index 4999406..c5ae52b 100644 --- a/test/test/tarosky/get-m/GetMManagedVersionedRacingTest.php +++ b/test/test/tarosky/get-m/GetMManagedVersionedRacingTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_tarosky_get_multiple() to an existing managed versioned value. class GetMManagedVersionedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_value(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValSupExists; // Not changed - use TestRedisVersionDefeated; // Not changed - use TestInternalVersionOld; - use TestInternalValExists; - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValSupExists; // Not changed + use TestRedisVersionDefeated; // Not changed + use TestInternalVersionOld; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMManagedVersionedTest.php b/test/test/tarosky/get-m/GetMManagedVersionedTest.php index 2e516af..69ef8a3 100644 --- a/test/test/tarosky/get-m/GetMManagedVersionedTest.php +++ b/test/test/tarosky/get-m/GetMManagedVersionedTest.php @@ -2,18 +2,18 @@ // wp_cache_tarosky_get_multiple() to an existing managed versioned value. class GetMManagedVersionedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; // Not updated - use TestRedisVersionNotChanged; - use TestInternalVersionOld; - use TestInternalValExists; - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; // Not updated + use TestRedisVersionNotChanged; + use TestInternalVersionOld; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMNoConnectionTest.php b/test/test/tarosky/get-m/GetMNoConnectionTest.php index 17622cf..d9793ab 100644 --- a/test/test/tarosky/get-m/GetMNoConnectionTest.php +++ b/test/test/tarosky/get-m/GetMNoConnectionTest.php @@ -2,18 +2,18 @@ // wp_cache_tarosky_get_multiple() when disconnected class GetMNoConnectionTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineGetMNoConnection; - use TestExamGetMNoConnection; - use TestRedisValExists; - use TestRedisVersionNotExist; // Not updated - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Not updated - use NoConnectionTests; + use ExamineGetMNoConnection; + use TestExamGetMNoConnection; + use TestRedisValExists; + use TestRedisVersionNotExist; // Not updated + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Not updated + use NoConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMUnmanagedUnversionedTest.php b/test/test/tarosky/get-m/GetMUnmanagedUnversionedTest.php index c3422f2..e0c5692 100644 --- a/test/test/tarosky/get-m/GetMUnmanagedUnversionedTest.php +++ b/test/test/tarosky/get-m/GetMUnmanagedUnversionedTest.php @@ -2,18 +2,18 @@ // wp_cache_tarosky_get_multiple() to an existing unmanaged unversioned value. class GetMUnmanagedUnversionedTest extends ObjectCacheTestCase { - public function setUp(): void { - $this->set_next_versioning_keys([]); - parent::setUp(); + public function setUp(): void { + $this->set_next_versioning_keys( array() ); + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; - use TestRedisVersionNotExist; // Not updated - use TestInternalVersionNotExist; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; + use TestRedisVersionNotExist; // Not updated + use TestInternalVersionNotExist; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/get-m/GetMUnmanagedVersionedTest.php b/test/test/tarosky/get-m/GetMUnmanagedVersionedTest.php index 9362284..ff1f325 100644 --- a/test/test/tarosky/get-m/GetMUnmanagedVersionedTest.php +++ b/test/test/tarosky/get-m/GetMUnmanagedVersionedTest.php @@ -2,17 +2,17 @@ // wp_cache_tarosky_get_multiple() to an existing unmanaged versioned value. class GetMUnmanagedVersionedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineGetMVer; - use TestExamGetMVer; - use TestRedisValExists; // Not updated - use TestRedisVersionNotExist; // Not updated - use TestInternalVersionNotExist; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineGetMVer; + use TestExamGetMVer; + use TestRedisValExists; // Not updated + use TestRedisVersionNotExist; // Not updated + use TestInternalVersionNotExist; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetIgnoredTest.php b/test/test/tarosky/get/GetIgnoredTest.php index ef7962d..3edcc2b 100644 --- a/test/test/tarosky/get/GetIgnoredTest.php +++ b/test/test/tarosky/get/GetIgnoredTest.php @@ -2,16 +2,16 @@ // wp_cache_get() an ignored key. class GetIgnoredTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->setup_ignored_key(); - } + $this->setup_ignored_key(); + } - public function testExamFails() { - $found = true; - $res = wp_cache_get(self::IGN_KEY, self::IGN_GROUP, false, $found); - $this->assertFalse($res); - $this->assertFalse($found); - } + public function testExamFails() { + $found = true; + $res = wp_cache_get( self::IGN_KEY, self::IGN_GROUP, false, $found ); + $this->assertFalse( $res ); + $this->assertFalse( $found ); + } } diff --git a/test/test/tarosky/get/GetInexistentRacingTest.php b/test/test/tarosky/get/GetInexistentRacingTest.php index 5e0ef63..c8701de 100644 --- a/test/test/tarosky/get/GetInexistentRacingTest.php +++ b/test/test/tarosky/get/GetInexistentRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_get()-ing an inexistent value. class GetInexistentRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_value(); - } + $this->set_rival_version(); + $this->set_value(); + } - use ExamineCacheGet; - use TestExamGetSucceeds; - use TestRedisValExists; // Set - use TestRedisVersionDefeated; // Set - use TestInternalVersionDefeated; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheGet; + use TestExamGetSucceeds; + use TestRedisValExists; // Set + use TestRedisVersionDefeated; // Set + use TestInternalVersionDefeated; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetInexistentTest.php b/test/test/tarosky/get/GetInexistentTest.php index a57f4e1..a0880c7 100644 --- a/test/test/tarosky/get/GetInexistentTest.php +++ b/test/test/tarosky/get/GetInexistentTest.php @@ -2,11 +2,11 @@ // wp_cache_get() an inexistent value. class GetInexistentTest extends ObjectCacheTestCase { - use ExamineCacheGet; - use TestExamFails; - use TestRedisValNotExist; // Not changed - use TestRedisVersionNotExist; // Not changed - use TestInternalVersionNotExist; - use TestInternalValNotExist; - use ConnectionTests; + use ExamineCacheGet; + use TestExamFails; + use TestRedisValNotExist; // Not changed + use TestRedisVersionNotExist; // Not changed + use TestInternalVersionNotExist; + use TestInternalValNotExist; + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetManagedRacingForceTest.php b/test/test/tarosky/get/GetManagedRacingForceTest.php index 83b64fc..0daf57c 100644 --- a/test/test/tarosky/get/GetManagedRacingForceTest.php +++ b/test/test/tarosky/get/GetManagedRacingForceTest.php @@ -2,23 +2,23 @@ // Race condition occurred during forcibly wp_cache_get()-ing an existing managed value. class GetManagedRacingForceTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_value(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_value(); + } - protected function examined() { - return wp_cache_get(self::KEY, self::GROUP, true); - } + protected function examined() { + return wp_cache_get( self::KEY, self::GROUP, true ); + } - use TestExamGetSucceeds; - use TestRedisValExists; // Not changed - use TestRedisVersionDefeated; // Not changed - use TestInternalVersionDefeated; - use TestInternalValExists; - use ConnectionTests; + use TestExamGetSucceeds; + use TestRedisValExists; // Not changed + use TestRedisVersionDefeated; // Not changed + use TestInternalVersionDefeated; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetManagedRacingTest.php b/test/test/tarosky/get/GetManagedRacingTest.php index f6953f5..b2e33d8 100644 --- a/test/test/tarosky/get/GetManagedRacingTest.php +++ b/test/test/tarosky/get/GetManagedRacingTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_get()-ing an existing managed value. class GetManagedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_value(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheGet; - use TestExamGetSucceeds; // Old value - use TestRedisValSupExists; // Not changed - use TestRedisVersionDefeated; // Not changed - use TestInternalVersionOld; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheGet; + use TestExamGetSucceeds; // Old value + use TestRedisValSupExists; // Not changed + use TestRedisVersionDefeated; // Not changed + use TestInternalVersionOld; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetManagedTest.php b/test/test/tarosky/get/GetManagedTest.php index 334a769..b1cf3c8 100644 --- a/test/test/tarosky/get/GetManagedTest.php +++ b/test/test/tarosky/get/GetManagedTest.php @@ -2,18 +2,18 @@ // wp_cache_get() an existing managed value. class GetManagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL ); + $this->get_and_store_version(); + } - use ExamineCacheGet; - use TestExamGetSucceeds; - use TestRedisValExists; // Not changed - use TestRedisVersionNotChanged; - use TestInternalVersionOld; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheGet; + use TestExamGetSucceeds; + use TestRedisValExists; // Not changed + use TestRedisVersionNotChanged; + use TestInternalVersionOld; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/get/GetUnmanagedTest.php b/test/test/tarosky/get/GetUnmanagedTest.php index f6b6544..4c6ae98 100644 --- a/test/test/tarosky/get/GetUnmanagedTest.php +++ b/test/test/tarosky/get/GetUnmanagedTest.php @@ -2,17 +2,17 @@ // wp_cache_get() an existing unmanaged value. class GetUnmanagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_value(); - } + $this->set_value(); + } - use ExamineCacheGet; - use TestExamGetSucceeds; - use TestRedisValExists; // Not changed - use TestRedisVersionNotExist; // Not changed - use TestInternalVersionNotExist; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineCacheGet; + use TestExamGetSucceeds; + use TestRedisValExists; // Not changed + use TestRedisVersionNotExist; // Not changed + use TestInternalVersionNotExist; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/misc/AlloptionsTest.php b/test/test/tarosky/misc/AlloptionsTest.php index 5e65cf4..d3dfb4e 100644 --- a/test/test/tarosky/misc/AlloptionsTest.php +++ b/test/test/tarosky/misc/AlloptionsTest.php @@ -1,35 +1,35 @@ assertFalse($this->oc1->get(self::KEY, '', true)); - $this->assertTrue($this->oc1->add(self::KEY, self::VAL)); - } + public function testEmptyPrecondition() { + $this->assertFalse( $this->oc1->get( self::KEY, '', true ) ); + $this->assertTrue( $this->oc1->add( self::KEY, self::VAL ) ); + } - public function testEmptyPreconditionPar() { - $this->assertFalse($this->oc1->get(self::KEY, '', true)); - $this->assertFalse($this->oc2->get(self::KEY, '', true)); - $this->assertTrue($this->oc1->add(self::KEY, self::VAL)); - $this->assertFalse($this->oc2->add(self::KEY, self::VAL)); - $this->assertEquals(self::VAL, $this->oc2->get(self::KEY, '', true)); - } + public function testEmptyPreconditionPar() { + $this->assertFalse( $this->oc1->get( self::KEY, '', true ) ); + $this->assertFalse( $this->oc2->get( self::KEY, '', true ) ); + $this->assertTrue( $this->oc1->add( self::KEY, self::VAL ) ); + $this->assertFalse( $this->oc2->add( self::KEY, self::VAL ) ); + $this->assertEquals( self::VAL, $this->oc2->get( self::KEY, '', true ) ); + } - public function testEmptyValuePrecondition() { - self::$redis->set( - $this->oc1->version_key(self::KEY), - $this->oc1->generate_version() - ); + public function testEmptyValuePrecondition() { + self::$redis->set( + $this->oc1->version_key( self::KEY ), + $this->oc1->generate_version() + ); - $this->assertFalse($this->oc1->get(self::KEY, '', true)); - $this->assertTrue($this->oc1->add(self::KEY, self::VAL)); - } + $this->assertFalse( $this->oc1->get( self::KEY, '', true ) ); + $this->assertTrue( $this->oc1->add( self::KEY, self::VAL ) ); + } - public function testEmptyVersionPrecondition() { - self::$redis->set( - $this->oc1->key(self::KEY), - WP_Object_Cache::encode_redis_string(self::VAL_SUP) - ); + public function testEmptyVersionPrecondition() { + self::$redis->set( + $this->oc1->key( self::KEY ), + WP_Object_Cache::encode_redis_string( self::VAL_SUP ) + ); - $this->assertEquals(self::VAL_SUP, $this->oc1->get(self::KEY, '', true)); - } + $this->assertEquals( self::VAL_SUP, $this->oc1->get( self::KEY, '', true ) ); + } } diff --git a/test/test/tarosky/misc/ExceptionTest.php b/test/test/tarosky/misc/ExceptionTest.php index 9c0497e..eb295fc 100644 --- a/test/test/tarosky/misc/ExceptionTest.php +++ b/test/test/tarosky/misc/ExceptionTest.php @@ -3,101 +3,101 @@ use PHPUnit\Framework\TestCase; class ExceptionTest extends TestCase { - protected static $redis; - - public static function setUpBeforeClass(): void { - parent::setUpBeforeClass(); - - global $redis_server; - $redis_server = [ - 'host' => 'redis', - 'port' => 6379, - 'timeout' => 1000, - 'retry_interval' => 100, - ]; - - self::$redis = new Redis(); - self::$redis->connect('redis'); - } - - public function setUp(): void { - global $wp_object_cache; - - self::$redis->flushdb(); - self::$redis->set('foo', 'bar'); - $wp_object_cache = new ThrowOnGetObjectCache(); - } - - public function testException() { - global $wp_object_cache; - - try { - $wp_object_cache->call_redis('get', ['foo']); - $this->fail(); - } catch (WP_Object_Cache_RedisCallException $e) { - $this->assertEquals('get', $e->getMethod()); - $this->assertEquals(['foo'], $e->getArgs()); - $this->assertStringContainsString('get', $e->getMessage()); - $this->assertStringContainsString('foo', $e->getMessage()); - $this->assertStringContainsString( - 'default error message', - $e->getMessage() - ); - $this->assertInstanceOf(RedisTestException::class, $e->getPrevious()); - $this->assertEquals(0, $wp_object_cache->trigger_error_count); - } - } - - public function testRetry1() { - global $wp_object_cache; - - $wp_object_cache->error_message = 'Connection closed'; - $wp_object_cache->error_ifs = [true, false]; - - $this->assertEquals('bar', $wp_object_cache->call_redis('get', ['foo'])); - $this->assertEquals(1, $wp_object_cache->trigger_error_count); - } - - public function testRetry2() { - global $wp_object_cache; - - $wp_object_cache->error_message = 'Connection closed'; - $wp_object_cache->error_ifs = [true, true, false]; - - $this->assertEquals('bar', $wp_object_cache->call_redis('get', ['foo'])); - $this->assertEquals(2, $wp_object_cache->trigger_error_count); - } - - public function testRetry3() { - global $wp_object_cache; - - $wp_object_cache->error_message = 'Connection closed'; - $wp_object_cache->error_ifs = [true, true, true, false]; - - $this->assertEquals('bar', $wp_object_cache->call_redis('get', ['foo'])); - $this->assertEquals(3, $wp_object_cache->trigger_error_count); - } - - public function testRetry4() { - global $wp_object_cache; - - $wp_object_cache->error_message = 'Connection closed'; - $wp_object_cache->error_ifs = [true, true, true, true, false]; - - try { - $wp_object_cache->call_redis('get', ['foo']); - $this->fail(); - } catch (WP_Object_Cache_RedisCallException $e) { - $this->assertEquals('get', $e->getMethod()); - $this->assertEquals(['foo'], $e->getArgs()); - $this->assertStringContainsString('get', $e->getMessage()); - $this->assertStringContainsString('foo', $e->getMessage()); - $this->assertStringContainsString( - 'Connection closed', - $e->getMessage() - ); - $this->assertInstanceOf(RedisTestException::class, $e->getPrevious()); - $this->assertEquals(3, $wp_object_cache->trigger_error_count); - } - } + protected static $redis; + + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + + global $redis_server; + $redis_server = array( + 'host' => 'redis', + 'port' => 6379, + 'timeout' => 1000, + 'retry_interval' => 100, + ); + + self::$redis = new Redis(); + self::$redis->connect( 'redis' ); + } + + public function setUp(): void { + global $wp_object_cache; + + self::$redis->flushdb(); + self::$redis->set( 'foo', 'bar' ); + $wp_object_cache = new ThrowOnGetObjectCache(); + } + + public function testException() { + global $wp_object_cache; + + try { + $wp_object_cache->call_redis( 'get', array( 'foo' ) ); + $this->fail(); + } catch ( WP_Object_Cache_RedisCallException $e ) { + $this->assertEquals( 'get', $e->getMethod() ); + $this->assertEquals( array( 'foo' ), $e->getArgs() ); + $this->assertStringContainsString( 'get', $e->getMessage() ); + $this->assertStringContainsString( 'foo', $e->getMessage() ); + $this->assertStringContainsString( + 'default error message', + $e->getMessage() + ); + $this->assertInstanceOf( RedisTestException::class, $e->getPrevious() ); + $this->assertEquals( 0, $wp_object_cache->trigger_error_count ); + } + } + + public function testRetry1() { + global $wp_object_cache; + + $wp_object_cache->error_message = 'Connection closed'; + $wp_object_cache->error_ifs = array( true, false ); + + $this->assertEquals( 'bar', $wp_object_cache->call_redis( 'get', array( 'foo' ) ) ); + $this->assertEquals( 1, $wp_object_cache->trigger_error_count ); + } + + public function testRetry2() { + global $wp_object_cache; + + $wp_object_cache->error_message = 'Connection closed'; + $wp_object_cache->error_ifs = array( true, true, false ); + + $this->assertEquals( 'bar', $wp_object_cache->call_redis( 'get', array( 'foo' ) ) ); + $this->assertEquals( 2, $wp_object_cache->trigger_error_count ); + } + + public function testRetry3() { + global $wp_object_cache; + + $wp_object_cache->error_message = 'Connection closed'; + $wp_object_cache->error_ifs = array( true, true, true, false ); + + $this->assertEquals( 'bar', $wp_object_cache->call_redis( 'get', array( 'foo' ) ) ); + $this->assertEquals( 3, $wp_object_cache->trigger_error_count ); + } + + public function testRetry4() { + global $wp_object_cache; + + $wp_object_cache->error_message = 'Connection closed'; + $wp_object_cache->error_ifs = array( true, true, true, true, false ); + + try { + $wp_object_cache->call_redis( 'get', array( 'foo' ) ); + $this->fail(); + } catch ( WP_Object_Cache_RedisCallException $e ) { + $this->assertEquals( 'get', $e->getMethod() ); + $this->assertEquals( array( 'foo' ), $e->getArgs() ); + $this->assertStringContainsString( 'get', $e->getMessage() ); + $this->assertStringContainsString( 'foo', $e->getMessage() ); + $this->assertStringContainsString( + 'Connection closed', + $e->getMessage() + ); + $this->assertInstanceOf( RedisTestException::class, $e->getPrevious() ); + $this->assertEquals( 3, $wp_object_cache->trigger_error_count ); + } + } } diff --git a/test/test/tarosky/misc/ExhaustiveTest.php b/test/test/tarosky/misc/ExhaustiveTest.php index b332d91..567e210 100644 --- a/test/test/tarosky/misc/ExhaustiveTest.php +++ b/test/test/tarosky/misc/ExhaustiveTest.php @@ -4,76 +4,76 @@ use Eris\TestTrait; class ExhaustiveTest extends ExhaustiveOCTestCase { - use TestTrait; + use TestTrait; - private static function createOCGenerator() { - return Generator\bind( - Generator\elements([0, 2, 4, 6]), - function ($size) { - return Generator\vector($size, Generator\nat()); - } - ); - } + private static function createOCGenerator() { + return Generator\bind( + Generator\elements( array( 0, 2, 4, 6 ) ), + function ( $size ) { + return Generator\vector( $size, Generator\nat() ); + } + ); + } - private static function ordering($indexes) { - $values = []; - for ($i = 0; $i < count($indexes); $i++) { - for ($j = 0; $j < count($indexes[$i]); $j++) { - $values[] = [ - $i, - $indexes[$i][$j], - ]; - } - } - usort($values, function ($a, $b) { - return $a[1] - $b[1]; - }); - $res = []; - foreach ($values as $v) { - $res[] = $v[0]; - } + private static function ordering( $indexes ) { + $values = array(); + for ( $i = 0; $i < count( $indexes ); $i++ ) { + for ( $j = 0; $j < count( $indexes[ $i ] ); $j++ ) { + $values[] = array( + $i, + $indexes[ $i ][ $j ], + ); + } + } + usort($values, function ( $a, $b ) { + return $a[1] - $b[1]; + }); + $res = array(); + foreach ( $values as $v ) { + $res[] = $v[0]; + } - return $res; - } + return $res; + } - public function testEmptyPrecondition() { - $this->forAll( - Generator\tuple( - self::createOCGenerator(), - self::createOCGenerator(), - self::createOCGenerator(), - ) - )->disableShrinking()->then(function ($indexes) { - $order_list = self::ordering($indexes); - $ocstates = array_fill(0, 3, true); + public function testEmptyPrecondition() { + $this->forAll( + Generator\tuple( + self::createOCGenerator(), + self::createOCGenerator(), + self::createOCGenerator(), + ) + )->disableShrinking()->then(function ( $indexes ) { + $order_list = self::ordering( $indexes ); + $ocstates = array_fill( 0, 3, true ); - if (count($order_list) == 0) { - return; - } + if ( 0 === count( $order_list ) ) { + return; + } - self::$redis->flushdb(); - $this->ocs = [ - new WP_Object_Cache(), - new WP_Object_Cache(), - new WP_Object_Cache(), - ]; - foreach ($order_list as $i) { - if ($ocstates[$i]) { - $this->ocs[$i]->get(self::KEY, '', true); - } else { - $this->ocs[$i]->add(self::KEY, self::VAL); - } - $ocstates[$i] = !$ocstates[$i]; - } + self::$redis->flushdb(); + $this->ocs = array( + new WP_Object_Cache(), + new WP_Object_Cache(), + new WP_Object_Cache(), + ); + foreach ( $order_list as $i ) { + if ( $ocstates[ $i ] ) { + $this->ocs[ $i ]->get( self::KEY, '', true ); + } else { + $this->ocs[ $i ]->add( self::KEY, self::VAL ); + } + $ocstates[ $i ] = ! $ocstates[ $i ]; + } - try { - foreach (range(0, 2) as $i) { - $this->assertEquals(self::VAL, $this->ocs[$i]->get(self::KEY, '', true)); - } - } catch (Exception $e) { - error_log('failed order: ' . var_export($order_list, true)); - throw $e; - } - }); - } + try { + foreach ( range( 0, 2 ) as $i ) { + $this->assertEquals( self::VAL, $this->ocs[ $i ]->get( self::KEY, '', true ) ); + } + } catch ( Exception $e ) { + error_log( 'failed order: ' . var_export( $order_list, true ) ); + throw $e; + } + }); + } } diff --git a/test/test/tarosky/misc/KeyTest.php b/test/test/tarosky/misc/KeyTest.php index 6c52abe..912e92e 100644 --- a/test/test/tarosky/misc/KeyTest.php +++ b/test/test/tarosky/misc/KeyTest.php @@ -1,35 +1,35 @@ key('abc'); - $key2 = $wp_object_cache->key('abc'); - $this->assertEquals($key1, $key2); - } + $key1 = $wp_object_cache->key( 'abc' ); + $key2 = $wp_object_cache->key( 'abc' ); + $this->assertEquals( $key1, $key2 ); + } - public function testDifferentTypeKey() { - global $wp_object_cache; + public function testDifferentTypeKey() { + global $wp_object_cache; - $key1 = $wp_object_cache->key('1'); - $key2 = $wp_object_cache->key(1); - $this->assertEquals($key1, $key2); - } + $key1 = $wp_object_cache->key( '1' ); + $key2 = $wp_object_cache->key( 1 ); + $this->assertEquals( $key1, $key2 ); + } - public function testSameVersionKey() { - global $wp_object_cache; + public function testSameVersionKey() { + global $wp_object_cache; - $key1 = $wp_object_cache->version_key('abc'); - $key2 = $wp_object_cache->version_key('abc'); - $this->assertEquals($key1, $key2); - } + $key1 = $wp_object_cache->version_key( 'abc' ); + $key2 = $wp_object_cache->version_key( 'abc' ); + $this->assertEquals( $key1, $key2 ); + } - public function testDifferentTypeVersionKey() { - global $wp_object_cache; + public function testDifferentTypeVersionKey() { + global $wp_object_cache; - $key1 = $wp_object_cache->version_key('1'); - $key2 = $wp_object_cache->version_key(1); - $this->assertEquals($key1, $key2); - } + $key1 = $wp_object_cache->version_key( '1' ); + $key2 = $wp_object_cache->version_key( 1 ); + $this->assertEquals( $key1, $key2 ); + } } diff --git a/test/test/tarosky/misc/ScriptTest.php b/test/test/tarosky/misc/ScriptTest.php index 285f942..e7f9af1 100644 --- a/test/test/tarosky/misc/ScriptTest.php +++ b/test/test/tarosky/misc/ScriptTest.php @@ -1,35 +1,35 @@ flushdb(); - self::$redis->script('flush'); - } + self::$redis->flushdb(); + self::$redis->script( 'flush' ); + } - public function testScriptsLoaded() { - global $wp_object_cache; + public function testScriptsLoaded() { + global $wp_object_cache; - $hashes = array_map(function ($path) { - return sha1(file_get_contents($path)); - }, glob(TAROSKY_WP_REDIS_PATCH_LUA_DIR . '/*.lua')); + $hashes = array_map(function ( $path ) { + return sha1( file_get_contents( $path ) ); + }, glob( TAROSKY_WP_REDIS_PATCH_LUA_DIR . '/*.lua' )); - foreach ($hashes as $hash) { - $this->assertFalse(!!self::$redis->script('exists', $hash)[0]); - } + foreach ( $hashes as $hash ) { + $this->assertFalse( ! ! self::$redis->script( 'exists', $hash )[0] ); + } - $wp_object_cache->ensureLua(self::$redis); + $wp_object_cache->ensureLua( self::$redis ); - foreach ($hashes as $hash) { - $this->assertTrue(!!self::$redis->script('exists', $hash)[0]); - } + foreach ( $hashes as $hash ) { + $this->assertTrue( ! ! self::$redis->script( 'exists', $hash )[0] ); + } - // Nothing happens when running it twice. - $wp_object_cache->ensureLua(self::$redis); + // Nothing happens when running it twice. + $wp_object_cache->ensureLua( self::$redis ); - foreach ($hashes as $hash) { - $this->assertTrue(!!self::$redis->script('exists', $hash)[0]); - } - } + foreach ( $hashes as $hash ) { + $this->assertTrue( ! ! self::$redis->script( 'exists', $hash )[0] ); + } + } } diff --git a/test/test/tarosky/replace-ex/ReplaceExInexistentRacingTest.php b/test/test/tarosky/replace-ex/ReplaceExInexistentRacingTest.php index 70e4ce5..d0f483f 100644 --- a/test/test/tarosky/replace-ex/ReplaceExInexistentRacingTest.php +++ b/test/test/tarosky/replace-ex/ReplaceExInexistentRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_replace() with expiration as an inexistent key. class ReplaceExInexistentRacingTest extends ReplaceInexistentRacingTest { - use ExamineCacheReplaceEx; + use ExamineCacheReplaceEx; } diff --git a/test/test/tarosky/replace-ex/ReplaceExInexistentTest.php b/test/test/tarosky/replace-ex/ReplaceExInexistentTest.php index a049227..e695a08 100644 --- a/test/test/tarosky/replace-ex/ReplaceExInexistentTest.php +++ b/test/test/tarosky/replace-ex/ReplaceExInexistentTest.php @@ -4,5 +4,5 @@ // wp_cache_replace() with expiration as an inexistent key. class ReplaceExInexistentTest extends ReplaceInexistentTest { - use ExamineCacheReplaceEx; + use ExamineCacheReplaceEx; } diff --git a/test/test/tarosky/replace-ex/ReplaceExManagedRacingTest.php b/test/test/tarosky/replace-ex/ReplaceExManagedRacingTest.php index 9ae076b..30effac 100644 --- a/test/test/tarosky/replace-ex/ReplaceExManagedRacingTest.php +++ b/test/test/tarosky/replace-ex/ReplaceExManagedRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_replace() with expiration to an existing managed key. class ReplaceExManagedRacingTest extends ReplaceManagedRacingTest { - use ExamineCacheReplaceEx; + use ExamineCacheReplaceEx; } diff --git a/test/test/tarosky/replace-ex/ReplaceExManagedTest.php b/test/test/tarosky/replace-ex/ReplaceExManagedTest.php index 211ae42..7f2405f 100644 --- a/test/test/tarosky/replace-ex/ReplaceExManagedTest.php +++ b/test/test/tarosky/replace-ex/ReplaceExManagedTest.php @@ -4,5 +4,5 @@ // wp_cache_replace() with expiration to an existing managed key. class ReplaceExManagedTest extends ReplaceManagedTest { - use ExamineCacheReplaceEx; + use ExamineCacheReplaceEx; } diff --git a/test/test/tarosky/replace-ex/ReplaceExUnmanagedTest.php b/test/test/tarosky/replace-ex/ReplaceExUnmanagedTest.php index 08aa0ea..523b4c8 100644 --- a/test/test/tarosky/replace-ex/ReplaceExUnmanagedTest.php +++ b/test/test/tarosky/replace-ex/ReplaceExUnmanagedTest.php @@ -4,5 +4,5 @@ // wp_cache_replace() with expiration to an existing unmanaged key. class ReplaceExUnmanagedTest extends ReplaceUnmanagedTest { - use ExamineCacheReplaceEx; + use ExamineCacheReplaceEx; } diff --git a/test/test/tarosky/replace/ReplaceIgnoredTest.php b/test/test/tarosky/replace/ReplaceIgnoredTest.php index 13b5a06..052bc2d 100644 --- a/test/test/tarosky/replace/ReplaceIgnoredTest.php +++ b/test/test/tarosky/replace/ReplaceIgnoredTest.php @@ -2,16 +2,16 @@ // wp_cache_replace() an ignored key. class ReplaceIgnoredTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->setup_ignored_key(); - } + $this->setup_ignored_key(); + } - protected function examined() { - return wp_cache_replace(self::IGN_KEY, self::VAL_SUP); - } + protected function examined() { + return wp_cache_replace( self::IGN_KEY, self::VAL_SUP ); + } - use TestExamFails; - use TestRedisIgnoredValExists; + use TestExamFails; + use TestRedisIgnoredValExists; } diff --git a/test/test/tarosky/replace/ReplaceInexistentRacingTest.php b/test/test/tarosky/replace/ReplaceInexistentRacingTest.php index 8b1833a..57b3b30 100644 --- a/test/test/tarosky/replace/ReplaceInexistentRacingTest.php +++ b/test/test/tarosky/replace/ReplaceInexistentRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_replace() as an inexistent key. class ReplaceInexistentRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_sup_value(); - } + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheReplace; - use TestExamSucceeds; - use TestRedisValExists; - use TestRedisVersionRenewed; - use TestInternalVersionLatest; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheReplace; + use TestExamSucceeds; + use TestRedisValExists; + use TestRedisVersionRenewed; + use TestInternalVersionLatest; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/replace/ReplaceInexistentTest.php b/test/test/tarosky/replace/ReplaceInexistentTest.php index a2dc6ba..3b6e0b4 100644 --- a/test/test/tarosky/replace/ReplaceInexistentTest.php +++ b/test/test/tarosky/replace/ReplaceInexistentTest.php @@ -2,11 +2,11 @@ // wp_cache_replace() as an inexistent key. class ReplaceInexistentTest extends ObjectCacheTestCase { - use ExamineCacheReplace; - use TestExamFails; - use TestRedisValNotExist; // Data won't be changed because of the original specification. - use TestRedisVersionNotExist; // Not updated - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Not changed - use ConnectionTests; + use ExamineCacheReplace; + use TestExamFails; + use TestRedisValNotExist; // Data won't be changed because of the original specification. + use TestRedisVersionNotExist; // Not updated + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Not changed + use ConnectionTests; } diff --git a/test/test/tarosky/replace/ReplaceManagedRacingTest.php b/test/test/tarosky/replace/ReplaceManagedRacingTest.php index 9d139e6..4946613 100644 --- a/test/test/tarosky/replace/ReplaceManagedRacingTest.php +++ b/test/test/tarosky/replace/ReplaceManagedRacingTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_replace() to an existing managed key. class ReplaceManagedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_2_value(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_2_value(); + } - use ExamineCacheReplace; - use TestExamSucceeds; - use TestRedisValExists; - use TestRedisVersionRenewed; - use TestInternalVersionLatest; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheReplace; + use TestExamSucceeds; + use TestRedisValExists; + use TestRedisVersionRenewed; + use TestInternalVersionLatest; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/replace/ReplaceManagedTest.php b/test/test/tarosky/replace/ReplaceManagedTest.php index f0cbcc3..2b9e7ae 100644 --- a/test/test/tarosky/replace/ReplaceManagedTest.php +++ b/test/test/tarosky/replace/ReplaceManagedTest.php @@ -2,18 +2,18 @@ // wp_cache_replace() to an existing managed key. class ReplaceManagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + } - use ExamineCacheReplace; - use TestExamSucceeds; - use TestRedisValExists; // Replaced - use TestRedisVersionRenewed; // Updated - use TestInternalVersionLatest; - use TestInternalValExists; - use ConnectionTests; + use ExamineCacheReplace; + use TestExamSucceeds; + use TestRedisValExists; // Replaced + use TestRedisVersionRenewed; // Updated + use TestInternalVersionLatest; + use TestInternalValExists; + use ConnectionTests; } diff --git a/test/test/tarosky/replace/ReplaceUnmanagedTest.php b/test/test/tarosky/replace/ReplaceUnmanagedTest.php index 01d0ee8..a7db6c9 100644 --- a/test/test/tarosky/replace/ReplaceUnmanagedTest.php +++ b/test/test/tarosky/replace/ReplaceUnmanagedTest.php @@ -2,17 +2,17 @@ // wp_cache_replace() to an existing unmanaged key. class ReplaceUnmanagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_sup_value(); - } + $this->set_sup_value(); + } - use ExamineCacheReplace; - use TestExamSucceeds; - use TestRedisValExists; // Updated - use TestRedisVersionRenewed; // Set - use TestInternalVersionLatest; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineCacheReplace; + use TestExamSucceeds; + use TestRedisValExists; // Updated + use TestRedisVersionRenewed; // Set + use TestInternalVersionLatest; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/set-ex/SetExInexistentRacingTest.php b/test/test/tarosky/set-ex/SetExInexistentRacingTest.php index 3fff1b3..992bb5f 100644 --- a/test/test/tarosky/set-ex/SetExInexistentRacingTest.php +++ b/test/test/tarosky/set-ex/SetExInexistentRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_set() with expiration to an inexistent key. class SetExInexistentRacingTest extends SetInexistentRacingTest { - use ExamineCacheSetEx; + use ExamineCacheSetEx; } diff --git a/test/test/tarosky/set-ex/SetExInexistentTest.php b/test/test/tarosky/set-ex/SetExInexistentTest.php index dd37787..0950eec 100644 --- a/test/test/tarosky/set-ex/SetExInexistentTest.php +++ b/test/test/tarosky/set-ex/SetExInexistentTest.php @@ -4,5 +4,5 @@ // wp_cache_set() with expiration to an inexistent key. class SetExInexistentTest extends SetInexistentTest { - use ExamineCacheSetEx; + use ExamineCacheSetEx; } diff --git a/test/test/tarosky/set-ex/SetExManagedRacingTest.php b/test/test/tarosky/set-ex/SetExManagedRacingTest.php index a6575a6..fc0c2da 100644 --- a/test/test/tarosky/set-ex/SetExManagedRacingTest.php +++ b/test/test/tarosky/set-ex/SetExManagedRacingTest.php @@ -4,5 +4,5 @@ // Race condition occurred during wp_cache_set() with expiration to an existing managed key. class SetExManagedRacingTest extends SetManagedRacingTest { - use ExamineCacheSetEx; + use ExamineCacheSetEx; } diff --git a/test/test/tarosky/set-ex/SetExManagedTest.php b/test/test/tarosky/set-ex/SetExManagedTest.php index cc0a798..1734065 100644 --- a/test/test/tarosky/set-ex/SetExManagedTest.php +++ b/test/test/tarosky/set-ex/SetExManagedTest.php @@ -4,5 +4,5 @@ // wp_cache_set() with expiration to an existing managed key. class SetExManagedTest extends SetManagedTest { - use ExamineCacheSetEx; + use ExamineCacheSetEx; } diff --git a/test/test/tarosky/set-ex/SetExUnmanagedTest.php b/test/test/tarosky/set-ex/SetExUnmanagedTest.php index 011734b..ca398f0 100644 --- a/test/test/tarosky/set-ex/SetExUnmanagedTest.php +++ b/test/test/tarosky/set-ex/SetExUnmanagedTest.php @@ -4,5 +4,5 @@ // wp_cache_set() with expiration to an existing unmanaged key. class SetExUnmanagedTest extends SetUnmanagedTest { - use ExamineCacheSetEx; + use ExamineCacheSetEx; } diff --git a/test/test/tarosky/set/SetIgnoredTest.php b/test/test/tarosky/set/SetIgnoredTest.php index 9cb7e29..dcf127e 100644 --- a/test/test/tarosky/set/SetIgnoredTest.php +++ b/test/test/tarosky/set/SetIgnoredTest.php @@ -2,10 +2,10 @@ // wp_cache_set() an ignored key. class SetIgnoredTest extends ObjectCacheTestCase { - protected function examined() { - return wp_cache_set(self::IGN_KEY, self::VAL, self::IGN_GROUP); - } + protected function examined() { + return wp_cache_set( self::IGN_KEY, self::VAL, self::IGN_GROUP ); + } - use TestExamSucceeds; - use TestRedisIgnoredValNotExist; + use TestExamSucceeds; + use TestRedisIgnoredValNotExist; } diff --git a/test/test/tarosky/set/SetInexistentRacingTest.php b/test/test/tarosky/set/SetInexistentRacingTest.php index 2e2be47..8da254d 100644 --- a/test/test/tarosky/set/SetInexistentRacingTest.php +++ b/test/test/tarosky/set/SetInexistentRacingTest.php @@ -2,18 +2,18 @@ // Race condition occurred during wp_cache_set() to an inexistent key. class SetInexistentRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_rival_version(); - $this->set_sup_value(); - } + $this->set_rival_version(); + $this->set_sup_value(); + } - use ExamineCacheSet; - use TestExamFails; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Removed - use ConnectionTests; + use ExamineCacheSet; + use TestExamFails; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Removed + use ConnectionTests; } diff --git a/test/test/tarosky/set/SetInexistentTest.php b/test/test/tarosky/set/SetInexistentTest.php index dfb5116..2866ce6 100644 --- a/test/test/tarosky/set/SetInexistentTest.php +++ b/test/test/tarosky/set/SetInexistentTest.php @@ -2,11 +2,11 @@ // wp_cache_set() to an inexistent key. class SetInexistentTest extends ObjectCacheTestCase { - use ExamineCacheSet; - use TestExamSucceeds; - use TestRedisValExists; // Set - use TestRedisVersionRenewed; // Set - use TestInternalVersionLatest; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineCacheSet; + use TestExamSucceeds; + use TestRedisValExists; // Set + use TestRedisVersionRenewed; // Set + use TestInternalVersionLatest; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/tarosky/set/SetManagedRacingTest.php b/test/test/tarosky/set/SetManagedRacingTest.php index 352668f..02ebe8c 100644 --- a/test/test/tarosky/set/SetManagedRacingTest.php +++ b/test/test/tarosky/set/SetManagedRacingTest.php @@ -2,20 +2,20 @@ // Race condition occurred during wp_cache_set() to an existing managed key. class SetManagedRacingTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - $this->set_rival_version(); - $this->set_sup_2_value(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + $this->set_rival_version(); + $this->set_sup_2_value(); + } - use ExamineCacheSet; - use TestExamFails; - use TestRedisValNotExist; - use TestRedisVersionNotExist; - use TestInternalVersionNotExist; - use TestInternalValNotExist; // Removed - use ConnectionTests; + use ExamineCacheSet; + use TestExamFails; + use TestRedisValNotExist; + use TestRedisVersionNotExist; + use TestInternalVersionNotExist; + use TestInternalValNotExist; // Removed + use ConnectionTests; } diff --git a/test/test/tarosky/set/SetManagedTest.php b/test/test/tarosky/set/SetManagedTest.php index a414732..a8f5749 100644 --- a/test/test/tarosky/set/SetManagedTest.php +++ b/test/test/tarosky/set/SetManagedTest.php @@ -2,18 +2,18 @@ // wp_cache_set() to an existing managed key. class SetManagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - wp_cache_set(self::KEY, self::VAL_SUP); - $this->get_and_store_version(); - } + wp_cache_set( self::KEY, self::VAL_SUP ); + $this->get_and_store_version(); + } - use ExamineCacheSet; - use TestExamSucceeds; - use TestRedisValExists; // Updated - use TestRedisVersionRenewed; // Updated - use TestInternalVersionLatest; - use TestInternalValExists; // Updated - use ConnectionTests; + use ExamineCacheSet; + use TestExamSucceeds; + use TestRedisValExists; // Updated + use TestRedisVersionRenewed; // Updated + use TestInternalVersionLatest; + use TestInternalValExists; // Updated + use ConnectionTests; } diff --git a/test/test/tarosky/set/SetUnmanagedTest.php b/test/test/tarosky/set/SetUnmanagedTest.php index 569ef72..75c8055 100644 --- a/test/test/tarosky/set/SetUnmanagedTest.php +++ b/test/test/tarosky/set/SetUnmanagedTest.php @@ -2,17 +2,17 @@ // wp_cache_set() to an existing unmanaged key. class SetUnmanagedTest extends ObjectCacheTestCase { - public function setUp(): void { - parent::setUp(); + public function setUp(): void { + parent::setUp(); - $this->set_sup_value(); - } + $this->set_sup_value(); + } - use ExamineCacheSet; - use TestExamSucceeds; - use TestRedisValExists; // Updated - use TestRedisVersionRenewed; // Updated - use TestInternalVersionLatest; - use TestInternalValExists; // Set - use ConnectionTests; + use ExamineCacheSet; + use TestExamSucceeds; + use TestRedisValExists; // Updated + use TestRedisVersionRenewed; // Updated + use TestInternalVersionLatest; + use TestInternalValExists; // Set + use ConnectionTests; } diff --git a/test/test/wp-redis/CacheTest.php b/test/test/wp-redis/CacheTest.php index e8f37ff..648ea4b 100644 --- a/test/test/wp-redis/CacheTest.php +++ b/test/test/wp-redis/CacheTest.php @@ -6,699 +6,699 @@ */ class CacheTest extends WPRedisTestCase { - private $cache; - - private function reset_cache_state() { - $this->cache->redis_calls = []; - $this->cache->cache = []; - } - - public function setUp(): void { - global $wp_object_cache, $redis_server; - - parent::setUp(); - - $redis_server = [ - 'host' => 'redis', - 'port' => 6379, - 'timeout' => 1000, - 'retry_interval' => 100, - ]; - - $wp_object_cache = new WP_Object_Cache; - $wp_object_cache->flush(); - - // create two cache objects with a shared cache dir - // this simulates a typical cache situation, two separate requests interacting - $this->cache = &$this->init_cache(); - $this->cache->redis_calls = []; - } - - public function tearDown(): void { - parent::tearDown(); - $this->flush_cache(); - } - - public function &init_cache() { - $cache = new WP_Object_Cache(); - $cache->add_global_groups([ - 'blog-details', - 'blog-id-cache', - 'blog-lookup', - 'global-cache-test', - 'global-posts', - 'rss', - 'site-lookup', - 'site-options', - 'site-transient', - 'user_meta', - 'userlogins', - 'usermeta', - 'users', - ]); - return $cache; - } - - public function test_redis_connected() { - $this->assertTrue(isset($this->cache->redis)); - $this->assertTrue($this->cache->redis->IsConnected()); - } - - public function test_redis_reload_connection_closed() { - // Connection is live - $this->cache->set('foo', 'bar'); - $this->assertTrue($this->cache->redis->IsConnected()); - $this->assertTrue($this->cache->is_redis_connected); - $this->assertEquals('bar', $this->cache->get('foo', 'default', true)); - // Connection is closed - $this->cache->redis->close(); - $this->assertFalse($this->cache->redis->IsConnected()); - // Reload occurs with set() - $this->cache->set('foo', 'banana'); - $this->assertEquals('banana', $this->cache->get('foo')); - $this->assertTrue($this->cache->is_redis_connected); - $this->assertTrue($this->cache->redis->IsConnected()); - } - - public function test_redis_bad_authentication() { - global $redis_server; - - $redis_server['host'] = '127.0.0.1'; - $redis_server['port'] = 9999; - $redis_server['auth'] = 'foobar'; - - $this->expectWarning(); - - $cache = new WP_Object_Cache; - $this->assertTrue($cache->is_retriable_error_message( - str_replace('WP Redis: ', '', $cache->last_triggered_error) - )); - $this->assertFalse($cache->is_redis_connected); - // Fails back to the internal object cache - $cache->set('foo', 'bar'); - $this->assertEquals('bar', $cache->get('foo')); - } - - public function test_miss() { - $this->assertFalse($this->cache->get(rand_str())); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_set() { - $key = rand_str(); - $val1 = rand_str(); - $val2 = rand_str(); - - // memcached accepts set() if the key does not exist - $this->assertTrue($this->cache->set($key, $val1)); - $this->assertEquals($val1, $this->cache->get($key)); - // Second set() with same key should be allowed - $this->assertTrue($this->cache->set($key, $val2)); - $this->assertEquals($val2, $this->cache->get($key)); - $this->assertEquals(['set' => 2], $this->cache->redis_calls); - } - - // Make sure objects are cloned going to and from the cache - public function test_object_refs() { - $key = rand_str(); - $object_a = new stdClass; - $object_a->foo = 'alpha'; - $this->cache->set($key, $object_a); - $object_a->foo = 'bravo'; - $object_b = $this->cache->get($key); - $this->assertEquals('alpha', $object_b->foo); - $object_b->foo = 'charlie'; - $this->assertEquals('bravo', $object_a->foo); - - $key = rand_str(); - $object_a = new stdClass; - $object_a->foo = 'alpha'; - $this->cache->add($key, $object_a); - $object_a->foo = 'bravo'; - $object_b = $this->cache->get($key); - $this->assertEquals('alpha', $object_b->foo); - $object_b->foo = 'charlie'; - $this->assertEquals('bravo', $object_a->foo); - } - - public function test_get_already_exists_internal() { - $key = rand_str(); - $this->cache->set($key, 'alpha'); - $this->assertEquals(['set' => 1], $this->cache->redis_calls); - $this->cache->redis_calls = []; // reset to limit scope of test - $this->assertEquals('alpha', $this->cache->get($key)); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_get_missing_persistent() { - $key = rand_str(); - $this->cache->get($key); - $this->cache->get($key); - $this->assertEquals(['get' => 2], $this->cache->redis_calls); - } - - public function test_get_non_persistent_group() { - $key = rand_str(); - $group = 'nonpersistent'; - $this->cache->add_non_persistent_groups($group); - $this->cache->get($key, $group); - $this->assertEmpty($this->cache->redis_calls); - $this->cache->get($key, $group); - $this->assertEmpty($this->cache->redis_calls); - $this->cache->set($key, 'alpha', $group); - $this->cache->get($key, $group); - $this->assertEmpty($this->cache->redis_calls); - $this->cache->get($key, $group); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_get_false_value_persistent_cache() { - $key = rand_str(); - $this->cache->set($key, false); - $this->reset_cache_state(); - $found = null; - $this->assertFalse($this->cache->get($key, 'default', false, $found)); - $this->assertTrue($found); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_get_true_value_persistent_cache() { - $key = rand_str(); - $this->cache->set($key, true); - $this->reset_cache_state(); - $found = null; - $this->assertTrue($this->cache->get($key, 'default', false, $found)); - $this->assertTrue($found); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_get_null_value_persistent_cache() { - $key = rand_str(); - $this->cache->set($key, null); - $this->reset_cache_state(); - $found = null; - $this->assertNull($this->cache->get($key, 'default', false, $found)); - $this->assertTrue($found); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_get_int_values_persistent_cache() { - $key1 = rand_str(); - $key2 = rand_str(); - $this->cache->set($key1, 123); - $this->cache->set($key2, 0xf4c3b00c); - $this->reset_cache_state(); - // Should be upgraded to more strict comparison if change proposed in issue #181 is merged. - $this->assertSame(123, $this->cache->get($key1)); - $this->assertSame(4106465292, $this->cache->get($key2)); - $this->assertEquals(['get' => 2], $this->cache->redis_calls); - } - - public function test_get_float_values_persistent_cache() { - $key1 = rand_str(); - $key2 = rand_str(); - $this->cache->set($key1, 123.456); - $this->cache->set($key2, +0123.45e6); - $this->reset_cache_state(); - $this->assertSame(123.456, $this->cache->get($key1)); - $this->assertSame(123450000.0, $this->cache->get($key2)); - $this->assertEquals(['get' => 2], $this->cache->redis_calls); - } - - public function test_get_string_values_persistent_cache() { - $key1 = rand_str(); - $key2 = rand_str(); - $key3 = rand_str(); - $key4 = rand_str(); - $this->cache->set($key1, 'a plain old string'); - // To ensure numeric strings are not converted to integers. - $this->cache->set($key2, '42'); - $this->cache->set($key3, '123.456'); - $this->cache->set($key4, '+0123.45e6'); - $this->reset_cache_state(); - $this->assertEquals('a plain old string', $this->cache->get($key1)); - $this->assertSame('42', $this->cache->get($key2)); - $this->assertSame('123.456', $this->cache->get($key3)); - $this->assertSame('+0123.45e6', $this->cache->get($key4)); - $this->assertEquals(['get' => 4], $this->cache->redis_calls); - } - - public function test_get_array_values_persistent_cache() { - $key = rand_str(); - $value = ['one', 2, true]; - $this->cache->set($key, $value); - $this->reset_cache_state(); - $this->assertEquals($value, $this->cache->get($key)); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_get_object_values_persistent_cache() { - $key = rand_str(); - $value = new stdClass; - $value->one = 'two'; - $value->three = 'four'; - $this->cache->set($key, $value); - $this->reset_cache_state(); - $this->assertEquals($value, $this->cache->get($key)); - $this->assertEquals(['get' => 1], $this->cache->redis_calls); - } - - public function test_get_found() { - $key = rand_str(); - $found = null; - $this->cache->get($key, 'default', false, $found); - $this->assertFalse($found); - $this->cache->set($key, 'alpha', 'default'); - $this->cache->get($key, 'default', false, $found); - $this->assertTrue($found); - } - - public function test_get_multiple() { - $this->cache->set('foo1', 'bar', 'group1'); - $this->cache->set('foo2', 'bar', 'group1'); - $this->cache->set('foo1', 'bar', 'group2'); - - $found = $this->cache->get_multiple(['foo1', 'foo2', 'foo3'], 'group1'); - - $this->assertSame([ - 'foo1' => 'bar', - 'foo2' => 'bar', - 'foo3' => false, - ], $found); - - $this->assertEquals([ - 'mget' => 1, - 'set' => 3, - ], $this->cache->redis_calls); - } - - public function test_get_multiple_non_persistent() { - $this->cache->add_non_persistent_groups(['group1']); - $this->cache->set('foo1', 'bar', 'group1'); - $this->cache->set('foo2', 'bar', 'group1'); - - $found = $this->cache->get_multiple(['foo1', 'foo2', 'foo3'], 'group1'); - - $this->assertSame([ - 'foo1' => 'bar', - 'foo2' => 'bar', - 'foo3' => false, - ], $found); - - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_incr_non_persistent() { - $key = rand_str(); - - $this->cache->add_non_persistent_groups(['nonpersistent']); - $this->assertFalse($this->cache->incr($key, 1, 'nonpersistent')); - - $this->cache->set($key, 0, 'nonpersistent'); - $this->cache->incr($key, 1, 'nonpersistent'); - $this->assertEquals(1, $this->cache->get($key, 'nonpersistent')); - - $this->cache->incr($key, 2, 'nonpersistent'); - $this->assertEquals(3, $this->cache->get($key, 'nonpersistent')); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_incr_non_persistent_never_below_zero() { - $key = rand_str(); - $this->cache->add_non_persistent_groups(['nonpersistent']); - $this->cache->set($key, 1, 'nonpersistent'); - $this->assertEquals(1, $this->cache->get($key, 'nonpersistent')); - $this->cache->incr($key, -2, 'nonpersistent'); - $this->assertEquals(0, $this->cache->get($key, 'nonpersistent')); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_wp_cache_incr() { - $key = rand_str(); - - $this->assertFalse(wp_cache_incr($key)); - - wp_cache_set($key, 0); - wp_cache_incr($key); - $this->assertEquals(1, wp_cache_get($key)); - - wp_cache_incr($key, 2); - $this->assertEquals(3, wp_cache_get($key)); - } - - public function test_decr_non_persistent() { - $key = rand_str(); - - $this->cache->add_non_persistent_groups(['nonpersistent']); - $this->assertFalse($this->cache->decr($key, 1, 'nonpersistent')); - - $this->cache->set($key, 0, 'nonpersistent'); - $this->cache->decr($key, 1, 'nonpersistent'); - $this->assertEquals(0, $this->cache->get($key, 'nonpersistent')); - - $this->cache->set($key, 3, 'nonpersistent'); - $this->cache->decr($key, 1, 'nonpersistent'); - $this->assertEquals(2, $this->cache->get($key, 'nonpersistent')); - - $this->cache->decr($key, 2, 'nonpersistent'); - $this->assertEquals(0, $this->cache->get($key, 'nonpersistent')); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_decr_non_persistent_never_below_zero() { - $key = rand_str(); - $this->cache->add_non_persistent_groups(['nonpersistent']); - $this->cache->set($key, 1, 'nonpersistent'); - $this->assertEquals(1, $this->cache->get($key, 'nonpersistent')); - $this->cache->decr($key, 2, 'nonpersistent'); - $this->assertEquals(0, $this->cache->get($key, 'nonpersistent')); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_wp_cache_decr() { - $key = rand_str(); - - $this->assertFalse(wp_cache_decr($key)); - - wp_cache_set($key, 0); - wp_cache_decr($key); - $this->assertEquals(0, wp_cache_get($key)); - - wp_cache_set($key, 3); - wp_cache_decr($key); - $this->assertEquals(2, wp_cache_get($key)); - - wp_cache_decr($key, 2); - $this->assertEquals(0, wp_cache_get($key)); - } - - public function test_wp_cache_delete() { - $key = rand_str(); - $val = rand_str(); - - // Verify set - $this->assertTrue(wp_cache_set($key, $val)); - $this->assertEquals($val, wp_cache_get($key)); - - // Verify successful delete - $this->assertTrue(wp_cache_delete($key)); - $this->assertFalse(wp_cache_get($key)); - - // wp_cache_delete() does not have a $force method. - // Delete returns (bool) true when key is not set and $force is true - // $this->assertTrue( wp_cache_delete( $key, 'default', true ) ); - - $this->assertFalse(wp_cache_delete($key, 'default')); - } - - public function test_switch_to_blog() { - $key = rand_str(); - $val = rand_str(); - $val2 = rand_str(); - - // Single site ingnores switch_to_blog(). - $this->assertTrue($this->cache->set($key, $val)); - $this->assertEquals($val, $this->cache->get($key)); - $this->cache->switch_to_blog(999); - $this->assertEquals($val, $this->cache->get($key)); - $this->assertTrue($this->cache->set($key, $val2)); - $this->assertEquals($val2, $this->cache->get($key)); - $this->cache->switch_to_blog(get_current_blog_id()); - $this->assertEquals($val2, $this->cache->get($key)); - - // Global group - $this->assertTrue($this->cache->set($key, $val, 'global-cache-test')); - $this->assertEquals($val, $this->cache->get($key, 'global-cache-test')); - $this->cache->switch_to_blog(999); - $this->assertEquals($val, $this->cache->get($key, 'global-cache-test')); - $this->assertTrue($this->cache->set($key, $val2, 'global-cache-test')); - $this->assertEquals($val2, $this->cache->get($key, 'global-cache-test')); - $this->cache->switch_to_blog(get_current_blog_id()); - $this->assertEquals($val2, $this->cache->get($key, 'global-cache-test')); - } - - public function test_wp_cache_init() { - $new_blank_cache_object = new WP_Object_Cache(); - wp_cache_init(); - - global $wp_object_cache; - // Differs from core tests because we'll have two different Redis sockets - $this->assertEquals($wp_object_cache->cache, $new_blank_cache_object->cache); - } - - public function test_wp_cache_replace() { - $key = 'my-key'; - $val1 = 'first-val'; - $val2 = 'second-val'; - - $fake_key = 'my-fake-key'; - - // Save the first value to cache and verify - wp_cache_set($key, $val1); - $this->assertEquals($val1, wp_cache_get($key)); - - // Replace the value and verify - wp_cache_replace($key, $val2); - $this->assertEquals($val2, wp_cache_get($key)); - - // Non-existant key should fail - $this->assertFalse(wp_cache_replace($fake_key, $val1)); - - // Make sure $fake_key is not stored - $this->assertFalse(wp_cache_get($fake_key)); - } - - public function test_cache_key() { - $key = rand_str(); - $group = 'default'; - $true_key = WP_CACHE_KEY_SALT . json_encode(['', $group, $key]); - $this->cache->cache[$true_key] = 'beta'; - $this->assertEquals('beta', $this->cache->get($key, $group)); - $this->assertEmpty($this->cache->redis_calls); - } - - public function test_get_force() { - $key = rand_str(); - $group = 'default'; - $this->cache->set($key, 'alpha', $group); - $this->assertEquals('alpha', $this->cache->get($key, $group, true)); - $this->assertEquals(['get' => 1, 'set' => 1], $this->cache->redis_calls); - } - - public function test_add_get() { - $key = rand_str(); - $val = rand_str(); - - $this->cache->add($key, $val); - $this->assertEquals($val, $this->cache->get($key)); - $this->assertEquals(['set' => 1], $this->cache->redis_calls); - } - - public function test_add_get_0() { - $key = rand_str(); - $val = 0; - - // you can store zero in the cache - $this->cache->add($key, $val); - $this->assertEquals($val, $this->cache->get($key)); - $this->assertEquals(['set' => 1], $this->cache->redis_calls); - } - - public function test_add_get_null() { - $key = rand_str(); - $val = null; - - $this->assertTrue($this->cache->add($key, $val)); - $this->assertNull($this->cache->get($key)); - $this->assertEquals(['set' => 1], $this->cache->redis_calls); - } - - public function test_flush() { - $key = rand_str(); - $val = rand_str(); - - $this->cache->add($key, $val); - // item is visible to both cache objects - $this->assertEquals($val, $this->cache->get($key)); - $this->cache->flush(); - // If there is no value get returns false. - $this->assertFalse($this->cache->get($key)); - $this->assertEquals([ - 'flushdb' => 1, - 'get' => 1, - 'set' => 1, - ], $this->cache->redis_calls); - } - - public function test_add() { - $key = rand_str(); - $val1 = rand_str(); - $val2 = rand_str(); - - // add $key to the cache - $this->assertTrue($this->cache->add($key, $val1)); - $this->assertEquals($val1, $this->cache->get($key)); - // $key is in the cache, so reject new calls to add() - $this->assertFalse($this->cache->add($key, $val2)); - $this->assertEquals($val1, $this->cache->get($key)); - $this->assertEquals([ - // The second `add()`, which fails, removes internal cache. - // While this behavior isn't desirable from the performance perspective, - // it simplifies implementation. - 'get' => 1, - 'set' => 2, - ], $this->cache->redis_calls); - } - - public function test_replace() { - $key = rand_str(); - $val = rand_str(); - $val2 = rand_str(); - - // memcached rejects replace() if the key does not exist - $this->assertFalse($this->cache->replace($key, $val)); - $this->assertFalse($this->cache->get($key)); - $this->assertTrue($this->cache->add($key, $val)); - $this->assertEquals($val, $this->cache->get($key)); - $this->assertTrue($this->cache->replace($key, $val2)); - $this->assertEquals($val2, $this->cache->get($key)); - $this->assertEquals(['get' => 1, 'set' => 3], $this->cache->redis_calls); - } - - public function test_delete() { - $key = rand_str(); - $val = rand_str(); - - // Verify set - $this->assertTrue($this->cache->set($key, $val)); - $this->assertEquals($val, $this->cache->get($key)); - - // Verify successful delete - $this->assertTrue($this->cache->delete($key)); - $this->assertFalse($this->cache->get($key)); - - $this->assertFalse($this->cache->delete($key, 'default')); - $this->assertEquals([ - 'del' => 2, - 'get' => 1, - 'set' => 1, - ], $this->cache->redis_calls); - } - - public function test_incr() { - $key = rand_str(); - - $this->assertFalse($this->cache->incr($key)); - - $this->cache->set($key, 0); - $this->cache->incr($key); - $this->assertEquals(1, $this->cache->get($key)); - - $this->cache->incr($key, 2); - $this->assertEquals(3, $this->cache->get($key)); - $this->assertEquals(['evalSha' => 3, 'set' => 1], $this->cache->redis_calls); - } - - public function test_incr_separate_groups() { - $key = rand_str(); - $group1 = 'group1'; - $group2 = 'group2'; - - $this->assertFalse($this->cache->incr($key, 1, $group1)); - $this->assertFalse($this->cache->incr($key, 1, $group2)); - - $this->cache->set($key, 0, $group1); - $this->cache->incr($key, 1, $group1); - $this->cache->set($key, 0, $group2); - $this->cache->incr($key, 1, $group2); - $this->assertEquals(1, $this->cache->get($key, $group1)); - $this->assertEquals(1, $this->cache->get($key, $group2)); - - $this->cache->incr($key, 2, $group1); - $this->cache->incr($key, 1, $group2); - $this->assertEquals(3, $this->cache->get($key, $group1)); - $this->assertEquals(2, $this->cache->get($key, $group2)); - $this->assertEquals(['evalSha' => 6, 'set' => 2], $this->cache->redis_calls); - } - - public function test_incr_never_below_zero() { - $key = rand_str(); - $this->cache->set($key, 1); - $this->assertEquals(1, $this->cache->get($key)); - $this->cache->incr($key, -2); - $this->assertEquals(0, $this->cache->get($key)); - $this->assertEquals(['evalSha' => 1, 'set' => 1], $this->cache->redis_calls); - } - - public function test_decr() { - $key = rand_str(); - - $this->assertFalse($this->cache->decr($key)); - - $this->cache->set($key, 0); - $this->cache->decr($key); - $this->assertEquals(0, $this->cache->get($key)); - - $this->cache->set($key, 3); - $this->cache->decr($key); - $this->assertEquals(2, $this->cache->get($key)); - - $this->cache->decr($key, 2); - $this->assertEquals(0, $this->cache->get($key)); - $this->assertEquals(['evalSha' => 4, 'set' => 2], $this->cache->redis_calls); - } - - public function test_decr_separate_groups() { - $key = rand_str(); - $group1 = 'group1'; - $group2 = 'group2'; - - $this->assertFalse($this->cache->decr($key, 1, $group1)); - $this->assertFalse($this->cache->decr($key, 1, $group2)); - - $this->cache->set($key, 0, $group1); - $this->cache->decr($key, 1, $group1); - $this->cache->set($key, 0, $group2); - $this->cache->decr($key, 1, $group2); - $this->assertEquals(0, $this->cache->get($key, $group1)); - $this->assertEquals(0, $this->cache->get($key, $group2)); - - $this->cache->set($key, 3, $group1); - $this->cache->decr($key, 1, $group1); - $this->cache->set($key, 2, $group2); - $this->cache->decr($key, 1, $group2); - $this->assertEquals(2, $this->cache->get($key, $group1)); - $this->assertEquals(1, $this->cache->get($key, $group2)); - - $this->cache->decr($key, 2, $group1); - $this->cache->decr($key, 2, $group2); - $this->assertEquals(0, $this->cache->get($key, $group1)); - $this->assertEquals(0, $this->cache->get($key, $group2)); - $this->assertEquals(['evalSha' => 8, 'set' => 4], $this->cache->redis_calls); - } - - public function test_decr_never_below_zero() { - $key = rand_str(); - $this->cache->set($key, 1); - $this->assertEquals(1, $this->cache->get($key)); - $this->cache->decr($key, 2); - $this->assertEquals(0, $this->cache->get($key)); - $this->assertEquals(['evalSha' => 1, 'set' => 1], $this->cache->redis_calls); - } - - public function test_get_multiple_no_connection() { - $this->cache->set('foo1', 'bar', 'group1'); - $this->cache->set('foo2', 'bar', 'group1'); - - $this->cache->is_redis_connected = false; - $this->cache->cache = []; - - $found = $this->cache->get_multiple(['foo1', 'foo2', 'foo3'], 'group1'); - - $this->assertSame([ - 'foo1' => false, - 'foo2' => false, - 'foo3' => false, - ], $found); - } + private $cache; + + private function reset_cache_state() { + $this->cache->redis_calls = array(); + $this->cache->cache = array(); + } + + public function setUp(): void { + global $wp_object_cache, $redis_server; + + parent::setUp(); + + $redis_server = array( + 'host' => 'redis', + 'port' => 6379, + 'timeout' => 1000, + 'retry_interval' => 100, + ); + + $wp_object_cache = new WP_Object_Cache(); + $wp_object_cache->flush(); + + // create two cache objects with a shared cache dir + // this simulates a typical cache situation, two separate requests interacting + $this->cache = &$this->init_cache(); + $this->cache->redis_calls = array(); + } + + public function tearDown(): void { + parent::tearDown(); + $this->flush_cache(); + } + + public function &init_cache() { + $cache = new WP_Object_Cache(); + $cache->add_global_groups(array( + 'blog-details', + 'blog-id-cache', + 'blog-lookup', + 'global-cache-test', + 'global-posts', + 'rss', + 'site-lookup', + 'site-options', + 'site-transient', + 'user_meta', + 'userlogins', + 'usermeta', + 'users', + )); + return $cache; + } + + public function test_redis_connected() { + $this->assertTrue( isset( $this->cache->redis ) ); + $this->assertTrue( $this->cache->redis->IsConnected() ); + } + + public function test_redis_reload_connection_closed() { + // Connection is live + $this->cache->set( 'foo', 'bar' ); + $this->assertTrue( $this->cache->redis->IsConnected() ); + $this->assertTrue( $this->cache->is_redis_connected ); + $this->assertEquals( 'bar', $this->cache->get( 'foo', 'default', true ) ); + // Connection is closed, but automatic reconnection occurs. + $this->cache->redis->close(); + $this->assertFalse( $this->cache->redis->IsConnected() ); + // Reload occurs with set() + $this->cache->set( 'foo', 'banana' ); + $this->assertEquals( 'banana', $this->cache->get( 'foo' ) ); + $this->assertTrue( $this->cache->is_redis_connected ); + $this->assertTrue( $this->cache->redis->IsConnected() ); + } + + public function test_redis_bad_authentication() { + global $redis_server; + + $redis_server['host'] = '127.0.0.1'; + $redis_server['port'] = 9999; + $redis_server['auth'] = 'foobar'; + + $this->expectWarning(); + + $cache = new WP_Object_Cache(); + $this->assertTrue($cache->is_retriable_error_message( + str_replace( 'WP Redis: ', '', $cache->last_triggered_error ) + )); + $this->assertFalse( $cache->is_redis_connected ); + // Fails back to the internal object cache + $cache->set( 'foo', 'bar' ); + $this->assertEquals( 'bar', $cache->get( 'foo' ) ); + } + + public function test_miss() { + $this->assertFalse( $this->cache->get( rand_str() ) ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_set() { + $key = rand_str(); + $val1 = rand_str(); + $val2 = rand_str(); + + // memcached accepts set() if the key does not exist + $this->assertTrue( $this->cache->set( $key, $val1 ) ); + $this->assertEquals( $val1, $this->cache->get( $key ) ); + // Second set() with same key should be allowed + $this->assertTrue( $this->cache->set( $key, $val2 ) ); + $this->assertEquals( $val2, $this->cache->get( $key ) ); + $this->assertEquals( array( 'set' => 2 ), $this->cache->redis_calls ); + } + + // Make sure objects are cloned going to and from the cache + public function test_object_refs() { + $key = rand_str(); + $object_a = new stdClass(); + $object_a->foo = 'alpha'; + $this->cache->set( $key, $object_a ); + $object_a->foo = 'bravo'; + $object_b = $this->cache->get( $key ); + $this->assertEquals( 'alpha', $object_b->foo ); + $object_b->foo = 'charlie'; + $this->assertEquals( 'bravo', $object_a->foo ); + + $key = rand_str(); + $object_a = new stdClass(); + $object_a->foo = 'alpha'; + $this->cache->add( $key, $object_a ); + $object_a->foo = 'bravo'; + $object_b = $this->cache->get( $key ); + $this->assertEquals( 'alpha', $object_b->foo ); + $object_b->foo = 'charlie'; + $this->assertEquals( 'bravo', $object_a->foo ); + } + + public function test_get_already_exists_internal() { + $key = rand_str(); + $this->cache->set( $key, 'alpha' ); + $this->assertEquals( array( 'set' => 1 ), $this->cache->redis_calls ); + $this->cache->redis_calls = array(); // reset to limit scope of test + $this->assertEquals( 'alpha', $this->cache->get( $key ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_get_missing_persistent() { + $key = rand_str(); + $this->cache->get( $key ); + $this->cache->get( $key ); + $this->assertEquals( array( 'get' => 2 ), $this->cache->redis_calls ); + } + + public function test_get_non_persistent_group() { + $key = rand_str(); + $group = 'nonpersistent'; + $this->cache->add_non_persistent_groups( $group ); + $this->cache->get( $key, $group ); + $this->assertEmpty( $this->cache->redis_calls ); + $this->cache->get( $key, $group ); + $this->assertEmpty( $this->cache->redis_calls ); + $this->cache->set( $key, 'alpha', $group ); + $this->cache->get( $key, $group ); + $this->assertEmpty( $this->cache->redis_calls ); + $this->cache->get( $key, $group ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_get_false_value_persistent_cache() { + $key = rand_str(); + $this->cache->set( $key, false ); + $this->reset_cache_state(); + $found = null; + $this->assertFalse( $this->cache->get( $key, 'default', false, $found ) ); + $this->assertTrue( $found ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_true_value_persistent_cache() { + $key = rand_str(); + $this->cache->set( $key, true ); + $this->reset_cache_state(); + $found = null; + $this->assertTrue( $this->cache->get( $key, 'default', false, $found ) ); + $this->assertTrue( $found ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_null_value_persistent_cache() { + $key = rand_str(); + $this->cache->set( $key, null ); + $this->reset_cache_state(); + $found = null; + $this->assertNull( $this->cache->get( $key, 'default', false, $found ) ); + $this->assertTrue( $found ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_int_values_persistent_cache() { + $key1 = rand_str(); + $key2 = rand_str(); + $this->cache->set( $key1, 123 ); + $this->cache->set( $key2, 0xf4c3b00c ); + $this->reset_cache_state(); + // Should be upgraded to more strict comparison if change proposed in issue #181 is merged. + $this->assertSame( 123, $this->cache->get( $key1 ) ); + $this->assertSame( 4106465292, $this->cache->get( $key2 ) ); + $this->assertEquals( array( 'get' => 2 ), $this->cache->redis_calls ); + } + + public function test_get_float_values_persistent_cache() { + $key1 = rand_str(); + $key2 = rand_str(); + $this->cache->set( $key1, 123.456 ); + $this->cache->set( $key2, +0123.45e6 ); + $this->reset_cache_state(); + $this->assertSame( 123.456, $this->cache->get( $key1 ) ); + $this->assertSame( 123450000.0, $this->cache->get( $key2 ) ); + $this->assertEquals( array( 'get' => 2 ), $this->cache->redis_calls ); + } + + public function test_get_string_values_persistent_cache() { + $key1 = rand_str(); + $key2 = rand_str(); + $key3 = rand_str(); + $key4 = rand_str(); + $this->cache->set( $key1, 'a plain old string' ); + // To ensure numeric strings are not converted to integers. + $this->cache->set( $key2, '42' ); + $this->cache->set( $key3, '123.456' ); + $this->cache->set( $key4, '+0123.45e6' ); + $this->reset_cache_state(); + $this->assertEquals( 'a plain old string', $this->cache->get( $key1 ) ); + $this->assertSame( '42', $this->cache->get( $key2 ) ); + $this->assertSame( '123.456', $this->cache->get( $key3 ) ); + $this->assertSame( '+0123.45e6', $this->cache->get( $key4 ) ); + $this->assertEquals( array( 'get' => 4 ), $this->cache->redis_calls ); + } + + public function test_get_array_values_persistent_cache() { + $key = rand_str(); + $value = array( 'one', 2, true ); + $this->cache->set( $key, $value ); + $this->reset_cache_state(); + $this->assertEquals( $value, $this->cache->get( $key ) ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_object_values_persistent_cache() { + $key = rand_str(); + $value = new stdClass(); + $value->one = 'two'; + $value->three = 'four'; + $this->cache->set( $key, $value ); + $this->reset_cache_state(); + $this->assertEquals( $value, $this->cache->get( $key ) ); + $this->assertEquals( array( 'get' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_found() { + $key = rand_str(); + $found = null; + $this->cache->get( $key, 'default', false, $found ); + $this->assertFalse( $found ); + $this->cache->set( $key, 'alpha', 'default' ); + $this->cache->get( $key, 'default', false, $found ); + $this->assertTrue( $found ); + } + + public function test_get_multiple() { + $this->cache->set( 'foo1', 'bar', 'group1' ); + $this->cache->set( 'foo2', 'bar', 'group1' ); + $this->cache->set( 'foo1', 'bar', 'group2' ); + + $found = $this->cache->get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' ); + + $this->assertSame(array( + 'foo1' => 'bar', + 'foo2' => 'bar', + 'foo3' => false, + ), $found); + + $this->assertEquals(array( + 'mget' => 1, + 'set' => 3, + ), $this->cache->redis_calls); + } + + public function test_get_multiple_non_persistent() { + $this->cache->add_non_persistent_groups( array( 'group1' ) ); + $this->cache->set( 'foo1', 'bar', 'group1' ); + $this->cache->set( 'foo2', 'bar', 'group1' ); + + $found = $this->cache->get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' ); + + $this->assertSame(array( + 'foo1' => 'bar', + 'foo2' => 'bar', + 'foo3' => false, + ), $found); + + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_incr_non_persistent() { + $key = rand_str(); + + $this->cache->add_non_persistent_groups( array( 'nonpersistent' ) ); + $this->assertFalse( $this->cache->incr( $key, 1, 'nonpersistent' ) ); + + $this->cache->set( $key, 0, 'nonpersistent' ); + $this->cache->incr( $key, 1, 'nonpersistent' ); + $this->assertEquals( 1, $this->cache->get( $key, 'nonpersistent' ) ); + + $this->cache->incr( $key, 2, 'nonpersistent' ); + $this->assertEquals( 3, $this->cache->get( $key, 'nonpersistent' ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_incr_non_persistent_never_below_zero() { + $key = rand_str(); + $this->cache->add_non_persistent_groups( array( 'nonpersistent' ) ); + $this->cache->set( $key, 1, 'nonpersistent' ); + $this->assertEquals( 1, $this->cache->get( $key, 'nonpersistent' ) ); + $this->cache->incr( $key, -2, 'nonpersistent' ); + $this->assertEquals( 0, $this->cache->get( $key, 'nonpersistent' ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_wp_cache_incr() { + $key = rand_str(); + + $this->assertFalse( wp_cache_incr( $key ) ); + + wp_cache_set( $key, 0 ); + wp_cache_incr( $key ); + $this->assertEquals( 1, wp_cache_get( $key ) ); + + wp_cache_incr( $key, 2 ); + $this->assertEquals( 3, wp_cache_get( $key ) ); + } + + public function test_decr_non_persistent() { + $key = rand_str(); + + $this->cache->add_non_persistent_groups( array( 'nonpersistent' ) ); + $this->assertFalse( $this->cache->decr( $key, 1, 'nonpersistent' ) ); + + $this->cache->set( $key, 0, 'nonpersistent' ); + $this->cache->decr( $key, 1, 'nonpersistent' ); + $this->assertEquals( 0, $this->cache->get( $key, 'nonpersistent' ) ); + + $this->cache->set( $key, 3, 'nonpersistent' ); + $this->cache->decr( $key, 1, 'nonpersistent' ); + $this->assertEquals( 2, $this->cache->get( $key, 'nonpersistent' ) ); + + $this->cache->decr( $key, 2, 'nonpersistent' ); + $this->assertEquals( 0, $this->cache->get( $key, 'nonpersistent' ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_decr_non_persistent_never_below_zero() { + $key = rand_str(); + $this->cache->add_non_persistent_groups( array( 'nonpersistent' ) ); + $this->cache->set( $key, 1, 'nonpersistent' ); + $this->assertEquals( 1, $this->cache->get( $key, 'nonpersistent' ) ); + $this->cache->decr( $key, 2, 'nonpersistent' ); + $this->assertEquals( 0, $this->cache->get( $key, 'nonpersistent' ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_wp_cache_decr() { + $key = rand_str(); + + $this->assertFalse( wp_cache_decr( $key ) ); + + wp_cache_set( $key, 0 ); + wp_cache_decr( $key ); + $this->assertEquals( 0, wp_cache_get( $key ) ); + + wp_cache_set( $key, 3 ); + wp_cache_decr( $key ); + $this->assertEquals( 2, wp_cache_get( $key ) ); + + wp_cache_decr( $key, 2 ); + $this->assertEquals( 0, wp_cache_get( $key ) ); + } + + public function test_wp_cache_delete() { + $key = rand_str(); + $val = rand_str(); + + // Verify set + $this->assertTrue( wp_cache_set( $key, $val ) ); + $this->assertEquals( $val, wp_cache_get( $key ) ); + + // Verify successful delete + $this->assertTrue( wp_cache_delete( $key ) ); + $this->assertFalse( wp_cache_get( $key ) ); + + // wp_cache_delete() does not have a $force method. + // Delete returns (bool) true when key is not set and $force is true + // $this->assertTrue( wp_cache_delete( $key, 'default', true ) ); + + $this->assertFalse( wp_cache_delete( $key, 'default' ) ); + } + + public function test_switch_to_blog() { + $key = rand_str(); + $val = rand_str(); + $val2 = rand_str(); + + // Single site ingnores switch_to_blog(). + $this->assertTrue( $this->cache->set( $key, $val ) ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->cache->switch_to_blog( 999 ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->assertTrue( $this->cache->set( $key, $val2 ) ); + $this->assertEquals( $val2, $this->cache->get( $key ) ); + $this->cache->switch_to_blog( get_current_blog_id() ); + $this->assertEquals( $val2, $this->cache->get( $key ) ); + + // Global group + $this->assertTrue( $this->cache->set( $key, $val, 'global-cache-test' ) ); + $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) ); + $this->cache->switch_to_blog( 999 ); + $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) ); + $this->assertTrue( $this->cache->set( $key, $val2, 'global-cache-test' ) ); + $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) ); + $this->cache->switch_to_blog( get_current_blog_id() ); + $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) ); + } + + public function test_wp_cache_init() { + $new_blank_cache_object = new WP_Object_Cache(); + wp_cache_init(); + + global $wp_object_cache; + // Differs from core tests because we'll have two different Redis sockets + $this->assertEquals( $wp_object_cache->cache, $new_blank_cache_object->cache ); + } + + public function test_wp_cache_replace() { + $key = 'my-key'; + $val1 = 'first-val'; + $val2 = 'second-val'; + + $fake_key = 'my-fake-key'; + + // Save the first value to cache and verify + wp_cache_set( $key, $val1 ); + $this->assertEquals( $val1, wp_cache_get( $key ) ); + + // Replace the value and verify + wp_cache_replace( $key, $val2 ); + $this->assertEquals( $val2, wp_cache_get( $key ) ); + + // Non-existant key should fail + $this->assertFalse( wp_cache_replace( $fake_key, $val1 ) ); + + // Make sure $fake_key is not stored + $this->assertFalse( wp_cache_get( $fake_key ) ); + } + + public function test_cache_key() { + $key = rand_str(); + $group = 'default'; + $true_key = WP_CACHE_KEY_SALT . json_encode( array( '', $group, $key ) ); + $this->cache->cache[ $true_key ] = 'beta'; + $this->assertEquals( 'beta', $this->cache->get( $key, $group ) ); + $this->assertEmpty( $this->cache->redis_calls ); + } + + public function test_get_force() { + $key = rand_str(); + $group = 'default'; + $this->cache->set( $key, 'alpha', $group ); + $this->assertEquals( 'alpha', $this->cache->get( $key, $group, true ) ); + $this->assertEquals( array( 'get' => 1, 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_add_get() { + $key = rand_str(); + $val = rand_str(); + + $this->cache->add( $key, $val ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->assertEquals( array( 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_add_get_0() { + $key = rand_str(); + $val = 0; + + // you can store zero in the cache + $this->cache->add( $key, $val ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->assertEquals( array( 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_add_get_null() { + $key = rand_str(); + $val = null; + + $this->assertTrue( $this->cache->add( $key, $val ) ); + $this->assertNull( $this->cache->get( $key ) ); + $this->assertEquals( array( 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_flush() { + $key = rand_str(); + $val = rand_str(); + + $this->cache->add( $key, $val ); + // item is visible to both cache objects + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->cache->flush(); + // If there is no value get returns false. + $this->assertFalse( $this->cache->get( $key ) ); + $this->assertEquals(array( + 'flushdb' => 1, + 'get' => 1, + 'set' => 1, + ), $this->cache->redis_calls); + } + + public function test_add() { + $key = rand_str(); + $val1 = rand_str(); + $val2 = rand_str(); + + // add $key to the cache + $this->assertTrue( $this->cache->add( $key, $val1 ) ); + $this->assertEquals( $val1, $this->cache->get( $key ) ); + // $key is in the cache, so reject new calls to add() + $this->assertFalse( $this->cache->add( $key, $val2 ) ); + $this->assertEquals( $val1, $this->cache->get( $key ) ); + $this->assertEquals(array( + // The second `add()`, which fails, removes internal cache. + // While this behavior isn't desirable from the performance perspective, + // it simplifies implementation. + 'get' => 1, + 'set' => 2, + ), $this->cache->redis_calls); + } + + public function test_replace() { + $key = rand_str(); + $val = rand_str(); + $val2 = rand_str(); + + // memcached rejects replace() if the key does not exist + $this->assertFalse( $this->cache->replace( $key, $val ) ); + $this->assertFalse( $this->cache->get( $key ) ); + $this->assertTrue( $this->cache->add( $key, $val ) ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + $this->assertTrue( $this->cache->replace( $key, $val2 ) ); + $this->assertEquals( $val2, $this->cache->get( $key ) ); + $this->assertEquals( array( 'get' => 1, 'set' => 3 ), $this->cache->redis_calls ); + } + + public function test_delete() { + $key = rand_str(); + $val = rand_str(); + + // Verify set + $this->assertTrue( $this->cache->set( $key, $val ) ); + $this->assertEquals( $val, $this->cache->get( $key ) ); + + // Verify successful delete + $this->assertTrue( $this->cache->delete( $key ) ); + $this->assertFalse( $this->cache->get( $key ) ); + + $this->assertFalse( $this->cache->delete( $key, 'default' ) ); + $this->assertEquals(array( + 'del' => 2, + 'get' => 1, + 'set' => 1, + ), $this->cache->redis_calls); + } + + public function test_incr() { + $key = rand_str(); + + $this->assertFalse( $this->cache->incr( $key ) ); + + $this->cache->set( $key, 0 ); + $this->cache->incr( $key ); + $this->assertEquals( 1, $this->cache->get( $key ) ); + + $this->cache->incr( $key, 2 ); + $this->assertEquals( 3, $this->cache->get( $key ) ); + $this->assertEquals( array( 'evalSha' => 3, 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_incr_separate_groups() { + $key = rand_str(); + $group1 = 'group1'; + $group2 = 'group2'; + + $this->assertFalse( $this->cache->incr( $key, 1, $group1 ) ); + $this->assertFalse( $this->cache->incr( $key, 1, $group2 ) ); + + $this->cache->set( $key, 0, $group1 ); + $this->cache->incr( $key, 1, $group1 ); + $this->cache->set( $key, 0, $group2 ); + $this->cache->incr( $key, 1, $group2 ); + $this->assertEquals( 1, $this->cache->get( $key, $group1 ) ); + $this->assertEquals( 1, $this->cache->get( $key, $group2 ) ); + + $this->cache->incr( $key, 2, $group1 ); + $this->cache->incr( $key, 1, $group2 ); + $this->assertEquals( 3, $this->cache->get( $key, $group1 ) ); + $this->assertEquals( 2, $this->cache->get( $key, $group2 ) ); + $this->assertEquals( array( 'evalSha' => 6, 'set' => 2 ), $this->cache->redis_calls ); + } + + public function test_incr_never_below_zero() { + $key = rand_str(); + $this->cache->set( $key, 1 ); + $this->assertEquals( 1, $this->cache->get( $key ) ); + $this->cache->incr( $key, -2 ); + $this->assertEquals( 0, $this->cache->get( $key ) ); + $this->assertEquals( array( 'evalSha' => 1, 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_decr() { + $key = rand_str(); + + $this->assertFalse( $this->cache->decr( $key ) ); + + $this->cache->set( $key, 0 ); + $this->cache->decr( $key ); + $this->assertEquals( 0, $this->cache->get( $key ) ); + + $this->cache->set( $key, 3 ); + $this->cache->decr( $key ); + $this->assertEquals( 2, $this->cache->get( $key ) ); + + $this->cache->decr( $key, 2 ); + $this->assertEquals( 0, $this->cache->get( $key ) ); + $this->assertEquals( array( 'evalSha' => 4, 'set' => 2 ), $this->cache->redis_calls ); + } + + public function test_decr_separate_groups() { + $key = rand_str(); + $group1 = 'group1'; + $group2 = 'group2'; + + $this->assertFalse( $this->cache->decr( $key, 1, $group1 ) ); + $this->assertFalse( $this->cache->decr( $key, 1, $group2 ) ); + + $this->cache->set( $key, 0, $group1 ); + $this->cache->decr( $key, 1, $group1 ); + $this->cache->set( $key, 0, $group2 ); + $this->cache->decr( $key, 1, $group2 ); + $this->assertEquals( 0, $this->cache->get( $key, $group1 ) ); + $this->assertEquals( 0, $this->cache->get( $key, $group2 ) ); + + $this->cache->set( $key, 3, $group1 ); + $this->cache->decr( $key, 1, $group1 ); + $this->cache->set( $key, 2, $group2 ); + $this->cache->decr( $key, 1, $group2 ); + $this->assertEquals( 2, $this->cache->get( $key, $group1 ) ); + $this->assertEquals( 1, $this->cache->get( $key, $group2 ) ); + + $this->cache->decr( $key, 2, $group1 ); + $this->cache->decr( $key, 2, $group2 ); + $this->assertEquals( 0, $this->cache->get( $key, $group1 ) ); + $this->assertEquals( 0, $this->cache->get( $key, $group2 ) ); + $this->assertEquals( array( 'evalSha' => 8, 'set' => 4 ), $this->cache->redis_calls ); + } + + public function test_decr_never_below_zero() { + $key = rand_str(); + $this->cache->set( $key, 1 ); + $this->assertEquals( 1, $this->cache->get( $key ) ); + $this->cache->decr( $key, 2 ); + $this->assertEquals( 0, $this->cache->get( $key ) ); + $this->assertEquals( array( 'evalSha' => 1, 'set' => 1 ), $this->cache->redis_calls ); + } + + public function test_get_multiple_no_connection() { + $this->cache->set( 'foo1', 'bar', 'group1' ); + $this->cache->set( 'foo2', 'bar', 'group1' ); + + $this->cache->is_redis_connected = false; + $this->cache->cache = array(); + + $found = $this->cache->get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' ); + + $this->assertSame(array( + 'foo1' => false, + 'foo2' => false, + 'foo3' => false, + ), $found); + } }