PHP code example of matepaiva / wp-graphql-cache

1. Go to this page and download the library: Download matepaiva/wp-graphql-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

matepaiva / wp-graphql-cache example snippets


use WPGraphQL\Extensions\Cache\CacheManager;

CacheManager::register_graphql_query_cache([
    'query_name' => '*',
    'expire' => 120, // sec
]);

use WPGraphQL\Extensions\Cache\CacheManager;

CacheManager::register_graphql_query_cache([
    'query_name' => 'MySlowQuery',
    'expire' => 120,
]);

use WPGraphQL\Extensions\Cache\CacheManager;

CacheManager::register_graphql_field_cache([
    'query_name' => 'MyBigQuery',
    'field_name' => 'menuItems',
    'expire' => 120, // sec
]);

/**
 * Register cache to a 'menus' zone
 */
CacheManager::register_graphql_field_cache([
    'zone' => 'menus', // 👈
    'query_name' => 'MyBigQuery',
    'field_name' => 'menuItems',
    'expire' => 120, // sec
]);

/**
 * Clear the zone 'menus' when the menus are updated
 */
add_action('wp_update_nav_menu', function () {
    CacheManager::clear_zone('menus');
});

use WPGraphQL\Extensions\Cache\Backend\FileSystem;

add_filter('graphql_cache_backend', function () {
    return new FileSystem('/custom/path');
});