Skip to content

Commit

Permalink
Cache queries in transients
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 16, 2023
1 parent e0f67c2 commit 6323658
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
54 changes: 45 additions & 9 deletions ft-network-sourcelinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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',
[
Expand All @@ -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();
}


Expand Down
6 changes: 5 additions & 1 deletion inc/Network/Post_Types/Post_Type__ft_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
}


Expand Down

0 comments on commit 6323658

Please sign in to comment.