diff options
author | Alex Legler <alex@a3li.li> | 2015-02-22 15:44:19 +0100 |
---|---|---|
committer | Alex Legler <alex@a3li.li> | 2015-02-22 15:44:19 +0100 |
commit | f93dd2d194eaa5b9b23715edf6b71ecb5001d839 (patch) | |
tree | 56a9078bdb95c709f7cd307e9dd6bdce0b14f082 /lib | |
parent | Fix mail count (diff) | |
download | frontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.tar.gz frontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.tar.bz2 frontend-f93dd2d194eaa5b9b23715edf6b71ecb5001d839.zip |
Cache message counts
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache.rb | 35 | ||||
-rw-r--r-- | lib/index.rb | 2 |
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, |