Harbor Documentation

Harbor::Cache::Item

Attributes

  • key [R] (Not documented)
  • ttl [R] (Not documented)
  • expires_at [R] (Not documented)
  • cached_at [R] (Not documented)
  • content [R] (Not documented)
  • maximum_age [R] (Not documented)
  • ultimate_expiration_time [R] (Not documented)

Public Class Methods

new(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil)

      # File lib/harbor/cache/item.rb, line 7
 7:     def initialize(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil)
 8:       raise ArgumentError.new("Harbor::Cache::Item#initialize expects a String value for 'key', got #{key}") unless key.is_a?(String)
 9:       raise ArgumentError.new("Harbor::Cache::Item#initialize expects a Fixnum value greater than 0 for 'ttl', got #{ttl}") unless ttl.is_a?(Fixnum) && ttl > 0
10:       raise ArgumentError.new("Harbor::Cache::Item#initialize expects nil, or a Fixnum value greater than 0 for 'maximum_age', got #{maximum_age}") unless maximum_age.nil? || (maximum_age.is_a?(Fixnum) && maximum_age > 0)
11:       raise ArgumentError.new("Harbor::Cache::Item#initialize expects a Time value for 'cached_at', got #{cached_at}") unless cached_at.is_a?(Time)
12:       raise ArgumentError.new("Harbor::Cache::Item#initialize expects a maximum_age greater than the ttl, got ttl: #{ttl}, maximum_age: #{maximum_age}") if maximum_age && ttl && (maximum_age <= ttl)
13: 
14:       @key = key
15:       @ttl = ttl
16:       @maximum_age = maximum_age
17:       @cached_at = cached_at
18:       @expires_at = expires_at || (cached_at + ttl)
19:       @ultimate_expiration_time = (maximum_age ? cached_at + maximum_age : nil)
20: 
21:       if string_or_io.respond_to?(:read)
22:         @io = string_or_io
23:       else
24:         @content = string_or_io
25:       end
26:     end

Public Instance Methods

bump()

      # File lib/harbor/cache/item.rb, line 40
40:     def bump
41:       if @ultimate_expiration_time
42:         @expires_at = [Time.now + ttl, @ultimate_expiration_time].min
43:       end
44:     end

content()

      # File lib/harbor/cache/item.rb, line 28
28:     def content
29:       @content ||= @io.read
30:     end

expired?()

      # File lib/harbor/cache/item.rb, line 36
36:     def expired?
37:       Time.now >= expires_at
38:     end

fresh?()

      # File lib/harbor/cache/item.rb, line 32
32:     def fresh?
33:       Time.now < expires_at
34:     end