Skip to content

Commit

Permalink
Merge pull request #178 from wp-graphql/feat/#177-exclude-specific-qu…
Browse files Browse the repository at this point in the history
…eries

feat: add new "graphql_cache_is_object_cache_inabled" filter
  • Loading branch information
jasonbahl authored Nov 11, 2022
2 parents e86416d + ae53ccc commit b66def5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
49 changes: 25 additions & 24 deletions src/Cache/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace WPGraphQL\SmartCache\Cache;

use WPGraphQL;
use WPGraphQL\Request;
use WPGraphQL\SmartCache\Admin\Settings;

class Results extends Query {
Expand All @@ -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 );
Expand Down Expand Up @@ -92,17 +99,19 @@ 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;
}

Expand Down Expand Up @@ -157,16 +166,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;
}

// however, if the user is logged in, we should bypass the cache
if ( is_user_logged_in() ) {
$enabled = false;
}

return false;
// @phpcs:ignore
return (bool) apply_filters( 'graphql_cache_is_object_cache_enabled', $enabled, $this->request );
}

/**
Expand Down Expand Up @@ -214,7 +229,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 ) {
Expand All @@ -224,20 +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() {
if ( ! Settings::caching_enabled() ) {
return false;
}

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.
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/AdminSettingsGrantCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
Expand Down
2 changes: 1 addition & 1 deletion tests/wpunit/CachedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b66def5

Please sign in to comment.