Pagecache for frequently fetched pages

This commit is contained in:
Michael
2022-09-06 06:04:41 +00:00
parent 934a3a6721
commit 6eb9dff807
11 changed files with 151 additions and 26 deletions
+1 -1
View File
@@ -3321,7 +3321,7 @@ class Contact
continue;
}
$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);
++$added;
} elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
+6 -20
View File
@@ -21,8 +21,10 @@
namespace Friendica\Model;
use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
class ItemURI
{
@@ -35,14 +37,16 @@ class ItemURI
*/
public static function insert(array $fields)
{
$fields = DI::dbaDefinition()->truncateFieldsForTable('item-uri', $fields);
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']]);
if (!DBA::isResult($itemuri)) {
// This shouldn't happen
Logger::warning('Item-uri not found', $fields);
return null;
}
@@ -77,22 +81,4 @@ class ItemURI
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'];
}
}
+12
View File
@@ -31,6 +31,7 @@ use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\ActivityPub\PageCache;
use Friendica\Util\HTTPSignature;
use Friendica\Util\Network;
use Friendica\Util\Strings;
@@ -50,6 +51,13 @@ class Objects extends BaseModule
DI::baseUrl()->redirect(str_replace('objects/', 'display/', DI::args()->getQueryString()));
}
$data = PageCache::fetch($_SERVER['REQUEST_URI']);
if (!empty($data)) {
header('Access-Control-Allow-Origin: *');
System::jsonExit($data, 'application/activity+json');
}
$itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $this->parameters['guid']]);
if (DBA::isResult($itemuri)) {
@@ -127,6 +135,10 @@ class Objects extends BaseModule
throw new HTTPException\NotFoundException();
}
if (in_array($item['private'], [Item::PUBLIC, Item::UNLISTED])) {
PageCache::add($_SERVER['REQUEST_URI'], $data);
}
// Relaxed CORS header for public items
header('Access-Control-Allow-Origin: *');
+2 -1
View File
@@ -38,6 +38,7 @@ use Friendica\Model\Verb;
use Friendica\Module\BaseSettings;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Util\Network;
use Friendica\Util\Temporal;
use Friendica\Worker\Delivery;
@@ -373,7 +374,7 @@ class Account extends BaseSettings
// or the handle of the account, therefore we check for either
// "http" or "@" to be present in the string.
// 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]);
} else {
Logger::notice('Invalid account', ['url' => $csvRow[0]]);
+73
View File
@@ -0,0 +1,73 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Protocol\ActivityPub;
use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\DateTimeFormat;
/**
* This class handles the page cache
*/
class PageCache
{
/**
* Add content to the page cache
*
* @param string $page
* @param mixed $content
* @return void
*/
public static function add(string $page, $content)
{
if (!DI::config()->get('system', 'pagecache')) {
return;
}
DBA::delete('pagecache', ["`fetched` < ?", DateTimeFormat::utc('now - 5 minutes')]);
DBA::insert('pagecache', ['page' => $page, 'content' => serialize($content), 'fetched' => DateTimeFormat::utcNow()], Database::INSERT_UPDATE);
Logger::debug('Page added', ['page' => $page]);
}
/**
* Fetch data from the page cache
*
* @param string $page
* @return mixed
*/
public static function fetch(string $page)
{
$pagecache = DBA::selectFirst('pagecache', [], ['page' => $page]);
if (empty($pagecache['content'])) {
return null;
}
DBA::update('pagecache', ['fetched' => DateTimeFormat::utcNow()], ['page' => $page]);
Logger::debug('Page fetched', ['page' => $page]);
return unserialize($pagecache['content']);
}
}
+1
View File
@@ -43,6 +43,7 @@ class OptimizeTables
DBA::e("OPTIMIZE TABLE `cache`");
DBA::e("OPTIMIZE TABLE `locks`");
DBA::e("OPTIMIZE TABLE `oembed`");
DBA::e("OPTIMIZE TABLE `pagecache`");
DBA::e("OPTIMIZE TABLE `parsed_url`");
DBA::e("OPTIMIZE TABLE `session`");