[ticket/13713] Rework names caching

PHPBB3-13713
This commit is contained in:
lavigor 2018-09-15 16:03:34 +03:00 committed by Marc Alexander
parent 5e26380da7
commit e769a036b6
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
7 changed files with 68 additions and 33 deletions

View file

@ -36,6 +36,9 @@ abstract class base_group implements source_interface
/** @var string */
protected $php_ext;
/** @var string|false */
protected $cache_ttl = false;
/** @var array Fetched groups' data */
protected $groups = null;
@ -125,8 +128,8 @@ abstract class base_group implements source_interface
*/
public function get(array &$names, $keyword, $topic_id)
{
// Grab all group IDs, cache for 5 minutes
$result = $this->db->sql_query($this->query($keyword, $topic_id), 300);
// Grab all group IDs and cache them if needed
$result = $this->db->sql_query($this->query($keyword, $topic_id), $this->cache_ttl);
$group_ids = [];
while ($row = $this->db->sql_fetchrow($result))

View file

@ -30,6 +30,9 @@ abstract class base_user implements source_interface
/** @var string */
protected $php_ext;
/** @var string|false */
protected $cache_ttl = false;
/**
* Constructor
*/
@ -73,47 +76,67 @@ abstract class base_user implements source_interface
$fetched_all = false;
$keyword = utf8_clean_string($keyword);
// Grab all necessary user IDs, cache results for 5 minutes
$result = $this->db->sql_query($this->query($keyword, $topic_id), 300);
$i = 0;
$users = [];
$user_ids = [];
while ($i < $this->config['mention_batch_size'])
// Grab all necessary user IDs and cache them if needed
if ($this->cache_ttl)
{
$row = $this->db->sql_fetchrow($result);
$result = $this->db->sql_query($this->query($keyword, $topic_id), $this->cache_ttl);
if (!$row)
while ($i < $this->config['mention_batch_size'])
{
$fetched_all = true;
break;
}
$row = $this->db->sql_fetchrow($result);
if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0)
{
continue;
}
if (!$row)
{
$fetched_all = true;
break;
}
$i++;
$users[] = $row;
$user_ids[] = $row['user_id'];
}
// Determine whether all usernames were fetched in current batch
if (!$fetched_all)
{
$fetched_all = true;
while ($row = $this->db->sql_fetchrow($result))
{
if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0)
{
continue;
}
// At least one username hasn't been fetched - exit loop
$fetched_all = false;
break;
$i++;
$users[] = $row;
$user_ids[] = $row['user_id'];
}
// Determine whether all usernames were fetched in current batch
if (!$fetched_all)
{
$fetched_all = true;
while ($row = $this->db->sql_fetchrow($result))
{
if (!empty($keyword) && strpos($row['username_clean'], $keyword) !== 0)
{
continue;
}
// At least one username hasn't been fetched - exit loop
$fetched_all = false;
break;
}
}
}
else
{
$result = $this->db->sql_query_limit($this->query($keyword, $topic_id), $this->config['mention_batch_size'], 0);
while ($row = $this->db->sql_fetchrow($result))
{
$users[] = $row;
$user_ids[] = $row['user_id'];
}
// Determine whether all usernames were fetched in current batch
if (count($user_ids) < $this->config['mention_batch_size'])
{
$fetched_all = true;
}
}

View file

@ -50,7 +50,8 @@ class friend extends base_user
]
],
'WHERE' => 'z.friend = 1 AND z.user_id = ' . (int) $this->user->data['user_id'] . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'u.user_lastvisit DESC'
]);
return $query;

View file

@ -15,6 +15,9 @@ namespace phpbb\mention\source;
class group extends base_group
{
/** @var string|false */
protected $cache_ttl = 300;
/**
* {@inheritdoc}
*/

View file

@ -15,6 +15,9 @@ namespace phpbb\mention\source;
class team extends base_user
{
/** @var string|false */
protected $cache_ttl = 300;
/**
* {@inheritdoc}
*/

View file

@ -61,7 +61,8 @@ class topic extends base_user
],
],
'WHERE' => 'p.topic_id = ' . (int) $topic_id . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'p.post_time DESC'
]);
return $query;

View file

@ -39,7 +39,8 @@ class user extends base_user
'FROM' => [
USERS_TABLE => 'u',
],
'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]),
'WHERE' => $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND u.username_clean ' . $this->db->sql_like_expression($keyword . $this->db->get_any_char()),
'ORDER_BY' => 'u.user_lastvisit DESC'
]);
return $query;