Class: TRuby::CacheEntry
- Inherits:
-
Object
- Object
- TRuby::CacheEntry
- Defined in:
- lib/t_ruby/cache.rb
Overview
Cache entry with metadata
Instance Attribute Summary collapse
-
#accessed_at ⇒ Object
readonly
Returns the value of attribute accessed_at.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#hits ⇒ Object
readonly
Returns the value of attribute hits.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #access ⇒ Object
-
#initialize(key, value) ⇒ CacheEntry
constructor
A new instance of CacheEntry.
- #stale?(max_age) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(key, value) ⇒ CacheEntry
Returns a new instance of CacheEntry.
12 13 14 15 16 17 18 |
# File 'lib/t_ruby/cache.rb', line 12 def initialize(key, value) @key = key @value = value @created_at = Time.now @accessed_at = Time.now @hits = 0 end |
Instance Attribute Details
#accessed_at ⇒ Object (readonly)
Returns the value of attribute accessed_at.
10 11 12 |
# File 'lib/t_ruby/cache.rb', line 10 def accessed_at @accessed_at end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
10 11 12 |
# File 'lib/t_ruby/cache.rb', line 10 def created_at @created_at end |
#hits ⇒ Object (readonly)
Returns the value of attribute hits.
10 11 12 |
# File 'lib/t_ruby/cache.rb', line 10 def hits @hits end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/t_ruby/cache.rb', line 10 def key @key end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
10 11 12 |
# File 'lib/t_ruby/cache.rb', line 10 def value @value end |
Instance Method Details
#access ⇒ Object
20 21 22 23 24 |
# File 'lib/t_ruby/cache.rb', line 20 def access @accessed_at = Time.now @hits += 1 @value end |
#stale?(max_age) ⇒ Boolean
26 27 28 |
# File 'lib/t_ruby/cache.rb', line 26 def stale?(max_age) Time.now - @created_at > max_age end |
#to_h ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/t_ruby/cache.rb', line 30 def to_h { key: @key, value: @value, created_at: @created_at.to_i, hits: @hits } end |