Skip to content

Commit

Permalink
Implement object cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iansvo committed Dec 23, 2024
1 parent e600dcf commit eb19e5c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
31 changes: 29 additions & 2 deletions includes/dynamic-tags/class-dynamic-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,32 @@ public function register_rest_routes() {
* @return WP_REST_Response
*/
public function get_dynamic_tag_replacements( $request ) {
$content = urldecode( $request->get_param( 'content' ) );
$content = $request->get_param( 'content' );
$context = $request->get_param( 'context' );
$fallback_id = $context['postId'] ?? 0;
$client_id = $request->get_param( 'clientId' );
$post_id = $context['postId'] ?? 0;
$fallback_id = $post_id;
$instance = new stdClass();
$replacements = [];

// Set up an instance object with a context key.
$instance->context = $context;

// Create a unique cache key.
$cache_key = sprintf(
'replacements_%s_%s_%s',
md5( $content ),
$client_id,
$post_id
);

$replacements = wp_cache_get( $cache_key, 'generate_blocks_dynamic_tags' );

// Return the cache here if present.
if ( false !== $replacements ) {
return rest_ensure_response( $replacements );
}

$all_tags = GenerateBlocks_Register_Dynamic_Tag::get_tags();
$tags_list = [];

Expand Down Expand Up @@ -567,6 +584,16 @@ public function get_dynamic_tag_replacements( $request ) {
}
}

// Set the cache with filterable duration.
/**
* Set the duration of the cache for dynamic tag replacements.
*
* @since 2.0.0
*/
$cache_duration = apply_filters( 'generateblocks_dynamic_tags_replacement_cache_duration', 3600, $content, $context, $request );

wp_cache_set( $cache_key, $replacements, 'generateblocks_dynamic_tags', $cache_duration );

return rest_ensure_response( $replacements );
}

Expand Down
10 changes: 5 additions & 5 deletions src/dynamic-tags/components/DynamicTagBlockToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export function DynamicTagBlockToolbar( {
return contentValue.substring( selectionStart.offset, selectionEnd.offset );
}, [ selectionStart, selectionEnd, value ] );

useEffect( () => {
if ( foundTags.length && ! isSelected ) {
setContentMode( 'preview' );
}
}, [ foundTags.length, isSelected ] );
// useEffect( () => {
// if ( foundTags.length && ! isSelected ) {
// setContentMode( 'preview' );
// }
// }, [ foundTags.length, isSelected ] );

return (
<BlockControls>
Expand Down
5 changes: 3 additions & 2 deletions src/dynamic-tags/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import apiFetch from '@wordpress/api-fetch';
import { applyFilters } from '@wordpress/hooks';

export async function replaceTags( content, context = {} ) {
export async function replaceTags( { content, context = {}, clientId } ) {
// Define an async function to fetch data
try {
const response = await apiFetch( {
path: '/generateblocks/v1/dynamic-tag-replacements',
method: 'POST',
data: {
content,
context: applyFilters( 'generateblocks.editor.preview.context', context, { content } ),
context: applyFilters( 'generateblocks.editor.preview.context', context, { content, clientId } ),
clientId,
},
} );

Expand Down
9 changes: 2 additions & 7 deletions src/editor/style-html-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const cache = {};
function getCacheKey( clientId, context ) {
const {
'generateblocks/loopIndex': loopIndex,
'generateblocks/loopPreviewId': previewId,
postId,
} = context;

Expand All @@ -16,9 +15,7 @@ function getCacheKey( clientId, context ) {
key += `${ loopIndex }_`;
}

if ( previewId ) {
key += `${ previewId }_`;
} else if ( postId ) {
if ( postId ) {
key += `${ postId }_`;
}

Expand Down Expand Up @@ -47,19 +44,17 @@ addFilter(

// Get the cached result if available.
if ( cache[ clientId ][ style ] ) {
console.log( 'Using cached data', cache[ clientId ][ style ] );
return cache[ style ];
}

const replacements = await replaceTags( style, context );
const replacements = await replaceTags( { content: style, context, clientId } );

if ( ! replacements.length ) {
return style;
}

// Cache the result.
cache[ clientId ][ style ] = replacements;
console.log( 'Cache miss, setting data', cache[ clientId ][ style ] );

const withReplacements = replacements.reduce( ( acc, { original, replacement, fallback } ) => {
if ( ! replacement ) {
Expand Down
32 changes: 16 additions & 16 deletions src/hoc/withDynamicTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from '@wordpress/element';
import { useState, useEffect, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { replaceTags } from '../dynamic-tags/utils';

Expand All @@ -7,7 +7,6 @@ const cache = {};
function getCacheKey( clientId, context ) {
const {
'generateblocks/loopIndex': loopIndex,
'generateblocks/loopPreviewId': previewId,
postId,
} = context;

Expand All @@ -17,9 +16,7 @@ function getCacheKey( clientId, context ) {
key += `${ loopIndex }_`;
}

if ( previewId ) {
key += `${ previewId }_`;
} else if ( postId ) {
if ( postId ) {
key += `${ postId }_`;
}

Expand All @@ -34,6 +31,7 @@ export function withDynamicTag( WrappedComponent ) {
context,
attributes,
clientId,
isSelected,
} = props;

const {
Expand All @@ -43,15 +41,15 @@ export function withDynamicTag( WrappedComponent ) {
} = attributes;

const [ dynamicTagValue, setDynamicTagValue ] = useState( '' );
const [ contentMode, setContentMode ] = useState( 'edit' );
const [ contentMode, setContentMode ] = useState( 'preview' );
const isSavingPost = useSelect( ( select ) => select( 'core/editor' ).isSavingPost() );
const blockCacheKey = getCacheKey( clientId, context );

if ( ! cache[ blockCacheKey ] ) {
cache[ blockCacheKey ] = {};
}

const getContentValue = () => {
const contentValue = useMemo( () => {
if ( 'img' === tagName ) {
return htmlAttributes?.src;
}
Expand All @@ -61,8 +59,7 @@ export function withDynamicTag( WrappedComponent ) {
}

return content?.text ?? content;
};
const contentValue = getContentValue();
}, [ tagName, htmlAttributes?.src, content ] );

useEffect( () => {
if ( ! contentValue || ! contentValue.includes( '{{' ) ) {
Expand All @@ -75,18 +72,13 @@ export function withDynamicTag( WrappedComponent ) {
return;
}

console.log( contentMode );

if ( cache[ blockCacheKey ][ contentValue ] ) {
console.log( 'Using cached data', cache[ blockCacheKey ][ contentValue ] );
setDynamicTagValue( cache[ blockCacheKey ][ contentValue ] );
return;
}

async function fetchData() {
const response = await replaceTags( contentValue, context );

console.log( 'cache miss, setting cache', cache[ blockCacheKey ][ contentValue ] );
const response = await replaceTags( { content: contentValue, context, clientId } );

setDynamicTagValue( response );

Expand All @@ -95,7 +87,15 @@ export function withDynamicTag( WrappedComponent ) {
}

fetchData();
}, [ contentValue, contentMode, context, tagName, isSavingPost, blockCacheKey ] );
}, [
contentValue,
contentMode,
context,
tagName,
isSavingPost,
blockCacheKey,
isSelected,
] );

return (
<WrappedComponent
Expand Down

0 comments on commit eb19e5c

Please sign in to comment.