Class: Alephant::Broker::Cache::CachedObject

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/alephant/broker/cache/cached_object.rb

Constant Summary collapse

DEFAULT_TTL =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ CachedObject

Returns a new instance of CachedObject.



13
14
15
16
17
18
19
# File 'lib/alephant/broker/cache/cached_object.rb', line 13

def initialize(obj)
  logger.info(event:   'SettingCachedObject',
              content: obj,
              method:  "#{self.class}#initialize")

  @s3_obj = obj
end

Instance Attribute Details

#s3_objObject (readonly)

Returns the value of attribute s3_obj.



9
10
11
# File 'lib/alephant/broker/cache/cached_object.rb', line 9

def s3_obj
  @s3_obj
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/alephant/broker/cache/cached_object.rb', line 45

def expired?
  result = (updated + ttl) < Time.now

  logger.info(event:            'Expired?',
              updated:          updated,
              ttl:              ttl,
              updated_plus_ttl: (updated + ttl),
              now:              Time.now,
              result:           result,
              method:           "#{self.class}#expired?")

  result
end

#to_h(obj = nil) ⇒ Object



59
60
61
# File 'lib/alephant/broker/cache/cached_object.rb', line 59

def to_h(obj = nil)
  obj || s3_obj
end

#ttlObject



38
39
40
41
42
43
# File 'lib/alephant/broker/cache/cached_object.rb', line 38

def ttl
  Integer([:ttl] || ['ttl'])
rescue TypeError => error
  logger.info(event: 'ErrorCaught', method: "#{self.class}#ttl", error: error)
  Integer(Broker.config[:revalidate_cache_ttl] || DEFAULT_TTL)
end

#update(obj) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/alephant/broker/cache/cached_object.rb', line 21

def update(obj)
  logger.info(event:       'UpdatingCachedObject',
              old_content: @s3_obj,
              new_content: obj,
              method:      "#{self.class}#update")

  @s3_obj = obj
end

#updatedObject



30
31
32
33
34
35
36
# File 'lib/alephant/broker/cache/cached_object.rb', line 30

def updated
  time = [:'head_Last-Modified']
  Time.parse(time)
rescue TypeError, ArgumentError => error
  logger.info(event: 'ErrorCaught', method: "#{self.class}#updated", error: error)
  Time.now
end