From da658cbf1dc1b2e53b6c9c47d56d89aaa6587ab1 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 6 Sep 2022 21:51:47 +0000 Subject: [PATCH] Delete the cache entry when the post is changed or deleted --- database.sql | 5 ++++- doc/database/db_pagecache.md | 18 +++++++++++++----- src/Model/Item.php | 4 ++++ src/Module/ActivityPub/Objects.php | 2 +- src/Protocol/ActivityPub/PageCache.php | 16 ++++++++++++++-- static/dbstructure.config.php | 2 ++ 6 files changed, 38 insertions(+), 9 deletions(-) diff --git a/database.sql b/database.sql index d50e0bbd06..e1ca686d47 100644 --- a/database.sql +++ b/database.sql @@ -1012,10 +1012,13 @@ CREATE TABLE IF NOT EXISTS `openwebauth-token` ( -- CREATE TABLE IF NOT EXISTS `pagecache` ( `page` varbinary(255) NOT NULL COMMENT 'Page', + `uri-id` int unsigned COMMENT 'Id of the item-uri table that contains the uri the page belongs to', `content` mediumtext COMMENT 'Page content', `fetched` datetime COMMENT 'date when the page had been fetched', PRIMARY KEY(`page`), - INDEX `fetched` (`fetched`) + INDEX `fetched` (`fetched`), + INDEX `uri-id` (`uri-id`), + FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Stores temporary data'; -- diff --git a/doc/database/db_pagecache.md b/doc/database/db_pagecache.md index 6cc3edf0fd..2be2df298d 100644 --- a/doc/database/db_pagecache.md +++ b/doc/database/db_pagecache.md @@ -6,11 +6,12 @@ Stores temporary data Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| ------- | ----------------------------------- | -------------- | ---- | --- | ------- | ----- | -| page | Page | varbinary(255) | NO | PRI | NULL | | -| content | Page content | mediumtext | YES | | NULL | | -| fetched | date when the page had been fetched | datetime | YES | | NULL | | +| Field | Description | Type | Null | Key | Default | Extra | +| ------- | ------------------------------------------------------------------ | -------------- | ---- | --- | ------- | ----- | +| page | Page | varbinary(255) | NO | PRI | NULL | | +| uri-id | Id of the item-uri table that contains the uri the page belongs to | int unsigned | YES | | NULL | | +| content | Page content | mediumtext | YES | | NULL | | +| fetched | date when the page had been fetched | datetime | YES | | NULL | | Indexes ------------ @@ -19,6 +20,13 @@ Indexes | ------- | ------- | | PRIMARY | page | | fetched | fetched | +| uri-id | uri-id | +Foreign Keys +------------ + +| Field | Target Table | Target Field | +|-------|--------------|--------------| +| uri-id | [item-uri](help/database/db_item-uri) | id | Return to [database documentation](help/database) diff --git a/src/Model/Item.php b/src/Model/Item.php index 78bc8c64d1..bd5591ef39 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -196,6 +196,8 @@ class Item $notify_items = []; while ($item = DBA::fetch($items)) { + ActivityPub\PageCache::deleteByUriId($item['uri-id']); + if (!empty($fields['body'])) { Post\Media::insertFromAttachmentData($item['uri-id'], $fields['body']); @@ -319,6 +321,8 @@ class Item // clean up categories and tags so they don't end up as orphans Post\Category::deleteByURIId($item['uri-id'], $item['uid']); + ActivityPub\PageCache::deleteByUriId($item['uri-id']); + /* * If item is a link to a photo resource, nuke all the associated photos * (visitors will not have photo resources) diff --git a/src/Module/ActivityPub/Objects.php b/src/Module/ActivityPub/Objects.php index dc9eaaf60c..b0834929c4 100644 --- a/src/Module/ActivityPub/Objects.php +++ b/src/Module/ActivityPub/Objects.php @@ -136,7 +136,7 @@ class Objects extends BaseModule } if (in_array($item['private'], [Item::PUBLIC, Item::UNLISTED])) { - PageCache::add($_SERVER['REQUEST_URI'], $data); + PageCache::add($_SERVER['REQUEST_URI'], $item['uri-id'], $data); } // Relaxed CORS header for public items diff --git a/src/Protocol/ActivityPub/PageCache.php b/src/Protocol/ActivityPub/PageCache.php index d34283b152..d9e82d713c 100644 --- a/src/Protocol/ActivityPub/PageCache.php +++ b/src/Protocol/ActivityPub/PageCache.php @@ -36,17 +36,18 @@ class PageCache * Add content to the page cache * * @param string $page + * @param int $uriid * @param mixed $content * @return void */ - public static function add(string $page, $content) + public static function add(string $page, int $uriid, $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); + DBA::insert('pagecache', ['page' => $page, 'uri-id' => $uriid, 'content' => serialize($content), 'fetched' => DateTimeFormat::utcNow()], Database::INSERT_UPDATE); Logger::debug('Page added', ['page' => $page]); } @@ -70,4 +71,15 @@ class PageCache return unserialize($pagecache['content']); } + + /** + * Delete the pagecache via its connected uri-id + * + * @param integer $uriid + * @return void + */ + public static function deleteByUriId(int $uriid) + { + DBA::delete('pagecache', ['uri-id' => $uriid]); + } } diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 01c593f68d..1e7fae786f 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1055,12 +1055,14 @@ return [ "comment" => "Stores temporary data", "fields" => [ "page" => ["type" => "varbinary(255)", "not null" => "1", "primary" => "1", "comment" => "Page"], + "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table that contains the uri the page belongs to"], "content" => ["type" => "mediumtext", "comment" => "Page content"], "fetched" => ["type" => "datetime", "comment" => "date when the page had been fetched"], ], "indexes" => [ "PRIMARY" => ["page"], "fetched" => ["fetched"], + "uri-id" => ["uri-id"], ], ], "parsed_url" => [ -- 2.39.5