Class: TRuby::CacheEntry

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

Overview

Cache entry with metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

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_atObject (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_atObject (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

#hitsObject (readonly)

Returns the value of attribute hits.



10
11
12
# File 'lib/t_ruby/cache.rb', line 10

def hits
  @hits
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/t_ruby/cache.rb', line 10

def key
  @key
end

#valueObject (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

#accessObject



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

Returns:



26
27
28
# File 'lib/t_ruby/cache.rb', line 26

def stale?(max_age)
  Time.now - @created_at > max_age
end

#to_hObject



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