Class: Harbor::Cache::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/cache/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil) ⇒ Item

Returns a new instance of Item.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/harbor/cache/item.rb', line 7

def initialize(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil)
  raise ArgumentError.new("Harbor::Cache::Item#initialize expects a String value for 'key', got #{key}") unless key.is_a?(String)
  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
  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)
  raise ArgumentError.new("Harbor::Cache::Item#initialize expects a Time value for 'cached_at', got #{cached_at}") unless cached_at.is_a?(Time)
  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)

  @key = key
  @ttl = ttl
  @maximum_age = maximum_age
  @cached_at = cached_at
  @expires_at = expires_at || (cached_at + ttl)
  @ultimate_expiration_time = (maximum_age ? cached_at + maximum_age : nil)

  if string_or_io.respond_to?(:read)
    @io = string_or_io
  else
    @content = string_or_io
  end
end

Instance Attribute Details

#cached_atObject (readonly)

Returns the value of attribute cached_at.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def cached_at
  @cached_at
end

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def content
  @content
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def expires_at
  @expires_at
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def key
  @key
end

#maximum_ageObject (readonly)

Returns the value of attribute maximum_age.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def maximum_age
  @maximum_age
end

#ttlObject (readonly)

Returns the value of attribute ttl.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def ttl
  @ttl
end

#ultimate_expiration_timeObject (readonly)

Returns the value of attribute ultimate_expiration_time.



5
6
7
# File 'lib/harbor/cache/item.rb', line 5

def ultimate_expiration_time
  @ultimate_expiration_time
end

Instance Method Details

#bumpObject



40
41
42
43
44
# File 'lib/harbor/cache/item.rb', line 40

def bump
  if @ultimate_expiration_time
    @expires_at = [Time.now + ttl, @ultimate_expiration_time].min
  end
end

#expired?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/harbor/cache/item.rb', line 36

def expired?
  Time.now >= expires_at
end

#fresh?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/harbor/cache/item.rb', line 32

def fresh?
  Time.now < expires_at
end