summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Legler <alex@a3li.li>2015-02-22 15:44:19 +0100
committerAlex Legler <alex@a3li.li>2015-02-22 15:44:19 +0100
commitf93dd2d194eaa5b9b23715edf6b71ecb5001d839 (patch)
tree56a9078bdb95c709f7cd307e9dd6bdce0b14f082 /lib
parentFix mail count (diff)
downloadfrontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.tar.gz
frontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.tar.bz2
frontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.zip
Cache message counts
Diffstat (limited to 'lib')
-rw-r--r--lib/cache.rb35
-rw-r--r--lib/index.rb2
2 files changed, 36 insertions, 1 deletions
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,