Add break mechanism to query result cache

In case many large queries need to be cached at once.
This commit is contained in:
Tetrakern 2024-08-16 16:56:12 +02:00
parent fe74b811b0
commit 7518ae2bba
3 changed files with 14 additions and 1 deletions

View File

@ -1602,6 +1602,7 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_STORY_CARD_CHAPTER_LIMIT | integer | Maximum number of chapters shown on story cards. Default 3.
| FICTIONEER_QUERY_RESULT_CACHE_THRESHOLD | integer | Count of a query result required to be eligible for caching. Default `75`.
| FICTIONEER_QUERY_RESULT_CACHE_LIMIT | integer | Number of query results cached if the feature is enabled. Default `50`.
| FICTIONEER_QUERY_RESULT_CACHE_BREAK | integer | Limit the number of large query result cache uploads per request. Default `3`.
| FICTIONEER_CACHE_PURGE_ASSIST | boolean | Whether to call the cache purge assist function on post updates. Default `true`.
| FICTIONEER_RELATIONSHIP_PURGE_ASSIST | boolean | Whether to purge related post caches. Default `true`.
| FICTIONEER_CHAPTER_LIST_TRANSIENTS | boolean | Whether to cache chapter lists on story pages as Transients. Default `true`.

View File

@ -313,6 +313,11 @@ if ( ! defined( 'FICTIONEER_QUERY_RESULT_CACHE_LIMIT' ) ) {
define( 'FICTIONEER_QUERY_RESULT_CACHE_LIMIT', 50 );
}
// Integer: Limit the number of large query result cache uploads per request
if ( ! defined( 'FICTIONEER_QUERY_RESULT_CACHE_BREAK' ) ) {
define( 'FICTIONEER_QUERY_RESULT_CACHE_BREAK', 3 );
}
/*
* Booleans
*/

View File

@ -212,6 +212,7 @@ if ( ! function_exists( 'fictioneer_get_last_fiction_update' ) ) {
function fictioneer_get_story_chapter_posts( $story_id, $args = [], $full = false ) {
// Static variable cache
static $cached_results = [];
static $query_count = 0;
// Setup
$chapter_ids = fictioneer_get_story_chapter_ids( $story_id );
@ -297,10 +298,16 @@ function fictioneer_get_story_chapter_posts( $story_id, $args = [], $full = fals
}
}
// Count query
$query_count++;
// Cache for subsequent calls
$cached_results[ $cache_key ] = $chapter_posts;
fictioneer_cache_query_result( $cache_key, $chapter_posts );
// Put on breaks for large queries uploaded to the database
if ( $query_count <= FICTIONEER_QUERY_RESULT_CACHE_BREAK ) {
fictioneer_cache_query_result( $cache_key, $chapter_posts );
}
// Return chapters selected in story
return $chapter_posts;