diff options
-rw-r--r-- | ag-web.rb | 1 | ||||
-rw-r--r-- | lib/cache.rb | 34 | ||||
-rw-r--r-- | views/indexentry.erb | 2 |
3 files changed, 36 insertions, 1 deletions
@@ -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> |