Class: Harbor::Cache::Item
- Inherits:
-
Object
- Object
- Harbor::Cache::Item
- Defined in:
- lib/harbor/cache/item.rb
Instance Attribute Summary collapse
-
#cached_at ⇒ Object
readonly
Returns the value of attribute cached_at.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#maximum_age ⇒ Object
readonly
Returns the value of attribute maximum_age.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
-
#ultimate_expiration_time ⇒ Object
readonly
Returns the value of attribute ultimate_expiration_time.
Instance Method Summary collapse
- #bump ⇒ Object
- #expired? ⇒ Boolean
- #fresh? ⇒ Boolean
-
#initialize(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil) ⇒ Item
constructor
A new instance of Item.
Constructor Details
#initialize(key, ttl, maximum_age, string_or_io, cached_at, expires_at = nil) ⇒ Item
Returns a new instance of Item.
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_at ⇒ Object (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 |
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/harbor/cache/item.rb', line 5 def content @content end |
#expires_at ⇒ Object (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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/harbor/cache/item.rb', line 5 def key @key end |
#maximum_age ⇒ Object (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 |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
5 6 7 |
# File 'lib/harbor/cache/item.rb', line 5 def ttl @ttl end |
#ultimate_expiration_time ⇒ Object (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
#bump ⇒ Object
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
36 37 38 |
# File 'lib/harbor/cache/item.rb', line 36 def expired? Time.now >= expires_at end |
#fresh? ⇒ Boolean
32 33 34 |
# File 'lib/harbor/cache/item.rb', line 32 def fresh? Time.now < expires_at end |