commit:     f93dd2d194eaa5b9b23715edf6b71ecb5001d839
Author:     Alex Legler <alex <AT> a3li <DOT> li>
AuthorDate: Sun Feb 22 14:44:19 2015 +0000
Commit:     Alex Legler <a3li <AT> gentoo <DOT> org>
CommitDate: Sun Feb 22 14:44:19 2015 +0000
URL:        
http://sources.gentoo.org/gitweb/?p=proj/ag-web.git;a=commit;h=f93dd2d1

Cache message counts

---
 ag-web.rb       |  3 +++
 lib/cache.rb    | 35 +++++++++++++++++++++++++++++++++++
 lib/index.rb    |  2 +-
 views/index.erb |  4 ++--
 4 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/ag-web.rb b/ag-web.rb
index ef326c8..8855dd7 100644
--- a/ag-web.rb
+++ b/ag-web.rb
@@ -13,6 +13,7 @@ require 'pony'
 
 require_relative 'lib/index.rb'
 require_relative 'lib/helpers.rb'
+require_relative 'lib/cache.rb'
 
 configure do
   set :partial_template_engine, :erb
@@ -24,6 +25,8 @@ $es.transport.reload_connections!
 
 $config = YAML.load_file('config.yml')
 
+MessageCountCache.instance.update!
+
 get '/:list/report/:msgid' do
   return unless list_check
 

diff --git a/lib/cache.rb b/lib/cache.rb
new file mode 100644
index 0000000..c2968d1
--- /dev/null
+++ b/lib/cache.rb
@@ -0,0 +1,35 @@
+require 'date'
+
+class MessageCountCache
+  include Singleton
+  CACHE_SECONDS = 3600
+
+  def initialize
+    update!
+  end
+
+  def update!
+    @message_counts ||= {}
+
+    @new_counts = {}
+    [$config['active_lists'], $config['frozen_lists']].flatten.each do |list|
+      @new_counts[list] = get_message_count_internal(list)
+    end
+
+    @message_counts = @new_counts
+    @load_date = DateTime.now
+  end
+
+  def [](list)
+    update?
+
+    @message_counts[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/lib/index.rb b/lib/index.rb
index 5690afe..de3d4f0 100644
--- a/lib/index.rb
+++ b/lib/index.rb
@@ -73,7 +73,7 @@ def get_month_listing(list)
   )
 end
 
-def get_message_count(list)
+def get_message_count_internal(list)
   $es.search(
     index: 'ml-' + list,
     size: 1,

diff --git a/views/index.erb b/views/index.erb
index d1454ea..c2cee2c 100644
--- a/views/index.erb
+++ b/views/index.erb
@@ -9,7 +9,7 @@
         <a href="<%= h list %>/" class="list-group-item">
           <span class="fa fa-fw fa-archive"></span>
           <%= h list %>
-          <span class="badge"><%= get_message_count(list) %></span>
+          <span class="badge"><%= MessageCountCache.instance[list] %></span>
         </a>
       <% end %>
     </div>
@@ -32,7 +32,7 @@
         <a href="<%= h list %>/" class="list-group-item">
           <span class="fa fa-fw fa-archive"></span>
           <%= h list %>
-          <span class="badge"><%= get_message_count(list) %></span>
+          <span class="badge"><%= MessageCountCache.instance[list] %></span>
         </a>
       <% end %>
     </div>

Reply via email to