memcache user list

This commit is contained in:
ghost 2024-02-18 16:37:59 +02:00
parent 3b0b482461
commit d3d2a61056

View File

@ -51,78 +51,113 @@ class UserController extends AbstractController
?Request $request ?Request $request
): Response ): Response
{ {
$list = []; // Connect memcached
$memcached = new \Memcached();
$memcached->addServer(
$this->getParameter('app.memcached.host'),
$this->getParameter('app.memcached.port')
);
// Check client connection $memory = md5(
if ($client = $this->_client()) sprintf(
'%s.UserController::list:list',
__DIR__,
),
);
if (!$list = $memcached->get($memory))
{ {
// Check users database accessible $list = [];
if ($namespace = $this->_namespace($client))
// Check client connection
if ($client = $this->_client())
{ {
// Collect usernames // Check users database accessible
foreach ((array) $client->kevaFilter($namespace) as $user) if ($namespace = $this->_namespace($client))
{ {
// Check record valid // Collect usernames
if (empty($user['key']) || empty($user['height'])) foreach ((array) $client->kevaFilter($namespace) as $user)
{ {
continue; // Check record valid
} if (empty($user['key']) || empty($user['height']))
// Skip values with meta keys
if (str_starts_with($user['key'], '_'))
{
continue;
}
// Validate username regex
if (!preg_match($this->getParameter('app.add.user.name.regex'), $user['key']))
{
continue;
}
// Get room stats
$total = 0;
$rooms = [];
foreach ($this->_rooms($client) as $room => $name)
{
$posts = 0;
foreach ((array) $client->kevaFilter($room, sprintf('^[\d]+@%s$', $user['key'])) as $post)
{ {
$total++; continue;
$posts++;
$rooms[$room] = $posts;
} }
}
$list[] = // Skip values with meta keys
[ if (str_starts_with($user['key'], '_'))
'name' => $user['key'], {
'balance' => $client->getBalance( continue;
$user['key'], }
$this->getParameter('app.pool.confirmations')
), // Validate username regex
'address' => $client->getAccountAddress( if (!preg_match($this->getParameter('app.add.user.name.regex'), $user['key']))
$user['key'] {
), continue;
'total' => $total, }
'rooms' => $rooms,
]; // Get room stats
$rooms = [];
foreach ((array) $client->kevaListNamespaces() as $value)
{
if (empty($value['namespaceId']))
{
continue;
}
if (empty($value['displayName']))
{
continue;
}
if (str_starts_with($value['displayName'], '_'))
{
continue;
}
$posts = 0;
foreach ((array) $client->kevaFilter($value['namespaceId'], sprintf('^[\d]+@%s$', $user['key'])) as $post)
{
$posts++;
$rooms[$value['displayName']] = $posts;
}
}
$list[] =
[
'name' => $user['key'],
'balance' => $client->getBalance(
$user['key'],
$this->getParameter('app.pool.confirmations')
),
'address' => $client->getAccountAddress(
$user['key']
),
'rooms' => $rooms,
];
}
} }
} }
}
// Sort by height // Sort by height
array_multisort( array_multisort(
array_column( array_column(
$list, $list,
'total' 'total'
), ),
SORT_DESC, SORT_DESC,
$list $list
); );
// Cache result
$memcached->set(
$memory,
$list
);
}
// RSS // RSS
if ('rss' === $request->get('feed')) if ('rss' === $request->get('feed'))
@ -882,35 +917,6 @@ class UserController extends AbstractController
return null; return null;
} }
private function _rooms(
\Kevachat\Kevacoin\Client $client
): array
{
$rooms = [];
foreach ((array) $client->kevaListNamespaces() as $value)
{
if (empty($value['namespaceId']))
{
continue;
}
if (empty($value['displayName']))
{
continue;
}
if (str_starts_with($value['displayName'], '_'))
{
continue;
}
$rooms[$value['namespaceId']] = $value['displayName'];
}
return $rooms;
}
private function _hash( private function _hash(
\Kevachat\Kevacoin\Client $client, \Kevachat\Kevacoin\Client $client,
string $namespace, string $namespace,