Merge pull request #324 from Automattic/add/caching

Introduce Caching
This commit is contained in:
Matthias Pfefferle 2023-05-22 14:34:49 +02:00 committed by GitHub
commit b8ee030d78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,6 +19,7 @@ use function Activitypub\get_remote_metadata_by_actor;
*/ */
class Followers { class Followers {
const TAXONOMY = 'activitypub-followers'; const TAXONOMY = 'activitypub-followers';
const CACHE_KEY_INBOXES = 'follower_inboxes_%s';
/** /**
* Register WordPress hooks/actions and register Taxonomy * Register WordPress hooks/actions and register Taxonomy
@ -225,6 +226,7 @@ class Followers {
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
return $result; return $result;
} else { } else {
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
return $follower; return $follower;
} }
} }
@ -238,6 +240,7 @@ class Followers {
* @return bool|WP_Error True on success, false or WP_Error on failure. * @return bool|WP_Error True on success, false or WP_Error on failure.
*/ */
public static function remove_follower( $user_id, $actor ) { public static function remove_follower( $user_id, $actor ) {
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY ); return wp_remove_object_terms( $user_id, $actor, self::TAXONOMY );
} }
@ -376,6 +379,13 @@ class Followers {
* @return array The list of Inboxes * @return array The list of Inboxes
*/ */
public static function get_inboxes( $user_id ) { public static function get_inboxes( $user_id ) {
$cache_key = sprintf( self::CACHE_KEY_INBOXES, $user_id );
$inboxes = wp_cache_get( $cache_key, 'activitypub' );
if ( $inboxes ) {
return $inboxes;
}
// get all Followers of a ID of the WordPress User // get all Followers of a ID of the WordPress User
$terms = new WP_Term_Query( $terms = new WP_Term_Query(
array( array(
@ -409,7 +419,10 @@ class Followers {
) )
); );
return array_filter( $results ); $inboxes = array_filter( $results );
wp_cache_set( $cache_key, $inboxes, 'activitypub' );
return $inboxes;
} }
/** /**