Browse Source

fix session cache

main
ghost 9 months ago
parent
commit
5d052c2d3a
  1. 148
      src/controller/room.php

148
src/controller/room.php

@ -159,19 +159,13 @@ class Room
public function posts(string $namespace): ?string public function posts(string $namespace): ?string
{ {
// Check for cache // Get records by namespace
if ($result = $this->_memory->get([__METHOD__, $namespace])) if (!$records = (array) $this->_records($namespace))
{
return $result;
}
// Get namespace records
if (!$records = (array) $this->_kevacoin->kevaFilter($namespace))
{ {
return null; return null;
} }
// Get saved posts // Get posts
$posts = []; $posts = [];
foreach ($records as $record) foreach ($records as $record)
@ -183,27 +177,8 @@ class Room
} }
// Get pending posts // Get pending posts
foreach ((array) $this->_kevacoin->kevaPending() as $pending) foreach ($this->_pending() as $pending)
{ {
// Ignore pending posts from other rooms
if ($pending['namespace'] != $namespace)
{
continue;
}
// Ignore everything in pending queue but keva_put
if ($pending['op'] != 'keva_put')
{
continue;
}
// Skip meta
if (str_starts_with($pending['key'], '_'))
{
continue;
}
// Require valid kevachat post
if ($post = $this->_post($namespace, $pending['key'], $records, null, $time)) if ($post = $this->_post($namespace, $pending['key'], $records, null, $time))
{ {
$posts[$time] = $post; $posts[$time] = $post;
@ -213,14 +188,15 @@ class Room
// Sort posts by time // Sort posts by time
krsort($posts); krsort($posts);
// Get result // Build result
$result = str_replace( return str_replace(
[ [
'{logo}', '{logo}',
'{home}', '{home}',
'{post}', '{post}',
'{subject}', '{subject}',
'{posts}' '{posts}',
'{session}'
], ],
[ [
// logo // logo
@ -234,9 +210,8 @@ class Room
// post // post
$this->_link( // @TODO $this->_link( // @TODO
sprintf( sprintf(
'/room/%s/%d/post', '/room/%s/{session}/post',
$namespace, $namespace
$this->_session
) )
), ),
@ -249,23 +224,15 @@ class Room
implode( implode(
PHP_EOL, PHP_EOL,
$posts $posts
) ),
// session
$this->_session
], ],
file_get_contents( file_get_contents(
__DIR__ . '/../view/posts.gemini' __DIR__ . '/../view/posts.gemini'
) )
); );
// Save to cache
$this->_memory->set(
[
__METHOD__,
$namespace
],
$result
);
return $result;
} }
public function post(string $namespace, ?string $txid, int $session, string $message): ?string public function post(string $namespace, ?string $txid, int $session, string $message): ?string
@ -322,15 +289,22 @@ class Room
) )
) )
{ {
// Cleanup memory // Cleanup session
$this->_memory->delete( $this->_memory->delete(
$session $session
); );
// Reset post list cache for this room // Reset cache
$this->_memory->delete(
[
'Kevachat\Geminiapp\Controller\Room::_pending',
$namespace
]
);
$this->_memory->delete( $this->_memory->delete(
[ [
'Kevachat\Geminiapp\Controller\Room::posts', 'Kevachat\Geminiapp\Controller\Room::_records',
$namespace $namespace
] ]
); );
@ -517,10 +491,9 @@ class Room
// Reply link // Reply link
$links[] = $this->_link( $links[] = $this->_link(
sprintf( sprintf(
'/room/%s/%s/%d/reply', '/room/%s/%s/{session}/reply',
$namespace, $namespace,
$record['txid'], $record['txid']
$this->_session
), ),
_('Reply'), _('Reply'),
true true
@ -928,4 +901,73 @@ class Room
return null; return null;
} }
private function _pending(): array
{
// Check for cache
if ($result = $this->_memory->get([__METHOD__]))
{
return $result;
}
// Get pending posts
$result = [];
foreach ((array) $this->_kevacoin->kevaPending() as $pending)
{
// Ignore pending from other namespaces
if ($pending['namespace'] != $namespace)
{
continue;
}
// Ignore everything in pending queue but keva_put
if ($pending['op'] != 'keva_put')
{
continue;
}
// Skip meta
if (str_starts_with($pending['key'], '_'))
{
continue;
}
$result[] = $pending;
}
// Save to cache
$this->_memory->set(
[
__METHOD__
],
$result
);
return $result;
}
private function _records(string $namespace): array
{
// Check for cache
if ($result = $this->_memory->get([__METHOD__]))
{
return $result;
}
// Get namespace records
$result = (array) $this->_kevacoin->kevaFilter(
$namespace
);
// Save to cache
$this->_memory->set(
[
__METHOD__
],
$result
);
return $result;
}
} }
Loading…
Cancel
Save