Browse Source

memcached sidebar and channel list

Byron Jones 6 years ago
parent
commit
bce1e78fc9

+ 15 - 17
logbot-web

@@ -93,19 +93,20 @@ under sub {
 
     # reload config, regenerating channel list if required
     $networks->[$config_index] = reload_config($networks->[$config_index]);
-    build_channel_list($networks->[$config_index]);
+    my $config = $networks->[$config_index];
+    build_channel_list($config);
 
     # store network name in response header for logging
-    $c->res->headers->add('X-Network' => $networks->[$config_index]->{name});
+    $c->res->headers->add('X-Network' => $config->{name});
 
     nick_colour_init();
 
     $c->stash(
-        config          => $networks->[$config_index],
+        config          => $config,
         networks        => $networks,
-        network         => $networks->[$config_index]->{name},
-        channels        => $networks->[$config_index]->{channel_list},
-        topics          => channel_topics($networks->[$config_index]),
+        network         => $config->{name},
+        channels        => $config->{channel_list},
+        topics          => channel_topics($config),
         font_s          => $c->cookie('font-s'),
         menu_c          => $c->cookie('menu-c'),
         nick_u          => $c->cookie('nick-u'),
@@ -123,7 +124,7 @@ under sub {
         is_stats        => 0,
         today           => DateTime->now()->truncate(to => 'day'),
         is_today        => 0,
-        cache_key       => '',
+        cache_prefix    => $config->{_derived}->{time} . '.' . $config->{name} . '.',
     );
     return 1;
 };
@@ -155,7 +156,6 @@ get '/' => sub {
 
         # index
         my $config = $c->stash('config');
-        $c->stash(cache_key => $config->{_derived}->{time} . '.' . $config->{name});
         LogBot::Web::Index::render($c);
     }
 };
@@ -251,10 +251,6 @@ get '/#channel/:date' => [date => qr/\d{8}/] => sub {
         is_logs  => 1,
         is_today => $date == $c->stash('today'),
     );
-    if (!$c->stash('is_today')) {
-        my $config = $c->stash('config');
-        $c->stash(cache_key => $config->{_derived}->{time} . '.' . $config->{name} . $channel . '.' . $date->ymd(''));
-    }
 
     # render
     LogBot::Web::Channel::logs($c);
@@ -317,26 +313,28 @@ my %cache;
 
 # static file with timestamp
 helper static => sub {
-    my ($self, $file) = @_;
+    my ($c, $file) = @_;
     return $cache{static}->{$file} //= '/static/' . $file . '?' . (stat($RealBin . '/web/public/static/' . $file))[9];
 };
 
 # inline svg
 helper svg => sub {
-    my ($self, $file) = @_;
+    my ($c, $file) = @_;
     return $cache{svg}->{$file} //= Mojo::ByteStream->new(slurp($RealBin . '/web/svg/' . $file . '.svg'));
 };
 
 # linkify text
 helper linkify => sub {
-    my ($self, $text) = @_;
+    my ($c, $text) = @_;
     return linkify($text);
 };
 
 # cache
 helper cached => sub {
-    my ($self, $key, $callback) = @_;
-    return Mojo::ByteStream->new($key ? $memcache->get($key, $callback) : $callback->());
+    my ($c, $key, $callback) = @_;
+    return $key eq ''
+        ? $callback->()
+        : Mojo::ByteStream->new($memcache->get($c->stash('cache_prefix') . $key, $callback));
 };
 
 hook after_render => sub {

+ 1 - 1
web/templates/channel.html.ep

@@ -5,7 +5,7 @@
 % use LogBot::Web::Util qw( nick_colour url_for_channel );
 % use Mojo::Util qw( trim );
 
-%= cached $cache_key => begin
+%= cached $is_today ? '' : $channel . '.' . $date->ymd('') => begin
 
 %= include 'shared/nick_styles', nick_hashes => $nick_hashes
 

+ 1 - 1
web/templates/index.html.ep

@@ -1,7 +1,7 @@
 % layout 'logbot';
 % use LogBot::Web::Util qw( irc_host );
 
-%= cached $cache_key => begin
+%= cached 'index' => begin
 
 <div id="index">
     <div id="logo-large">

+ 2 - 0
web/templates/layouts/logbot.html.ep

@@ -20,6 +20,7 @@
   <noscript><h1>This site requires JavaScript</h1></noscript>
 
   <div id="sidebar">
+    %= cached 'sidebar' => begin
 
     <div id="actions">
       <a id="collapse-sidebar" class="button" title="Toggle Sidebar">
@@ -82,6 +83,7 @@
         </ul>
       </aside>
     </div>
+    % end
   </div>
 
   <div id="nav">

+ 4 - 2
web/templates/list.html.ep

@@ -1,7 +1,7 @@
 % layout 'logbot';
 % use LogBot::Web::Util qw( url_for_channel );
 
-<% my $channel_li = begin %>
+% my $channel_li = begin
   % my ($channel, $topic) = @_;
   <li class="<%= 'archived' if $channel->{archived} %>" data-text="<%= $channel->{name} . ' ' . lc($topic) %>">
     <a href="<%= url_for_channel( channel => $channel->{name} ) %>" class="channel"><%= $channel->{name} %></a>
@@ -9,8 +9,9 @@
       <div class="topic"><%== linkify $topic %></div>
     % }
   </li>
-<% end %>
+% end
 
+%= cached 'list' => begin
 <div id="channel-list">
   <h3>Active Channels</h3>
 
@@ -45,3 +46,4 @@
     </div>
   % }
 </div>
+% end