From ad017baaa6e09375cd347b4da635de168c7ac69f Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 20 Nov 2025 20:41:20 +0000 Subject: [PATCH] singleton object cache - initial commit --- Zotlabs/Daemon/Cron.php | 68 +++++++++++++++++++++++++++++++++++++ Zotlabs/Daemon/Notifier.php | 9 ++++- Zotlabs/Lib/Activity.php | 11 ++++-- Zotlabs/Lib/Libzot.php | 4 +-- Zotlabs/Module/Viewsrc.php | 18 ++++++++-- include/items.php | 10 ++++++ 6 files changed, 111 insertions(+), 9 deletions(-) diff --git a/Zotlabs/Daemon/Cron.php b/Zotlabs/Daemon/Cron.php index 131abe2e1..647aea543 100644 --- a/Zotlabs/Daemon/Cron.php +++ b/Zotlabs/Daemon/Cron.php @@ -3,6 +3,7 @@ namespace Zotlabs\Daemon; use Zotlabs\Lib\Config; +use Zotlabs\Lib\ObjCache; use Zotlabs\Lib\Libsync; use Zotlabs\Lib\Libzotdir; @@ -236,6 +237,73 @@ class Cron { if (!$restart) Master::Summon(array('Cronhooks')); + + // move as obj cache to fs + if (!Config::Get('system', 'as_objects_moved')) { + $results = dbq("select iconfig.*, item.mid from iconfig left join item on iid = item.id where cat = 'activitypub' and k = 'rawmsg' limit 300"); + if ($results) { + foreach ($results as $result) { + if (is_string($result['v'])) { + if (str_starts_with($result['v'], '{')) { + $result['v'] = json_decode($result['v'], true); + } + + elseif (str_starts_with($result['v'], 'json:')) { + $result['v'] = unserialise($result['v']); + } + + elseif (preg_match('|^a:[0-9]+:{.*}$|s', $result['v'])) { + $result['v'] = unserialize($result['v'], ['allowed_classes' => false]); + } + } + + if (is_array($result['v'])) { + ObjCache::Set($result['mid'], $result['v']); + } + + q("delete from iconfig where id = %d", + intval($result['id']) + ); + } + } + else { + Config::Set('system', 'as_objects_moved', 1); + } + } + + // move diaspora obj cache to fs + if (!Config::Get('system', 'diaspora_objects_moved')) { + $results = dbq("select iconfig.*, item.mid from iconfig left join item on iid = item.id where cat = 'diaspora' and k = 'fields' limit 300"); + if ($results) { + foreach ($results as $result) { + if (is_string($result['v'])) { + if (str_starts_with($result['v'], '{')) { + $result['v'] = json_decode($result['v'], true); + } + + elseif (str_starts_with($result['v'], 'json:')) { + $result['v'] = unserialise($result['v']); + } + + elseif (preg_match('|^a:[0-9]+:{.*}$|s', $result['v'])) { + $result['v'] = unserialize($result['v'], ['allowed_classes' => false]); + } + } + + if (is_array($result['v'])) { + ObjCache::Set($result['mid'], $result['v'], 'diaspora'); + } + + q("delete from iconfig where id = %d", + intval($result['id']) + ); + } + } + else { + Config::Set('system', 'diaspora_objects_moved', 1); + } + } + Config::Set('system', 'lastcron', datetime_convert()); //All done - clear the lockfile diff --git a/Zotlabs/Daemon/Notifier.php b/Zotlabs/Daemon/Notifier.php index 30c6e431d..81a17d8fe 100644 --- a/Zotlabs/Daemon/Notifier.php +++ b/Zotlabs/Daemon/Notifier.php @@ -4,6 +4,8 @@ namespace Zotlabs\Daemon; use Zotlabs\Lib\Activity; use Zotlabs\Lib\Config; +use Zotlabs\Lib\IConfig; +use Zotlabs\Lib\ObjCache; use Zotlabs\Lib\Libzot; use Zotlabs\Lib\Queue; @@ -317,7 +319,12 @@ class Notifier { return; } - $m = get_iconfig($target_item, 'activitypub', 'rawmsg'); + $m = ObjCache::Get($target_item['mid']); + + if (!$m) { + $m = IConfig::Get($target_item, 'activitypub', 'rawmsg'); + } + // Re-use existing signature unless the activity type changed to a Tombstone, which won't verify. if ($m && (!intval($target_item['item_deleted']))) { self::$encoded_item = $m; diff --git a/Zotlabs/Lib/Activity.php b/Zotlabs/Lib/Activity.php index 43a496831..9fca8e4a7 100644 --- a/Zotlabs/Lib/Activity.php +++ b/Zotlabs/Lib/Activity.php @@ -382,7 +382,12 @@ class Activity { if ($items) { $x = []; foreach ($items as $i) { - $m = IConfig::Get($i['id'], 'activitypub', 'rawmsg'); + $m = ObjCache::Get($i['mid']); + + if (!$m) { + $m = IConfig::Get($i['id'], 'activitypub', 'rawmsg'); + } + if ($m) { if (is_string($m)) $t = json_decode($m, true); @@ -2681,7 +2686,7 @@ class Activity { IConfig::Set($s, 'event', 'timezone', $act->objprop('timezone'), true); } - IConfig::Set($s, 'activitypub', 'rawmsg', $act->data); + ObjCache::Set($s['mid'], $act->data); $hookinfo = [ 'act' => $act, @@ -2846,7 +2851,7 @@ class Activity { if (tgroup_check($channel['channel_id'], $item) && (!$is_child_node)) { // for forum deliveries, make sure we keep a copy of the signed original - IConfig::Set($item, 'activitypub', 'rawmsg', $act->raw, 1); + ObjCache::Set($item['mid'], $act->data); $allowed = true; } diff --git a/Zotlabs/Lib/Libzot.php b/Zotlabs/Lib/Libzot.php index 7f62a4679..e3fa6e9d3 100644 --- a/Zotlabs/Lib/Libzot.php +++ b/Zotlabs/Lib/Libzot.php @@ -6,7 +6,6 @@ use App; use Zotlabs\Access\PermissionLimits; use Zotlabs\Access\Permissions; use Zotlabs\Daemon\Master; -use Zotlabs\Lib\Config; use Zotlabs\Web\HTTPSig; require_once('include/crypto.php'); @@ -2140,10 +2139,9 @@ class Libzot { } if (isset($AS->meta['signed_data'])) { - IConfig::Set($arr, 'activitypub', 'signed_data', $AS->meta['signed_data'], false); $j = json_decode($AS->meta['signed_data'], true); if ($j) { - IConfig::Set($arr, 'activitypub', 'rawmsg', json_encode(JSalmon::unpack($j['data'])), true); + ObjCache::Set($arr['mid'], json_encode(JSalmon::unpack($j['data']))); } } diff --git a/Zotlabs/Module/Viewsrc.php b/Zotlabs/Module/Viewsrc.php index c8cc9383b..c3d8d96de 100644 --- a/Zotlabs/Module/Viewsrc.php +++ b/Zotlabs/Module/Viewsrc.php @@ -2,6 +2,8 @@ namespace Zotlabs\Module; use Zotlabs\Lib\Activity; +use Zotlabs\Lib\IConfig; +use Zotlabs\Lib\ObjCache; class Viewsrc extends \Zotlabs\Web\Controller { @@ -38,7 +40,6 @@ class Viewsrc extends \Zotlabs\Web\Controller { xchan_query($r, true); $r = fetch_post_tags($r); - if(intval($r[0]['item_obscured'])) $dload = true; @@ -50,7 +51,20 @@ class Viewsrc extends \Zotlabs\Web\Controller { } $cached = true; - $obj = get_iconfig($r[0], 'activitypub', 'rawmsg'); + + $obj = ObjCache::Get($r[0]['mid']); + + if (!$obj) { + $obj = IConfig::Get($r[0], 'activitypub', 'rawmsg'); + } + + if (in_array($r[0]['owner']['xchan_network'], ['diaspora'])) { + $obj = ObjCache::Get($r[0]['mid'], 'diaspora'); + + if (!$obj) { + $obj = IConfig::Get($r[0], 'diaspora', 'fields'); + } + } if (!$obj) { $cached = false; diff --git a/include/items.php b/include/items.php index 993fc86b5..b5c9bd5f7 100644 --- a/include/items.php +++ b/include/items.php @@ -10,6 +10,7 @@ use Zotlabs\Lib\Enotify; use Zotlabs\Lib\MarkdownSoap; use Zotlabs\Lib\MessageFilter; use Zotlabs\Lib\ThreadListener; +use Zotlabs\Lib\ObjCache; use Zotlabs\Lib\IConfig; use Zotlabs\Lib\PConfig; use Zotlabs\Lib\Activity; @@ -4179,6 +4180,15 @@ function delete_item_lowlevel($item, $stage = DROPITEM_NORMAL) { intval($item['id']) ); + $n = q("SELECT count(id) AS total FROM item WHERE mid = '%s'", + dbesc($item['mid']) + ); + + if (!$n[0]['total']) { + ObjCache::Delete($item['mid']); + ObjCache::Delete($item['mid'], 'diaspora'); + } + q("delete from term where oid = %d and otype = %d", intval($item['id']), intval(TERM_OBJ_POST)