Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from markkelnar/refactor/save-urls
Browse files Browse the repository at this point in the history
Use action from cache plugin to save urls to collection
  • Loading branch information
jasonbahl authored Jul 28, 2022
2 parents c91e487 + 4daf201 commit 65d3a04
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 29 deletions.
56 changes: 28 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion wpe-graphql-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use WPGraphQL\SmartCache\Cache\Collection;
use WPGraphQL\SmartCache\Admin\Settings;
use GraphQLRelay\Relay;

const MAGIC_STRING = 'wpe-graphql:';

Expand Down Expand Up @@ -110,3 +109,47 @@ function log( $msg ) {
log( 'WpeGraphql Trigger Varnish Purge - After '. $id );
}
}, 10, 2);


add_action( 'wpgraphql_cache_save_request', function( $request_key ) {
// Only store mappings of urls when it's a GET request
$map_the_url = false;
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) {
$map_the_url = true;
}

// We don't want POSTs during mutations or nothing on the url. cause it'll purge /graphql*
if ( $map_the_url && ! empty( $_SERVER['REQUEST_URI'] ) ) {
$url_to_save = wp_unslash( $_SERVER['REQUEST_URI'] );

// Save the url this query request came in on, so we can purge it later when something changes
$collection = new Collection();
$urls = $collection->store_content( wpe_cache_url_key( $request_key ), $url_to_save );

log( "Graphql Save Urls: $request_key " . print_r( $urls, 1 ) );
}
}, 10, 1 );

/**
* When save or retrieve urls for a specific Unique identifier for this request for use in the collection map
*
* @param string $id Id for the node
*
* @return string unique id for this request
*/
function wpe_cache_url_key( $id ) {
return 'url:' . $id;
}

/**
* Get the list of urls associated with the content/node/list id
*
* @param mixed|string|int $id The content node identifier
*
* @return array The unique list of content stored
*/
function wpe_cache_retrieve_urls( $id ) {
$key = wpe_cache_url_key( $id );
$collection = new Collection();
return $collection->get( $key );
}

0 comments on commit 65d3a04

Please sign in to comment.