Merge pull request #11526 from annando/ap-endpoint-cache

Prolonged cache lifetime for endpoints
This commit is contained in:
Tobias Diekershoff 2022-05-17 17:43:20 +02:00 committed by GitHub
commit 6eb70bea16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 19 deletions

View File

@ -35,6 +35,7 @@ use Friendica\Core\Worker;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Module\NoScrape;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
@ -797,11 +798,13 @@ class Contact
public static function remove($id) public static function remove($id)
{ {
// We want just to make sure that we don't delete our "self" contact // We want just to make sure that we don't delete our "self" contact
$contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro'], ['id' => $id, 'self' => false]); $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro', 'uid'], ['id' => $id, 'self' => false]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return; return;
} }
self::clearFollowerFollowingEndpointCache($contact['uid']);
// Archive the contact // Archive the contact
self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]);
@ -899,6 +902,16 @@ class Contact
self::remove($contact['id']); self::remove($contact['id']);
} }
private static function clearFollowerFollowingEndpointCache(int $uid)
{
if (empty($uid)) {
return;
}
DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'followers:' . $uid);
DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_CONTACTS . 'following:' . $uid);
DI::cache()->delete(NoScrape::CACHEKEY . $uid);
}
/** /**
* Marks a contact for archival after a communication issue delay * Marks a contact for archival after a communication issue delay
@ -1764,7 +1777,7 @@ class Contact
break; break;
default: default:
/** /**
* Use a random picture. * Use a random picture.
* The service provides random pictures from Unsplash. * The service provides random pictures from Unsplash.
* @license https://unsplash.com/license * @license https://unsplash.com/license
*/ */
@ -2321,7 +2334,7 @@ class Contact
Worker::add(PRIORITY_LOW, 'FetchFeaturedPosts', $ret['url']); Worker::add(PRIORITY_LOW, 'FetchFeaturedPosts', $ret['url']);
} }
} }
$ret['last-item'] = Probe::getLastUpdate($ret); $ret['last-item'] = Probe::getLastUpdate($ret);
Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]); Logger::info('Fetched last item', ['id' => $id, 'probed_url' => $ret['url'], 'last-item' => $ret['last-item'], 'callstack' => System::callstack(20)]);
} }
@ -2707,6 +2720,8 @@ class Contact
$contact = DBA::selectFirst('contact', [], ['id' => $cid]); $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
} }
self::clearFollowerFollowingEndpointCache($importer['uid']);
if (!empty($contact)) { if (!empty($contact)) {
if (!empty($contact['pending'])) { if (!empty($contact['pending'])) {
Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]); Logger::info('Pending contact request already exists.', ['url' => $url, 'uid' => $importer['uid']]);
@ -2830,6 +2845,8 @@ class Contact
return; return;
} }
self::clearFollowerFollowingEndpointCache($contact['uid']);
$cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']);
DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]); DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]);
@ -2844,6 +2861,8 @@ class Contact
*/ */
public static function removeSharer(array $contact) public static function removeSharer(array $contact)
{ {
self::clearFollowerFollowingEndpointCache($contact['uid']);
if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) { if ($contact['rel'] == self::SHARING || in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
self::remove($contact['id']); self::remove($contact['id']);
} else { } else {

View File

@ -1223,6 +1223,10 @@ class Item
self::updateDisplayCache($posted_item['uri-id']); self::updateDisplayCache($posted_item['uri-id']);
} }
if ($posted_item['origin'] && ($posted_item['uid'] != 0) && in_array($posted_item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_OUTBOX . $posted_item['uid']);
}
return $post_user_id; return $post_user_id;
} }
@ -2789,7 +2793,7 @@ class Item
$shared_item = Post::selectFirst(['uri-id', 'plink', 'has-media'], ['guid' => $shared['guid']]); $shared_item = Post::selectFirst(['uri-id', 'plink', 'has-media'], ['guid' => $shared['guid']]);
$shared_uri_id = $shared_item['uri-id'] ?? 0; $shared_uri_id = $shared_item['uri-id'] ?? 0;
$shared_links = [strtolower($shared_item['plink'] ?? '')]; $shared_links = [strtolower($shared_item['plink'] ?? '')];
$shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared['guid'], [], $shared_item['has-media']); $shared_attachments = Post\Media::splitAttachments($shared_uri_id, $shared['guid'], [], $shared_item['has-media'] ?? false);
$shared_links = array_merge($shared_links, array_column($shared_attachments['visual'], 'url')); $shared_links = array_merge($shared_links, array_column($shared_attachments['visual'], 'url'));
$shared_links = array_merge($shared_links, array_column($shared_attachments['link'], 'url')); $shared_links = array_merge($shared_links, array_column($shared_attachments['link'], 'url'));
$shared_links = array_merge($shared_links, array_column($shared_attachments['additional'], 'url')); $shared_links = array_merge($shared_links, array_column($shared_attachments['additional'], 'url'));

View File

@ -24,6 +24,8 @@ namespace Friendica\Model\Post;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use BadMethodCallException; use BadMethodCallException;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Protocol\ActivityPub;
class Collection class Collection
{ {
@ -34,14 +36,19 @@ class Collection
* *
* @param integer $uri_id * @param integer $uri_id
* @param integer $type * @param integer $type
* @param integer $cache_uid If set to a non zero value, the featured cache is cleared
*/ */
public static function add(int $uri_id, int $type) public static function add(int $uri_id, int $type, int $cache_uid = 0)
{ {
if (empty($uri_id)) { if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id'); throw new BadMethodCallException('Empty URI_id');
} }
DBA::insert('post-collection', ['uri-id' => $uri_id, 'type' => $type], Database::INSERT_IGNORE); DBA::insert('post-collection', ['uri-id' => $uri_id, 'type' => $type], Database::INSERT_IGNORE);
if (!empty($cache_uid) && ($type == self::FEATURED)) {
DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_FEATURED . $cache_uid);
}
} }
/** /**
@ -49,14 +56,19 @@ class Collection
* *
* @param integer $uri_id * @param integer $uri_id
* @param integer $type * @param integer $type
* @param integer $cache_uid If set to a non zero value, the featured cache is cleared
*/ */
public static function remove(int $uri_id, int $type) public static function remove(int $uri_id, int $type, int $cache_uid = 0)
{ {
if (empty($uri_id)) { if (empty($uri_id)) {
throw new BadMethodCallException('Empty URI_id'); throw new BadMethodCallException('Empty URI_id');
} }
DBA::delete('post-collection', ['uri-id' => $uri_id, 'type' => $type]); DBA::delete('post-collection', ['uri-id' => $uri_id, 'type' => $type]);
if (!empty($cache_uid) && ($type == self::FEATURED)) {
DI::cache()->delete(ActivityPub\Transmitter::CACHEKEY_FEATURED . $cache_uid);
}
} }
/** /**

View File

@ -46,7 +46,7 @@ class Pin extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED); Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED, $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray()); System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
} }

View File

@ -46,7 +46,7 @@ class Unpin extends BaseApi
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->RecordNotFound();
} }
Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED); Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED, $uid);
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray()); System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)->toArray());
} }

View File

@ -60,9 +60,9 @@ class Pin extends BaseModule
$pinned = !$item['featured']; $pinned = !$item['featured'];
if ($pinned) { if ($pinned) {
Post\Collection::add($item['uri-id'], Post\Collection::FEATURED); Post\Collection::add($item['uri-id'], Post\Collection::FEATURED, local_user());
} else { } else {
Post\Collection::remove($item['uri-id'], Post\Collection::FEATURED); Post\Collection::remove($item['uri-id'], Post\Collection::FEATURED, local_user());
} }
// See if we've been passed a return path to redirect to // See if we've been passed a return path to redirect to

View File

@ -22,6 +22,7 @@
namespace Friendica\Module; namespace Friendica\Module;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -35,6 +36,8 @@ use Friendica\Model\User;
*/ */
class NoScrape extends BaseModule class NoScrape extends BaseModule
{ {
const CACHEKEY = 'noscrape:';
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$a = DI::app(); $a = DI::app();
@ -55,6 +58,12 @@ class NoScrape extends BaseModule
System::jsonError(404, 'Profile not found'); System::jsonError(404, 'Profile not found');
} }
$cachekey = self::CACHEKEY . $owner['uid'];
$result = DI::cache()->get($cachekey);
if (!is_null($result)) {
System::jsonExit($result);
}
$json_info = [ $json_info = [
'addr' => $owner['addr'], 'addr' => $owner['addr'],
'nick' => $which, 'nick' => $which,
@ -126,6 +135,8 @@ class NoScrape extends BaseModule
} }
} }
DI::cache()->set($cachekey, $json_info, Duration::DAY);
System::jsonExit($json_info); System::jsonExit($json_info);
} }
} }

View File

@ -44,7 +44,6 @@ use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Relay; use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;
use Friendica\Util\JsonLD;
use Friendica\Util\LDSignature; use Friendica\Util\LDSignature;
use Friendica\Util\Map; use Friendica\Util\Map;
use Friendica\Util\Network; use Friendica\Util\Network;
@ -59,6 +58,10 @@ use Friendica\Util\XML;
*/ */
class Transmitter class Transmitter
{ {
const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
const CACHEKEY_OUTBOX = 'transmitter:getOutbox:';
/** /**
* Add relay servers to the list of inboxes * Add relay servers to the list of inboxes
* *
@ -159,7 +162,7 @@ class Transmitter
public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, $nocache = false) public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null, $nocache = false)
{ {
if (empty($page)) { if (empty($page)) {
$cachekey = 'transmitter:getContacts:' . $module . ':'. $owner['uid']; $cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
$result = DI::cache()->get($cachekey); $result = DI::cache()->get($cachekey);
if (!$nocache && !is_null($result)) { if (!$nocache && !is_null($result)) {
return $result; return $result;
@ -202,7 +205,7 @@ class Transmitter
if (!$show_contacts) { if (!$show_contacts) {
if (!empty($cachekey)) { if (!empty($cachekey)) {
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); DI::cache()->set($cachekey, $data, Duration::DAY);
} }
return $data; return $data;
@ -230,7 +233,7 @@ class Transmitter
} }
if (!empty($cachekey)) { if (!empty($cachekey)) {
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); DI::cache()->set($cachekey, $data, Duration::DAY);
} }
return $data; return $data;
@ -251,7 +254,7 @@ class Transmitter
public static function getOutbox(array $owner, int $page = null, string $requester = '', $nocache = false) public static function getOutbox(array $owner, int $page = null, string $requester = '', $nocache = false)
{ {
if (empty($page)) { if (empty($page)) {
$cachekey = 'transmitter:getOutbox:' . $owner['uid']; $cachekey = self::CACHEKEY_OUTBOX . $owner['uid'];
$result = DI::cache()->get($cachekey); $result = DI::cache()->get($cachekey);
if (!$nocache && !is_null($result)) { if (!$nocache && !is_null($result)) {
return $result; return $result;
@ -320,7 +323,7 @@ class Transmitter
} }
if (!empty($cachekey)) { if (!empty($cachekey)) {
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); DI::cache()->set($cachekey, $data, Duration::DAY);
} }
return $data; return $data;
@ -339,15 +342,16 @@ class Transmitter
*/ */
public static function getFeatured(array $owner, int $page = null, $nocache = false) public static function getFeatured(array $owner, int $page = null, $nocache = false)
{ {
$owner_cid = Contact::getIdForURL($owner['url'], 0, false);
if (empty($page)) { if (empty($page)) {
$cachekey = 'transmitter:getFeatured:' . $owner_cid; $cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
$result = DI::cache()->get($cachekey); $result = DI::cache()->get($cachekey);
if (!$nocache && !is_null($result)) { if (!$nocache && !is_null($result)) {
return $result; return $result;
} }
} }
$owner_cid = Contact::getIdForURL($owner['url'], 0, false);
$condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)", $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
$owner_cid, Post\Collection::FEATURED]; $owner_cid, Post\Collection::FEATURED];
@ -403,7 +407,7 @@ class Transmitter
$data['orderedItems'] = $list; $data['orderedItems'] = $list;
if (!empty($cachekey)) { if (!empty($cachekey)) {
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); DI::cache()->set($cachekey, $data, Duration::DAY);
} }
return $data; return $data;