Class: TypedCache::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_cache/snapshot.rb

Overview

Immutable snapshot of a cached value with metadata about its source and age

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, source:, retrieved_at: Time.now) ⇒ Snapshot

: (CacheKey, V, source: Symbol, retrieved_at: Time) -> void



13
14
15
16
17
18
# File 'lib/typed_cache/snapshot.rb', line 13

def initialize(key, value, source:, retrieved_at: Time.now)
  @key = key
  @value = value
  @retrieved_at = retrieved_at
  @source = source
end

Instance Attribute Details

#keyObject (readonly)

: CacheKey



7
8
9
# File 'lib/typed_cache/snapshot.rb', line 7

def key
  @key
end

#retrieved_atObject (readonly)

: Time



9
10
11
# File 'lib/typed_cache/snapshot.rb', line 9

def retrieved_at
  @retrieved_at
end

#sourceObject (readonly)

: Symbol



10
11
12
# File 'lib/typed_cache/snapshot.rb', line 10

def source
  @source
end

#valueObject (readonly)

: V



8
9
10
# File 'lib/typed_cache/snapshot.rb', line 8

def value
  @value
end

Class Method Details

.cached(key, value) ⇒ Object

Creates a snapshot for a cached value : [V] (CacheKey, V) -> Snapshot



69
70
71
# File 'lib/typed_cache/snapshot.rb', line 69

def cached(key, value)
  new(key, value, source: :cache)
end

.computed(key, value) ⇒ Object

Creates a snapshot for a computed value : [V] (CacheKey, V) -> Snapshot



75
76
77
# File 'lib/typed_cache/snapshot.rb', line 75

def computed(key, value)
  new(key, value, source: :computed)
end

.updated(key, value) ⇒ Object

Creates a snapshot for an updated value : [V] (CacheKey, V) -> Snapshot



81
82
83
# File 'lib/typed_cache/snapshot.rb', line 81

def updated(key, value)
  new(key, value, source: :updated)
end

Instance Method Details

#ageObject

Age of the snapshot in seconds : -> Float



22
23
24
# File 'lib/typed_cache/snapshot.rb', line 22

def age
  Time.now - retrieved_at
end

#bind(&block) ⇒ Object Also known as: flat_map

Bind over the value with Either error handling : [R] () { (V) -> either[Error, R] } -> either[Error, Snapshot]



53
54
55
56
# File 'lib/typed_cache/snapshot.rb', line 53

def bind(&block)
  result = yield(value)
  result.map { |new_value| Snapshot.new(key, new_value, source: source, retrieved_at: retrieved_at) }
end

#computed?Boolean

Whether this value was freshly computed : -> bool

Returns:

  • (Boolean)


34
35
36
# File 'lib/typed_cache/snapshot.rb', line 34

def computed?
  source == :computed
end

#from_cache?Boolean

Whether this value came from cache : -> bool

Returns:

  • (Boolean)


28
29
30
# File 'lib/typed_cache/snapshot.rb', line 28

def from_cache?
  source == :cache
end

#inspectObject



64
# File 'lib/typed_cache/snapshot.rb', line 64

def inspect = "Snapshot(#{key}, #{value.inspect}, #{source}, #{retrieved_at})"

#map(&block) ⇒ Object

Map over the value while preserving snapshot metadata : [R] () { (V) -> R } -> Snapshot



46
47
48
49
# File 'lib/typed_cache/snapshot.rb', line 46

def map(&block)
  new_value = yield(value)
  Snapshot.new(key, new_value, source: source, retrieved_at: retrieved_at)
end

#to_sObject



61
# File 'lib/typed_cache/snapshot.rb', line 61

def to_s = "Snapshot(#{key} retrieved at #{retrieved_at} from #{source} with value #{value})"

#updated?Boolean

Whether this value was updated : -> bool

Returns:

  • (Boolean)


40
41
42
# File 'lib/typed_cache/snapshot.rb', line 40

def updated?
  source == :updated
end