Class: TypedCache::Snapshot
- Inherits:
-
Object
- Object
- TypedCache::Snapshot
- 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
-
#key ⇒ Object
readonly
: CacheKey.
-
#retrieved_at ⇒ Object
readonly
: Time.
-
#source ⇒ Object
readonly
: Symbol.
-
#value ⇒ Object
readonly
: V.
Class Method Summary collapse
-
.cached(key, value) ⇒ Object
Creates a snapshot for a cached value : [V] (CacheKey, V) -> Snapshot.
-
.computed(key, value) ⇒ Object
Creates a snapshot for a computed value : [V] (CacheKey, V) -> Snapshot.
-
.updated(key, value) ⇒ Object
Creates a snapshot for an updated value : [V] (CacheKey, V) -> Snapshot.
Instance Method Summary collapse
-
#age ⇒ Object
Age of the snapshot in seconds : -> Float.
-
#bind(&block) ⇒ Object
(also: #flat_map)
Bind over the value with Either error handling : [R] () { (V) -> either[Error, R] } -> either[Error, Snapshot].
-
#computed? ⇒ Boolean
Whether this value was freshly computed : -> bool.
-
#from_cache? ⇒ Boolean
Whether this value came from cache : -> bool.
-
#initialize(key, value, source:, retrieved_at: Time.now) ⇒ Snapshot
constructor
: (CacheKey, V, source: Symbol, retrieved_at: Time) -> void.
- #inspect ⇒ Object
-
#map(&block) ⇒ Object
Map over the value while preserving snapshot metadata : [R] () { (V) -> R } -> Snapshot.
- #to_s ⇒ Object
-
#updated? ⇒ Boolean
Whether this value was updated : -> bool.
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
#key ⇒ Object (readonly)
: CacheKey
7 8 9 |
# File 'lib/typed_cache/snapshot.rb', line 7 def key @key end |
#retrieved_at ⇒ Object (readonly)
: Time
9 10 11 |
# File 'lib/typed_cache/snapshot.rb', line 9 def retrieved_at @retrieved_at end |
#source ⇒ Object (readonly)
: Symbol
10 11 12 |
# File 'lib/typed_cache/snapshot.rb', line 10 def source @source end |
#value ⇒ Object (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
#age ⇒ Object
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
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
28 29 30 |
# File 'lib/typed_cache/snapshot.rb', line 28 def from_cache? source == :cache end |
#inspect ⇒ Object
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_s ⇒ Object
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
40 41 42 |
# File 'lib/typed_cache/snapshot.rb', line 40 def updated? source == :updated end |