Class: CacheFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/cache-flow/cache-flow.rb,
lib/cache-flow/configuration.rb

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequency = "daily", options = {}) ⇒ CacheFlow

Returns a new instance of CacheFlow.



5
6
7
8
# File 'lib/cache-flow/cache-flow.rb', line 5

def initialize(frequency = "daily", options = {})
  @options = CacheFlow.configuration.default_options.merge(options)
  @frequency = frequency
end

Class Attribute Details

.configurationObject

Configure CacheFlow in your initializers: example file location: config/initializers/cache_flow.rb example code: CacheFlow.configure do |config|

config.default_options = {
  time_zone: "Eastern Time (US & Canada)",
  hour_range_start: 17,
  hour_range_end: 20
}

end



31
32
33
# File 'lib/cache-flow/configuration.rb', line 31

def configuration
  @configuration
end

Instance Attribute Details

#frequencyObject

Returns the value of attribute frequency.



2
3
4
# File 'lib/cache-flow/cache-flow.rb', line 2

def frequency
  @frequency
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/cache-flow/cache-flow.rb', line 3

def options
  @options
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



35
36
37
# File 'lib/cache-flow/configuration.rb', line 35

def self.configure
  yield(configuration)
end

Instance Method Details

#generate_expiryObject



10
11
12
13
14
15
16
# File 'lib/cache-flow/cache-flow.rb', line 10

def generate_expiry
  # Rails :expires_in accepts seconds from now to expire the key in
  case frequency
  when "daily"
    random_time_in_range(24.hours.from_now)
  end
end

#random_time_in_range(day_to_bust) ⇒ Object



18
19
20
21
22
23
# File 'lib/cache-flow/cache-flow.rb', line 18

def random_time_in_range(day_to_bust)
  time_to_bust = day_to_bust.in_time_zone(options[:time_zone]).beginning_of_day
  range_start = time_to_bust + options[:hour_range_start].hours
  range_end = time_to_bust + options[:hour_range_end].hours
  rand(range_start.to_i..range_end.to_i) - Time.now.to_i
end