From b9ca52b657962214e581ae2c891adfa2e3a76ebf Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 11 Nov 2022 09:57:21 -0700 Subject: [PATCH 1/5] - add filter `graphql_cache_is_object_cache_inabled` to allow dynamically enabling/disabling the object cache. - composer run fix-cs --- src/Cache/Invalidation.php | 1 - src/Cache/Results.php | 38 +++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Cache/Invalidation.php b/src/Cache/Invalidation.php index 75f42e79..19861062 100644 --- a/src/Cache/Invalidation.php +++ b/src/Cache/Invalidation.php @@ -30,7 +30,6 @@ public function __construct( Collection $collection ) { * Initialize the actions to listen for */ public function init() { - // @phpcs:ignore do_action( 'graphql_cache_invalidation_init', $this ); diff --git a/src/Cache/Results.php b/src/Cache/Results.php index d8f60a4e..ce217581 100644 --- a/src/Cache/Results.php +++ b/src/Cache/Results.php @@ -6,6 +6,8 @@ namespace WPGraphQL\SmartCache\Cache; +use WPGraphQL; +use WPGraphQL\Request; use WPGraphQL\SmartCache\Admin\Settings; class Results extends Query { @@ -19,6 +21,11 @@ class Results extends Query { */ protected $is_cached = []; + /** + * @var + */ + protected $request; + public function init() { add_filter( 'pre_graphql_execute_request', [ $this, 'get_query_results_from_cache_cb' ], 10, 2 ); add_action( 'graphql_return_response', [ $this, 'save_query_results_to_cache_cb' ], 10, 8 ); @@ -92,17 +99,20 @@ public function add_cache_key_to_response_extensions( /** * Look for a 'cached' response for this exact query, variables and operation name * - * @param mixed|array|object $result The response from execution. Array for batch requests, + * @param mixed|array|object $result The response from execution. Array for batch requests, * single object for individual requests - * @param WPGraphql/Request $request The Request object + * @param Request $request * * @return mixed|array|object|null The response or null if not found in cache */ - public function get_query_results_from_cache_cb( $result, $request ) { + public function get_query_results_from_cache_cb( $result, Request $request ) { + + $this->request = $request; + // if caching is not enabled or the request is authenticated, bail early // right now we're not supporting GraphQL cache for authenticated requests. // Possibly in the future. - if ( ! Settings::caching_enabled() || is_user_logged_in() ) { + if ( ! $this->is_object_cache_enabled() ) { return $result; } @@ -157,16 +167,22 @@ public function get_result( $query_id, $query_string, $variables, $operation_nam * @return bool */ protected function is_object_cache_enabled() { - if ( is_user_logged_in() ) { - return false; - } + + // default to disabled + $enabled = false; // if caching is enabled, respect it if ( Settings::caching_enabled() ) { - return true; + $enabled = true; } - return false; + // however, if the user is logged in, we should bypass the cache + if ( is_user_logged_in() ) { + $enabled = false; + } + + // @phpcs:ignore + return (bool) apply_filters( 'graphql_cache_is_object_cache_enabled', $enabled, $this->request ); } /** @@ -214,7 +230,7 @@ public function save_query_results_to_cache_cb( return; } - // If do not have a cached version, or it expired, save the results again with new expiration + // If we do not have a cached version, or it expired, save the results again with new expiration $cached_result = $this->get( $key ); if ( false === $cached_result ) { @@ -231,7 +247,7 @@ public function save_query_results_to_cache_cb( * @return bool True on success, false on failure. */ public function purge_all() { - if ( ! Settings::caching_enabled() ) { + if ( ! $this->is_object_cache_enabled() ) { return false; } From b5f6ccc9b5488720bcd7c0abe7109f7cb1680ae7 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 11 Nov 2022 11:00:00 -0700 Subject: [PATCH 2/5] - composer run fix-cs --- src/Cache/Results.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Cache/Results.php b/src/Cache/Results.php index ce217581..94977458 100644 --- a/src/Cache/Results.php +++ b/src/Cache/Results.php @@ -106,7 +106,6 @@ public function add_cache_key_to_response_extensions( * @return mixed|array|object|null The response or null if not found in cache */ public function get_query_results_from_cache_cb( $result, Request $request ) { - $this->request = $request; // if caching is not enabled or the request is authenticated, bail early From ae0bb60f25d4b8ee01c6ad5e9ff42ef148a15c1e Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 11 Nov 2022 12:57:49 -0700 Subject: [PATCH 3/5] - remove condition for the purge_all method --- src/Cache/Results.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Cache/Results.php b/src/Cache/Results.php index 94977458..b444528f 100644 --- a/src/Cache/Results.php +++ b/src/Cache/Results.php @@ -246,10 +246,6 @@ public function save_query_results_to_cache_cb( * @return bool True on success, false on failure. */ public function purge_all() { - if ( ! $this->is_object_cache_enabled() ) { - return false; - } - return parent::purge_all(); } From e336eb199338c25bd0eaa02ed875a9e1f42e5897 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 11 Nov 2022 13:47:38 -0700 Subject: [PATCH 4/5] - update tests --- tests/functional/AdminSettingsGrantCest.php | 2 +- tests/wpunit/CachedQueryTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/AdminSettingsGrantCest.php b/tests/functional/AdminSettingsGrantCest.php index 1e6eae12..742eb1a3 100644 --- a/tests/functional/AdminSettingsGrantCest.php +++ b/tests/functional/AdminSettingsGrantCest.php @@ -23,7 +23,7 @@ public function saveAllowOnlySettingsTest( FunctionalTester $I ) { } public function testChangeAllowTriggersPurge( FunctionalTester $I ) { - $I->wantTo( 'Change the allow/deny grant glopbal setting and verify cache is purged' ); + $I->wantTo( 'Change the allow/deny grant global setting and verify cache is purged' ); // Enable caching for this test $I->haveOptionInDatabase( 'graphql_cache_section', [ 'cache_toggle' => 'on' ] ); diff --git a/tests/wpunit/CachedQueryTest.php b/tests/wpunit/CachedQueryTest.php index bdf81877..8966290b 100644 --- a/tests/wpunit/CachedQueryTest.php +++ b/tests/wpunit/CachedQueryTest.php @@ -164,7 +164,7 @@ public function testPurgeCacheWhenNotEnabled() { $results_object = new Results(); $response = $results_object->purge_all(); - $this->assertFalse( $response ); + $this->assertNotFalse( $response ); } public function testPurgeCacheWhenNothingCached() { From ae53ccc5ac1ac3e6bc09b10c883a777ba4873369 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Fri, 11 Nov 2022 13:50:11 -0700 Subject: [PATCH 5/5] - remove redundant function that calls parent function --- src/Cache/Results.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Cache/Results.php b/src/Cache/Results.php index b444528f..99f796fa 100644 --- a/src/Cache/Results.php +++ b/src/Cache/Results.php @@ -239,16 +239,6 @@ public function save_query_results_to_cache_cb( } } - /** - * Searches the database for all graphql transients matching our prefix - * - * @return int|false Count of the number deleted. False if error, nothing to delete or caching not enabled. - * @return bool True on success, false on failure. - */ - public function purge_all() { - return parent::purge_all(); - } - /** * When an item changed and this callback is triggered to delete results we have cached for that list of nodes * Related to the data type that changed.