Story data cache purge improvements

Probably.
This commit is contained in:
Tetrakern 2023-08-14 20:55:59 +02:00
parent 9357f5e3c3
commit 0dde699200
4 changed files with 25 additions and 9 deletions

View File

@ -786,3 +786,4 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_API_STORYGRAPH_TRANSIENTS | boolean | Whether to cache Storygraph responses as Transients. Default `true`.
| FICTIONEER_DISABLE_ACF_JSON_IMPORT | boolean | Whether to disable the ACF JSON field import. Dangerous. Default `false`.
| FICTIONEER_ENABLE_STICKY_CARDS | boolean | Whether to allow sticky cards. Expensive. Default `true`.
| FICTIONEER_ENABLE_STORY_DATA_META_CACHE | boolean | Whether to "cache" story data in a meta field. Default `true`.

View File

@ -299,6 +299,11 @@ if ( ! defined( 'FICTIONEER_ENABLE_STICKY_CARDS' ) ) {
define( 'FICTIONEER_ENABLE_STICKY_CARDS', true );
}
// Boolean: Enable story data meta cache
if ( ! defined( 'FICTIONEER_ENABLE_STORY_DATA_META_CACHE' ) ) {
define( 'FICTIONEER_ENABLE_STORY_DATA_META_CACHE', true );
}
// =============================================================================
// GLOBAL
// =============================================================================

View File

@ -240,9 +240,14 @@ if ( ! function_exists( 'fictioneer_refresh_post_caches' ) ) {
}
// Purge parent story (if any)
fictioneer_purge_post_cache(
fictioneer_get_field( 'fictioneer_chapter_story', $post_id )
);
if ( get_post_type( $post_id ) == 'fcn_chapter' ) {
$story_id = fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
if ( ! empty( $story_id ) ) {
update_post_meta( $story_id, 'fictioneer_story_data_collection', false );
fictioneer_purge_post_cache( $story_id );
}
}
// Purge associated list pages
if ( in_array( get_post_type( $post_id ), ['fcn_chapter', 'fcn_story'] ) ) {
@ -349,11 +354,11 @@ function fictioneer_toggle_refresh_hooks( $add = true ) {
if ( $add ) {
foreach( $hooks as $hook ) {
add_action( $hook, 'fictioneer_refresh_post_caches' );
add_action( $hook, 'fictioneer_refresh_post_caches', 20 );
}
} else {
foreach( $hooks as $hook ) {
remove_action( $hook, 'fictioneer_refresh_post_caches' );
remove_action( $hook, 'fictioneer_refresh_post_caches', 20 );
}
}
}
@ -543,11 +548,11 @@ function fictioneer_toggle_update_tracker_hooks( $add = true ) {
if ( $add ) {
foreach( $hooks as $hook ) {
add_action( $hook, 'fictioneer_track_chapter_and_story_updates' );
add_action( $hook, 'fictioneer_track_chapter_and_story_updates', 10 );
}
} else {
foreach( $hooks as $hook ) {
remove_action( $hook, 'fictioneer_track_chapter_and_story_updates' );
remove_action( $hook, 'fictioneer_track_chapter_and_story_updates', 10 );
}
}
}

View File

@ -150,11 +150,16 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
function fictioneer_get_story_data( $story_id, $refresh_comment_count = true, $args = [] ) {
$story_id = fictioneer_validate_id( $story_id, 'fcn_story' );
$old_data = false;
if ( ! $story_id ) return false;
if ( empty( $story_id ) ) {
return false;
}
// Check cache
$old_data = get_post_meta( $story_id, 'fictioneer_story_data_collection', true );
if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
$old_data = get_post_meta( $story_id, 'fictioneer_story_data_collection', true );
}
if ( ! empty( $old_data ) && $old_data['last_modified'] >= get_the_modified_time( 'U', $story_id ) ) {
// Return cached data without refreshing the comment count