From 4abd5aefb45e651af0f31b328f47817de46999e8 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 9 May 2023 10:28:23 +0200 Subject: [PATCH 1/2] cache inbox list --- includes/collection/class-followers.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index c6c6223..a6a8ba4 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -19,6 +19,7 @@ use function Activitypub\get_remote_metadata_by_actor; */ class Followers { const TAXONOMY = 'activitypub-followers'; + const CACHE_KEY_INBOXES = 'activitypub_follower_inboxes_for_%s'; /** * Register WordPress hooks/actions and register Taxonomy @@ -225,6 +226,8 @@ class Followers { if ( is_wp_error( $result ) ) { return $result; } else { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + wp_cache_delete( $cache_key ); return $follower; } } @@ -238,6 +241,8 @@ class Followers { * @return bool|WP_Error True on success, false or WP_Error on failure. */ public static function remove_follower( $user_id, $actor ) { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + wp_cache_delete( $cache_key ); return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY ); } @@ -369,6 +374,13 @@ class Followers { * @return array The list of Inboxes */ public static function get_inboxes( $user_id ) { + $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); + $inboxes = wp_cache_get( $cache_key ); + + if ( $inboxes ) { + return $inboxes; + } + // get all Followers of a ID of the WordPress User $terms = new WP_Term_Query( array( @@ -402,6 +414,9 @@ class Followers { ) ); - return array_filter( $results ); + $inboxes = array_filter( $results ); + wp_cache_set( $cache_key, $inboxes ); + + return $inboxes; } } From 74be5d6b5159bed33b67e377a7fbf6383d66fee1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 10 May 2023 09:04:33 +0200 Subject: [PATCH 2/2] implemented feedback of @akirk --- includes/collection/class-followers.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index a6a8ba4..5e5e532 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -19,7 +19,7 @@ use function Activitypub\get_remote_metadata_by_actor; */ class Followers { const TAXONOMY = 'activitypub-followers'; - const CACHE_KEY_INBOXES = 'activitypub_follower_inboxes_for_%s'; + const CACHE_KEY_INBOXES = 'follower_inboxes_%s'; /** * Register WordPress hooks/actions and register Taxonomy @@ -226,8 +226,7 @@ class Followers { if ( is_wp_error( $result ) ) { return $result; } else { - $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - wp_cache_delete( $cache_key ); + wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); return $follower; } } @@ -241,8 +240,7 @@ class Followers { * @return bool|WP_Error True on success, false or WP_Error on failure. */ public static function remove_follower( $user_id, $actor ) { - $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - wp_cache_delete( $cache_key ); + wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY ); } @@ -375,7 +373,7 @@ class Followers { */ public static function get_inboxes( $user_id ) { $cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id ); - $inboxes = wp_cache_get( $cache_key ); + $inboxes = wp_cache_get( $cache_key, 'activitypub' ); if ( $inboxes ) { return $inboxes; @@ -415,7 +413,7 @@ class Followers { ); $inboxes = array_filter( $results ); - wp_cache_set( $cache_key, $inboxes ); + wp_cache_set( $cache_key, $inboxes, 'activitypub' ); return $inboxes; }