Improved "user_filter=remote" page caching

This commit is contained in:
Bofh 2022-12-16 04:38:14 +01:00
parent d8170d81d8
commit ffdbab1f79
1 changed files with 39 additions and 16 deletions

View File

@ -135,6 +135,13 @@ $all_accounts = $pg->fetch_all($sql_all, $cache, $only_cached);
if ($all_accounts === 'not_cached') if ($all_accounts === 'not_cached')
apiresult(['ok' => 'Query is being cached, but it is not available yet. Please wait until it is 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']) != '') if (isset($_GET['profile']) && trim($_GET['profile']) != '')
{ {
$q = trim($_GET['profile']); $q = trim($_GET['profile']);
@ -153,8 +160,27 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '')
} }
$ai = -1; $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) 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++; $ai++;
$a_note = trim($account['note']); $a_note = trim($account['note']);
$a_fields = json_decode($account['fields'], true); $a_fields = json_decode($account['fields'], true);
@ -175,7 +201,9 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '')
continue; continue;
$matches = false; $matches = false;
if ($qt === 'simple') { if ($enable_caching && isset($match_data[$account['id']])) {
$matches = $match_data[$account['id']];
} else if ($qt === 'simple') {
$matches = true; $matches = true;
$words = explode(' ', normalize_for_search($a_note)); $words = explode(' ', normalize_for_search($a_note));
foreach (explode(' ', $q) as $qi) { foreach (explode(' ', $q) as $qi) {
@ -194,20 +222,25 @@ if (isset($_GET['profile']) && trim($_GET['profile']) != '')
} else if ($qt === 'expr') } else if ($qt === 'expr')
$matches = matches_comparing_expression($q, $a_note); $matches = matches_comparing_expression($q, $a_note);
if ($user_filter === 'remote' && $enable_caching)
$match_data[$account['id']] = $matches;
if ($matches) { if ($matches) {
$filtered_accounts []= $account['id']; $filtered_accounts []= $account['id'];
$filtered_accounts_index[$account['id']] = $ai; $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'); $config = instance_config('mastodon');
$filtered_accounts = array_unique($filtered_accounts); $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 = []; $silenced_users = [];
$sile_file = 'silenced_accounts,'.$GLOBALS['ap_software'].','.$GLOBALS['ap_instance']; $sile_file = 'silenced_accounts,'.$GLOBALS['ap_software'].','.$GLOBALS['ap_instance'];
if (filedb_exists('cache', $sile_file)) if (filedb_exists('cache', $sile_file))
@ -218,16 +251,6 @@ foreach ($filtered_accounts as $id) {
$newacc = []; $newacc = [];
$account = $all_accounts[$filtered_accounts_index[$id]]; $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.'\''); $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.'\''); $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)) if (isset($_GET['no_follows']) && (intval($following['count']) > 0 || intval($followers['count']) > 0))