Skip to content

Commit fe967fc

Browse files
authored
Merge pull request #230 from wp-graphql/fix/#229-disable-maps-when-object-cache-is-off
fix: disable cache maps when "Use Object Cache" is disabled
2 parents e3ca36c + 9479e3d commit fe967fc

File tree

7 files changed

+58
-4
lines changed

7 files changed

+58
-4
lines changed

.github/workflows/tests-wordpress.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ jobs:
1515
runs-on: ubuntu-latest
1616
name: WordPress ${{ matrix.wordpress }} Integration Tests on PHP ${{ matrix.php }}
1717
strategy:
18+
fail-fast: false
1819
matrix:
1920
php: [ '8.0', '7.4' ]
2021
wordpress: [ '5.9', '5.8', '5.7.2', '5.6' ]
22+
wpgraphql_version: [ 'latest' ]
2123
include:
2224
- php: '8.0'
2325
wordpress: '6.0'
2426
- php: '8.1'
2527
wordpress: '6.1'
28+
# Temporary inclusion while we resolve the tests that broke in the WPGraphQL v1.14.5 release
29+
# this allows us to continue testing new features to this codebase while we work
30+
# toward updating the tests impacted by that release
31+
- php: '8.1'
32+
wordpress: '6.2'
33+
wpgraphql_version: '1.14.4'
2634
steps:
2735
- name: Checkout
2836
uses: actions/checkout@v2
@@ -58,7 +66,7 @@ jobs:
5866
run: composer update
5967

6068
- name: Run acceptance, functional tests
61-
run: docker-compose run -e DEBUG=1 testing
69+
run: docker-compose run -e DEBUG=1 -e WPGRAPHQL_VERSION=${{matrix.wpgraphql_version}} testing
6270
env:
6371
WP_VERSION: ${{ matrix.wordpress }}
6472
PHP_VERSION: ${{ matrix.php }}

src/Admin/Settings.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,29 @@ public static function caching_enabled() {
2323
// get the cache_toggle setting
2424
$option = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'cache_toggle', false, 'graphql_cache_section' ) : false;
2525

26+
$enabled = ( 'on' === $option );
27+
28+
$enabled = apply_filters( 'wpgraphql_cache_wordpress_cache_enabled', (bool) $enabled );
29+
2630
// if there's no user logged in, and GraphQL Caching is enabled
27-
return ( 'on' === $option );
31+
return (bool) $enabled;
32+
}
33+
34+
/**
35+
* Whether cache maps are enabled.
36+
*
37+
* Cache maps are used to track which nodes and list keys are associated with which queries,
38+
* and can be referenced to purge specific queries.
39+
*
40+
* Default behavior is to only enable building and storage of the cache maps if "WordPress Cache" (non-network cache) is enabled, but this can be filtered to be enabled without WordPress cache being enabled.
41+
*
42+
* @return bool
43+
*/
44+
public static function cache_maps_enabled() {
45+
46+
// Whether "WordPress Cache" (object/transient) cache is enabled
47+
$enabled = self::caching_enabled();
48+
return (bool) apply_filters( 'wpgraphql_cache_enable_cache_maps', (bool) $enabled );
2849
}
2950

3051
/**

src/Cache/Collection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GraphQL\Executor\ExecutionResult;
1111
use GraphQL\Type\Schema;
1212
use WPGraphQL\Request;
13+
use WPGraphQL\SmartCache\Admin\Settings;
1314

1415
class Collection extends Query {
1516

@@ -62,6 +63,12 @@ public function save_query_mapping_cb(
6263
$request,
6364
$query_id
6465
) {
66+
67+
// If cache maps are not enabled, do nothing
68+
if ( ! Settings::cache_maps_enabled() ) {
69+
return;
70+
}
71+
6572
$request_key = $this->build_key( $query_id, $query, $variables, $operation );
6673

6774
// get the runtime nodes from the query analyzer

tests/_support/TestCase/WPGraphQLSmartCacheTestCaseWithSeedDataAndPopulatedCaches.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public function setUp(): void {
187187
*/
188188
$this->clearSchema();
189189

190+
// enable graphql cache maps
191+
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );
192+
190193
// prevent default category from being added to posts on creation
191194
update_option( 'default_category', 0 );
192195

tests/wpunit/CacheCollectionTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88

99
class CacheCollectionTest extends \Codeception\TestCase\WPTestCase {
1010

11-
public function testAddData() {
11+
public function _setUp() {
12+
13+
// enable graphql cache maps
14+
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );
15+
16+
parent::_setUp(); // TODO: Change the autogenerated stub
17+
}
18+
19+
public function testAddData() {
1220
$key = uniqid( 'test-' );
1321
$content = 'foo-bar';
1422

tests/wpunit/CacheFromConnectionFieldNameTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class CacheFromConnectionFieldNameTest extends \Codeception\TestCase\WPTestCase
1010

1111
public function setUp(): void {
1212
// clear schema so that the register connection works
13-
\WPGraphQL::clear_schema();
13+
// enable graphql cache maps
14+
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );
15+
16+
\WPGraphQL::clear_schema();
1417
parent::setUp();
1518
}
1619

tests/wpunit/CacheInvalidationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ class CacheInvalidationTest extends \Codeception\TestCase\WPTestCase {
99
protected $collection;
1010

1111
public function setUp(): void {
12+
1213
\WPGraphQL::clear_schema();
1314

15+
// enable graphql cache maps
16+
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );
17+
1418
if ( ! defined( 'GRAPHQL_DEBUG' ) ) {
1519
define( 'GRAPHQL_DEBUG', true );
1620
}

0 commit comments

Comments
 (0)