From 3e986ecfc544b94f329e35ae5b771f68ed27851a Mon Sep 17 00:00:00 2001 From: Alex Young Date: Mon, 20 Apr 2015 16:14:03 +0100 Subject: Pull the bank holidays cache out to its own class --- lib/mauve/bank_holidays_cache.rb | 28 ++++++++++++++++++++++++++++ lib/mauve/server.rb | 16 +++------------- 2 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 lib/mauve/bank_holidays_cache.rb diff --git a/lib/mauve/bank_holidays_cache.rb b/lib/mauve/bank_holidays_cache.rb new file mode 100644 index 0000000..ce50663 --- /dev/null +++ b/lib/mauve/bank_holidays_cache.rb @@ -0,0 +1,28 @@ +require 'mauve/calendar_interface' + +module Mauve + + class BankHolidaysCache + def initialize + @bank_holidays = [] + @last_checked_at = nil + end + + def bank_holidays + now = Time.now + # + # Update the bank holidays list hourly. + # + if @bank_holidays.nil? or + @last_checked_at.nil? or + @last_checked_at < (now - 1.hour) + + @bank_holidays = CalendarInterface.get_bank_holiday_list(now) + @last_checked_at = now + end + + @bank_holidays + end + end # class BankHolidaysCache + +end # module Mauve diff --git a/lib/mauve/server.rb b/lib/mauve/server.rb index 90d5e77..7ce5823 100644 --- a/lib/mauve/server.rb +++ b/lib/mauve/server.rb @@ -13,6 +13,7 @@ require 'mauve/processor' require 'mauve/http_server' require 'mauve/heartbeat' require 'mauve/configuration' +require 'mauve/bank_holidays_cache' require 'log4r' module Mauve @@ -52,7 +53,7 @@ module Mauve # Bank Holidays -- this list is kept here, because I can't think of # anywhere else to put it. # - @bank_holidays = nil + @bank_holidays_cache = BankHolidaysCache.new # # Turn off unwanted reverse DNS lookups across the board. @@ -127,18 +128,7 @@ module Mauve # # def bank_holidays - # - # Update the bank holidays list hourly. - # - if @bank_holidays.nil? or - @bank_holidays_last_checked_at.nil? or - @bank_holidays_last_checked_at < (Time.now - 1.hour) - - @bank_holidays = CalendarInterface.get_bank_holiday_list(Time.now) - @bank_holidays_last_checked_at = Time.now - end - - @bank_holidays + @bank_holidays_cache.bank_holidays end # return [Log4r::Logger] -- cgit v1.2.1