summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Legler <alex@a3li.li>2015-02-25 11:44:29 +0100
committerAlex Legler <alex@a3li.li>2015-02-25 11:44:29 +0100
commit81bc7df02d522a4a43dd73fce4e95b4fec765296 (patch)
tree2ac7d4d5b4ed0c28cb5c60ba253fb214380cb731
parentBetter email escape (diff)
downloadfrontend-81bc7df02d522a4a43dd73fce4e95b4fec765296.tar.gz
frontend-81bc7df02d522a4a43dd73fce4e95b4fec765296.tar.bz2
frontend-81bc7df02d522a4a43dd73fce4e95b4fec765296.zip
Cache most recent messages
-rw-r--r--ag-web.rb1
-rw-r--r--lib/cache.rb34
-rw-r--r--views/indexentry.erb2
3 files changed, 36 insertions, 1 deletions
diff --git a/ag-web.rb b/ag-web.rb
index fd11ba4..bdac32b 100644
--- a/ag-web.rb
+++ b/ag-web.rb
@@ -26,6 +26,7 @@ $es.transport.reload_connections!
$config = YAML.load_file('config.yml')
MessageCountCache.instance.update!
+MostRecentMessagesCache.instance.update!
get '/:list/report/:msgid' do
return unless list_check
diff --git a/lib/cache.rb b/lib/cache.rb
index c2968d1..f635114 100644
--- a/lib/cache.rb
+++ b/lib/cache.rb
@@ -32,4 +32,38 @@ class MessageCountCache
update!
end
end
+end
+
+class MostRecentMessagesCache
+ include Singleton
+ CACHE_SECONDS=3600
+
+ def initialize
+ update!
+ end
+
+ def update!
+ @messages ||= {}
+
+ @new_messages = {}
+ [$config['active_lists'], $config['frozen_lists']].flatten.each do |list|
+ @new_messages[list] = most_recent(list, 5)
+ end
+
+ @messages = @new_messages
+ @load_date = DateTime.now
+ end
+
+ def [](list)
+ update?
+
+ @messages[list] || []
+ end
+
+ private
+ def update?
+ if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS
+ update!
+ end
+ end
end \ No newline at end of file
diff --git a/views/indexentry.erb b/views/indexentry.erb
index 6028ec9..ba6a2cf 100644
--- a/views/indexentry.erb
+++ b/views/indexentry.erb
@@ -18,7 +18,7 @@
<th>Subject</th>
<th class="ag-mostrecent-table-author">Author</th>
</tr>
- <% most_recent(list, 5).each do |message| %>
+ <% MostRecentMessagesCache.instance[list].each do |message| %>
<tr>
<td><a href="/<%= list %>/message/<%= message['_id'] %>"><%= h message['_source']['subject'] %></a></td>
<td><%= h message['_source']['from_realname'] %></td>