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_fetched = true
25:         @content = string_or_io
26:       end
27:     end

Public Instance Methods

bump()

      # File lib/harbor/cache/item.rb, line 51
51:     def bump
52:       if @ultimate_expiration_time
53:         @expires_at = [Time.now + ttl, @ultimate_expiration_time].min
54:       end
55:     end

content()

      # File lib/harbor/cache/item.rb, line 29
29:     def content
30:       read_io unless @content_fetched
31:       @content
32:     end

expired?()

      # File lib/harbor/cache/item.rb, line 47
47:     def expired?
48:       Time.now >= expires_at
49:     end

fresh?()

      # File lib/harbor/cache/item.rb, line 43
43:     def fresh?
44:       Time.now < expires_at
45:     end

read_io()

      # File lib/harbor/cache/item.rb, line 34
34:     def read_io
35:       @content_fetched = true
36:       @content = begin
37:         @io.read
38:       rescue
39:         nil
40:       end
41:     end