From 51dbffd39697b794963eb6a9b87374e65a63f0aa Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Oct 2018 21:00:40 +0000 Subject: [PATCH] Cache the AP delivery process --- src/Protocol/ActivityPub/Transmitter.php | 22 ++++++++++++++++++++++ src/Worker/APDelivery.php | 12 +++++++++--- src/Worker/Notifier.php | 3 +++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index bfbd9f9b4..77688233a 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -22,6 +22,7 @@ use Friendica\Model\Profile; use Friendica\Core\Config; use Friendica\Object\Image; use Friendica\Protocol\ActivityPub; +use Friendica\Core\Cache; /** * @brief ActivityPub Transmitter Protocol class @@ -480,6 +481,27 @@ class Transmitter return $type; } + /** + * @brief Creates the activity or fetches it from the cache + * + * @param integer $item_id + * + * @return array with the activity + */ + public static function createCachedActivityFromItem($item_id) + { + $cachekey = "APDelivery:createActivity:".$item_id; + $data = Cache::get($cachekey); + if (!is_null($data)) { + return $data; + } + + $data = ActivityPub\Transmitter::createActivityFromItem($item_id); + + Cache::set($cachekey, $data, CACHE_QUARTER_HOUR); + return $data; + } + /** * @brief Creates an activity array for a given item id * diff --git a/src/Worker/APDelivery.php b/src/Worker/APDelivery.php index 3c2eba8d8..039cdfbb3 100644 --- a/src/Worker/APDelivery.php +++ b/src/Worker/APDelivery.php @@ -11,6 +11,14 @@ use Friendica\Util\HTTPSignature; class APDelivery extends BaseObject { + /** + * @brief Delivers ActivityPub messages + * + * @param string $cmd + * @param integer $item_id + * @param string $inbox + * @param integer $uid + */ public static function execute($cmd, $item_id, $inbox, $uid) { logger('Invoked: ' . $cmd . ': ' . $item_id . ' to ' . $inbox, LOGGER_DEBUG); @@ -23,12 +31,10 @@ class APDelivery extends BaseObject } elseif ($cmd == Delivery::PROFILEUPDATE) { ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox); } else { - $data = ActivityPub\Transmitter::createActivityFromItem($item_id); + $data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id); if (!empty($data)) { HTTPSignature::transmit($data, $inbox, $uid); } } - - return; } } diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 8945a958f..186422b6c 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -433,6 +433,9 @@ class Notifier $inboxes = array_merge($inboxes, $parent_inboxes); } + // Fill the item cache + ActivityPub\Transmitter::createCachedActivityFromItem($item_id); + foreach ($inboxes as $inbox) { logger('Deliver ' . $item_id .' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);