From 63236585dbea905eaaa3105a22ca62bc7e1e12be Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Mon, 16 Oct 2023 11:05:34 +0200 Subject: [PATCH] Cache queries in transients --- ft-network-sourcelinks.php | 54 +++++++++++++++---- inc/Network/Post_Types/Post_Type__ft_link.php | 6 ++- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/ft-network-sourcelinks.php b/ft-network-sourcelinks.php index 31c51d4..d3ded57 100644 --- a/ft-network-sourcelinks.php +++ b/ft-network-sourcelinks.php @@ -32,6 +32,9 @@ * @package Ft_Network_Sourcelinks */ + const TRANSIENT_KEY = 'ft_ns_urls'; + + /** * This class handles all the major use-cases for external URLs in WordPress * @@ -224,18 +227,11 @@ public function enable__on_admin() : void { $this->debug(); }*/ - - - public static function get_urls() : Array - { - // minimal caching - if( isset( self::$urls ) && !empty( self::$urls ) ) - return self::$urls; - + public static function query_urls() : Array { $ft_query = \Figuren_Theater\FT_Query::init(); // - return self::$urls = $ft_query->find_many_by_type( + return $ft_query->find_many_by_type( Post_Types\Post_Type__ft_link::NAME, 'publish', [ @@ -252,9 +248,49 @@ public static function get_urls() : Array 'update_post_term_cache' => false, // 'suppress_filters' => true, + 'no_found_rows' => true, // Useful when pagination is not needed. + 'posts_per_page' => 25, ] ); + } + + /** + * Get stored labels from the database. + * + * Uses transients to cache simplified WP_Query results. + * + * @return Array An array of Label objects. + */ + public static function get_stored_urls() : array { + + // Check if the value is already stored. + $stored_urls = \get_transient( TRANSIENT_KEY ); + + if ( empty( $stored_urls ) ) { + + // Retrieve posts from DB. + $stored_urls = static::query_urls(); + + // Store for long, + // because this will beflushed with every new (and updated) 'wp_block' post. + \set_transient( + TRANSIENT_KEY, + $stored_urls, + \WEEK_IN_SECONDS + ); + } + + return $stored_urls; + } + + + public static function get_urls() : Array + { + // minimal caching + if( isset( self::$urls ) && !empty( self::$urls ) ) + return self::$urls; + return self::$urls = static::get_stored_urls(); } diff --git a/inc/Network/Post_Types/Post_Type__ft_link.php b/inc/Network/Post_Types/Post_Type__ft_link.php index 785ff74..5c2e2d0 100644 --- a/inc/Network/Post_Types/Post_Type__ft_link.php +++ b/inc/Network/Post_Types/Post_Type__ft_link.php @@ -6,6 +6,7 @@ use Figuren_Theater\inc\EventManager; use Figuren_Theater\Network\Taxonomies; use Figuren_Theater\Network\Users; +use Figuren_Theater\Network\Sources; use WP_Post; @@ -444,10 +445,13 @@ function ( string $service_url ) use ( $url_short ) { return false; } */ - + // Create a simple function to delete our transient public static function delete_transient() { + // Has all our links with 'OWN' taxonomy-term. \delete_transient( 'ft_link_own_q' ); + // Has all our links. + \delete_transient( Sources\TRANSIENT_KEY ); }