Merge pull request #11892 from annando/pagecache

Pagecache for frequently fetched pages
This commit is contained in:
Hypolite Petovan 2022-09-07 22:25:19 -04:00 committed by GitHub
commit 7da43fe28a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 29 deletions

View file

@ -3321,7 +3321,7 @@ class Contact
continue; continue;
} }
$contact = self::getByURL($url, false, ['id', 'updated']); $contact = self::getByURL($url, false, ['id', 'updated']);
if (empty($contact['id'])) { if (empty($contact['id']) && Network::isValidHttpUrl($url)) {
Worker::add(PRIORITY_LOW, 'AddContact', 0, $url); Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
++$added; ++$added;
} elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) { } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {

View file

@ -21,8 +21,10 @@
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\Core\Logger;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI;
class ItemURI class ItemURI
{ {
@ -35,14 +37,16 @@ class ItemURI
*/ */
public static function insert(array $fields) public static function insert(array $fields)
{ {
$fields = DI::dbaDefinition()->truncateFieldsForTable('item-uri', $fields);
if (!DBA::exists('item-uri', ['uri' => $fields['uri']])) { if (!DBA::exists('item-uri', ['uri' => $fields['uri']])) {
DBA::insert('item-uri', $fields, Database::INSERT_UPDATE); DBA::insert('item-uri', $fields, Database::INSERT_IGNORE);
} }
$itemuri = DBA::selectFirst('item-uri', ['id', 'guid'], ['uri' => $fields['uri']]); $itemuri = DBA::selectFirst('item-uri', ['id', 'guid'], ['uri' => $fields['uri']]);
if (!DBA::isResult($itemuri)) { if (!DBA::isResult($itemuri)) {
// This shouldn't happen // This shouldn't happen
Logger::warning('Item-uri not found', $fields);
return null; return null;
} }
@ -77,22 +81,4 @@ class ItemURI
return $itemuri['id'] ?? 0; return $itemuri['id'] ?? 0;
} }
/**
* Searched for an id of a given guid.
*
* @param string $guid
* @return integer item-uri id
* @throws \Exception
*/
public static function getIdByGUID(string $guid): int
{
$itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $guid]);
if (!DBA::isResult($itemuri)) {
return 0;
}
return $itemuri['id'];
}
} }

View file

@ -98,7 +98,7 @@ class Objects extends BaseModule
Network::checkEtagModified($etag, $last_modified); Network::checkEtagModified($etag, $last_modified);
if (empty($this->parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) { if (empty($this->parameters['activity']) && ($item['gravity'] != GRAVITY_ACTIVITY)) {
$activity = ActivityPub\Transmitter::createActivityFromItem($item['id'], true); $activity = ActivityPub\Transmitter::createCachedActivityFromItem($item['id'], false, true);
if (empty($activity['type'])) { if (empty($activity['type'])) {
throw new HTTPException\NotFoundException(); throw new HTTPException\NotFoundException();
} }
@ -115,7 +115,7 @@ class Objects extends BaseModule
} elseif (empty($this->parameters['activity']) || in_array($this->parameters['activity'], } elseif (empty($this->parameters['activity']) || in_array($this->parameters['activity'],
['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject', ['Create', 'Announce', 'Update', 'Like', 'Dislike', 'Accept', 'Reject',
'TentativeAccept', 'Follow', 'Add'])) { 'TentativeAccept', 'Follow', 'Add'])) {
$data = ActivityPub\Transmitter::createActivityFromItem($item['id']); $data = ActivityPub\Transmitter::createCachedActivityFromItem($item['id']);
if (empty($data)) { if (empty($data)) {
throw new HTTPException\NotFoundException(); throw new HTTPException\NotFoundException();
} }

View file

@ -38,6 +38,7 @@ use Friendica\Model\Verb;
use Friendica\Module\BaseSettings; use Friendica\Module\BaseSettings;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\Network;
use Friendica\Util\Temporal; use Friendica\Util\Temporal;
use Friendica\Worker\Delivery; use Friendica\Worker\Delivery;
@ -373,7 +374,7 @@ class Account extends BaseSettings
// or the handle of the account, therefore we check for either // or the handle of the account, therefore we check for either
// "http" or "@" to be present in the string. // "http" or "@" to be present in the string.
// All other fields from the row will be ignored // All other fields from the row will be ignored
if ((strpos($csvRow[0], '@') !== false) || in_array(parse_url($csvRow[0], PHP_URL_SCHEME), ['http', 'https'])) { if ((strpos($csvRow[0], '@') !== false) || Network::isValidHttpUrl($csvRow[0])) {
Worker::add(PRIORITY_MEDIUM, 'AddContact', local_user(), $csvRow[0]); Worker::add(PRIORITY_MEDIUM, 'AddContact', local_user(), $csvRow[0]);
} else { } else {
Logger::notice('Invalid account', ['url' => $csvRow[0]]); Logger::notice('Invalid account', ['url' => $csvRow[0]]);

View file

@ -1185,9 +1185,9 @@ class Transmitter
* @return array|false activity or false on failure * @return array|false activity or false on failure
* @throws \Exception * @throws \Exception
*/ */
public static function createCachedActivityFromItem(int $item_id, bool $force = false) public static function createCachedActivityFromItem(int $item_id, bool $force = false, bool $object_mode = false)
{ {
$cachekey = 'APDelivery:createActivity:' . $item_id; $cachekey = 'APDelivery:createActivity:' . $item_id . ':' . (int)$object_mode;
if (!$force) { if (!$force) {
$data = DI::cache()->get($cachekey); $data = DI::cache()->get($cachekey);
@ -1196,7 +1196,7 @@ class Transmitter
} }
} }
$data = self::createActivityFromItem($item_id); $data = self::createActivityFromItem($item_id, $object_mode);
DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR); DI::cache()->set($cachekey, $data, Duration::QUARTER_HOUR);
return $data; return $data;

View file

@ -795,7 +795,7 @@ return [
"conversation" => ["type" => "varbinary(383)", "comment" => ""], "conversation" => ["type" => "varbinary(383)", "comment" => ""],
"type" => ["type" => "varchar(64)", "comment" => "Type of the activity"], "type" => ["type" => "varchar(64)", "comment" => "Type of the activity"],
"object-type" => ["type" => "varchar(64)", "comment" => "Type of the object activity"], "object-type" => ["type" => "varchar(64)", "comment" => "Type of the object activity"],
"object-object-type" => ["type" => "varchar(64)", "comment" => "Type of the object's object activity"], "object-object-type" => ["type" => "varchar(64)", "comment" => "Type of the object's object activity"],
"received" => ["type" => "datetime", "comment" => "Receiving date"], "received" => ["type" => "datetime", "comment" => "Receiving date"],
"activity" => ["type" => "mediumtext", "comment" => "The JSON activity"], "activity" => ["type" => "mediumtext", "comment" => "The JSON activity"],
"signer" => ["type" => "varchar(255)", "comment" => ""], "signer" => ["type" => "varchar(255)", "comment" => ""],

View file

@ -643,7 +643,7 @@ return [
'worker_load_exponent' => 3, 'worker_load_exponent' => 3,
// worker_processes_cooldown (Integer) // worker_processes_cooldown (Integer)
// Maximum number pro processes that causes a cooldown before each worker function call. // Maximum number per processes that causes a cooldown before each worker function call.
'worker_processes_cooldown' => 0, 'worker_processes_cooldown' => 0,
// worker_multiple_fetch (Boolean) // worker_multiple_fetch (Boolean)