From ffdbab1f79ec9bac38727e8a291e6fbd217856b0 Mon Sep 17 00:00:00 2001 From: Bofh Date: Fri, 16 Dec 2022 04:38:14 +0100 Subject: [PATCH] Improved "user_filter=remote" page caching --- .../database/mastodon/accounts/search/mod.php | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/api/v1/database/mastodon/accounts/search/mod.php b/api/v1/database/mastodon/accounts/search/mod.php index ee4be35..7477b04 100644 --- a/api/v1/database/mastodon/accounts/search/mod.php +++ b/api/v1/database/mastodon/accounts/search/mod.php @@ -135,6 +135,13 @@ $all_accounts = $pg->fetch_all($sql_all, $cache, $only_cached); if ($all_accounts === 'not_cached') apiresult(['ok' => 'Query is being cached, but it is not available yet. Please wait until it is cached']); + +$trusted_users = mod_php('api/v1/config/trusted_users/get'); +$suspended_users = []; +$susp_file = 'suspended_accounts,'.$GLOBALS['ap_software'].','.$GLOBALS['ap_instance']; +if (filedb_exists('cache', $susp_file)) + $suspended_users = unserialize(filedb_get('cache', $susp_file)); + if (isset($_GET['profile']) && trim($_GET['profile']) != '') { $q = trim($_GET['profile']); @@ -153,8 +160,27 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '') } $ai = -1; + $count_matches = 0; + $enable_caching = false; + $match_cache_exists = true; + $match_ckey = _cachekey_construct('matches_note', $filter_key, $qt); + $match_data = []; + if (content_cache__exists($match_ckey) !== false) + $match_data = content_cache__get($match_ckey); + else $match_cache_exists = false; + foreach ($all_accounts as $account) { + $acct_for_trusted = $account['username'].'@'.($account['domain'] === null ? + $GLOBALS['ap_instance'] : $account['domain']); + if (in_array($acct_for_trusted, $trusted_users) || + in_array($account['id'], $suspended_users)) + continue; + + $ckey = _cachekey_construct('temp_hidden', $filter_key, $acct_for_trusted); + if (content_cache__exists($ckey) !== false) + continue; + $ai++; $a_note = trim($account['note']); $a_fields = json_decode($account['fields'], true); @@ -175,7 +201,9 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '') continue; $matches = false; - if ($qt === 'simple') { + if ($enable_caching && isset($match_data[$account['id']])) { + $matches = $match_data[$account['id']]; + } else if ($qt === 'simple') { $matches = true; $words = explode(' ', normalize_for_search($a_note)); foreach (explode(' ', $q) as $qi) { @@ -194,20 +222,25 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '') } else if ($qt === 'expr') $matches = matches_comparing_expression($q, $a_note); + if ($user_filter === 'remote' && $enable_caching) + $match_data[$account['id']] = $matches; + if ($matches) { $filtered_accounts []= $account['id']; $filtered_accounts_index[$account['id']] = $ai; + if ($user_filter !== 'remote') + continue; + $count_matches++; + if ($paging !== null && $count_matches >= $paging[1] && !$enable_caching) + $enable_caching = true; } } + if ($enable_caching && !$match_cache_exists) + content_cache__put($match_ckey, 'ondemand,300', $match_data); } $config = instance_config('mastodon'); $filtered_accounts = array_unique($filtered_accounts); -$trusted_users = mod_php('api/v1/config/trusted_users/get'); -$suspended_users = []; -$susp_file = 'suspended_accounts,'.$GLOBALS['ap_software'].','.$GLOBALS['ap_instance']; -if (filedb_exists('cache', $susp_file)) - $suspended_users = unserialize(filedb_get('cache', $susp_file)); $silenced_users = []; $sile_file = 'silenced_accounts,'.$GLOBALS['ap_software'].','.$GLOBALS['ap_instance']; if (filedb_exists('cache', $sile_file)) @@ -218,16 +251,6 @@ foreach ($filtered_accounts as $id) { $newacc = []; $account = $all_accounts[$filtered_accounts_index[$id]]; - $acct_for_trusted = $account['username'].'@'.($account['domain'] === null ? - $GLOBALS['ap_instance'] : $account['domain']); - if (in_array($acct_for_trusted, $trusted_users) || - in_array($id, $suspended_users)) - continue; - - $ckey = _cachekey_construct('temp_hidden', $filter_key, $acct_for_trusted); - if (content_cache__exists($ckey) !== false) - continue; - $following = $pg->fetch('SELECT count(1) FROM follows WHERE account_id = \''.$id.'\''); $followers = $pg->fetch('SELECT count(1) FROM follows WHERE target_account_id = \''.$id.'\''); if (isset($_GET['no_follows']) && (intval($following['count']) > 0 || intval($followers['count']) > 0))