Class: Lutaml::Store::CacheStore::CacheEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/cache_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, ttl: nil, metadata: {}, created_at: nil) ⇒ CacheEntry

Returns a new instance of CacheEntry.



12
13
14
15
16
17
# File 'lib/lutaml/store/cache_store.rb', line 12

def initialize(value, ttl: nil, metadata: {}, created_at: nil)
  @value = value
  @created_at = created_at || Time.now
  @ttl = ttl
  @metadata = 
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/lutaml/store/cache_store.rb', line 10

def created_at
  @created_at
end

#metadataObject (readonly)

Returns the value of attribute metadata.



10
11
12
# File 'lib/lutaml/store/cache_store.rb', line 10

def 
  @metadata
end

#ttlObject (readonly)

Returns the value of attribute ttl.



10
11
12
# File 'lib/lutaml/store/cache_store.rb', line 10

def ttl
  @ttl
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/lutaml/store/cache_store.rb', line 10

def value
  @value
end

Class Method Details

.from_h(hash) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/lutaml/store/cache_store.rb', line 41

def self.from_h(hash)
  new(
    hash[:value],
    ttl: hash[:ttl],
    metadata: hash[:metadata] || {},
    created_at: Time.parse(hash[:created_at])
  )
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/lutaml/store/cache_store.rb', line 19

def expired?
  return false unless @ttl

  Time.now - @created_at > @ttl
end

#expires_atObject



25
26
27
28
29
# File 'lib/lutaml/store/cache_store.rb', line 25

def expires_at
  return nil unless @ttl

  @created_at + @ttl
end

#to_hObject



31
32
33
34
35
36
37
38
39
# File 'lib/lutaml/store/cache_store.rb', line 31

def to_h
  {
    value: @value,
    created_at: @created_at.iso8601,
    ttl: @ttl,
    expires_at: expires_at&.iso8601,
    metadata: @metadata
  }
end