typed_cache 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +25 -3
- data/examples.md +36 -2
- data/lib/typed_cache/backends/active_support.rb +50 -5
- data/lib/typed_cache/backends/memory.rb +5 -5
- data/lib/typed_cache/cache_key.rb +9 -1
- data/lib/typed_cache/cache_ref.rb +16 -16
- data/lib/typed_cache/decorator.rb +9 -1
- data/lib/typed_cache/decorators/instrumented.rb +21 -8
- data/lib/typed_cache/either.rb +22 -0
- data/lib/typed_cache/instrumenter.rb +6 -6
- data/lib/typed_cache/instrumenters/mixins/namespaced_singleton.rb +3 -0
- data/lib/typed_cache/instrumenters.rb +2 -1
- data/lib/typed_cache/maybe.rb +18 -0
- data/lib/typed_cache/namespace.rb +33 -6
- data/lib/typed_cache/railtie.rb +15 -0
- data/lib/typed_cache/snapshot.rb +18 -10
- data/lib/typed_cache/store.rb +47 -15
- data/lib/typed_cache/version.rb +1 -1
- data/lib/typed_cache.rb +4 -0
- data/rbi/typed_cache/backend.rbi +9 -0
- data/rbi/typed_cache/backends/active_support.rbi +13 -0
- data/rbi/typed_cache/backends/memory.rbi +13 -0
- data/rbi/typed_cache/backends.rbi +19 -0
- data/rbi/typed_cache/cache_builder.rbi +23 -0
- data/rbi/typed_cache/cache_key.rbi +16 -0
- data/rbi/typed_cache/cache_ref.rbi +56 -0
- data/rbi/typed_cache/decorator.rbi +67 -0
- data/rbi/typed_cache/decorators/instrumented.rbi +13 -0
- data/rbi/typed_cache/decorators.rbi +19 -0
- data/rbi/typed_cache/either.rbi +122 -0
- data/rbi/typed_cache/errors.rbi +20 -0
- data/rbi/typed_cache/instrumenter.rbi +45 -0
- data/rbi/typed_cache/instrumenters/mixins/namedspaced_singleton.rbi +33 -0
- data/rbi/typed_cache/instrumenters.rbi +19 -0
- data/rbi/typed_cache/maybe.rbi +108 -0
- data/rbi/typed_cache/namespace.rbi +30 -0
- data/rbi/typed_cache/snapshot.rbi +54 -0
- data/rbi/typed_cache/store.rbi +71 -0
- data/rbi/typed_cache/version.rbi +5 -0
- data/rbi/typed_cache.rbi +49 -0
- data/sig/generated/typed_cache/backends/active_support.rbs +14 -2
- data/sig/generated/typed_cache/backends/memory.rbs +2 -2
- data/sig/generated/typed_cache/cache_key.rbs +5 -0
- data/sig/generated/typed_cache/cache_ref.rbs +4 -4
- data/sig/generated/typed_cache/decorator.rbs +4 -0
- data/sig/generated/typed_cache/decorators/instrumented.rbs +4 -4
- data/sig/generated/typed_cache/either.rbs +24 -0
- data/sig/generated/typed_cache/instrumenter.rbs +5 -5
- data/sig/generated/typed_cache/instrumenters/mixins/namespaced_singleton.rbs +3 -0
- data/sig/generated/typed_cache/instrumenters.rbs +2 -0
- data/sig/generated/typed_cache/maybe.rbs +20 -0
- data/sig/generated/typed_cache/namespace.rbs +24 -3
- data/sig/generated/typed_cache/railtie.rbs +6 -0
- data/sig/generated/typed_cache/snapshot.rbs +12 -6
- data/sig/generated/typed_cache/store.rbs +23 -8
- data/sig/generated/typed_cache.rbs +2 -0
- data/typed_cache.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +26 -3
- metadata.gz.sig +0 -0
@@ -27,6 +27,10 @@ module TypedCache
|
|
27
27
|
|
28
28
|
def bind: [T] () { (V) -> maybe[T] } -> maybe[T]
|
29
29
|
|
30
|
+
def value_or: [T] (T) -> T
|
31
|
+
|
32
|
+
def value_or_raise!: () -> V
|
33
|
+
|
30
34
|
alias flat_map bind
|
31
35
|
end
|
32
36
|
|
@@ -55,6 +59,14 @@ module TypedCache
|
|
55
59
|
# : [T] () { (V) -> maybe[T] } -> maybe[T]
|
56
60
|
def bind: ...
|
57
61
|
|
62
|
+
# @rbs override
|
63
|
+
# : [T] (T) -> T
|
64
|
+
def value_or: ...
|
65
|
+
|
66
|
+
# @rbs override
|
67
|
+
# : -> V
|
68
|
+
def value_or_raise!: ...
|
69
|
+
|
58
70
|
alias flat_map bind
|
59
71
|
|
60
72
|
# : (Array[top]) -> ({ value: V })
|
@@ -81,5 +93,13 @@ module TypedCache
|
|
81
93
|
def bind: ...
|
82
94
|
|
83
95
|
alias flat_map bind
|
96
|
+
|
97
|
+
# @rbs override
|
98
|
+
# : [T] (T) -> T
|
99
|
+
def value_or: ...
|
100
|
+
|
101
|
+
# @rbs override
|
102
|
+
# : -> V
|
103
|
+
def value_or_raise!: ...
|
84
104
|
end
|
85
105
|
end
|
@@ -19,15 +19,16 @@ module TypedCache
|
|
19
19
|
# Returns a new Namespace instance rooted at the given namespace string.
|
20
20
|
#
|
21
21
|
# @param namespace [String] the root namespace
|
22
|
+
# @param namespaces [Array<String>] additional namespaces to join
|
22
23
|
# @return [Namespace] a new Namespace instance at the given root
|
23
24
|
#
|
24
25
|
# Example:
|
25
|
-
# TypedCache::Namespace.at("users") # => #<TypedCache::Namespace namespace=users>
|
26
|
+
# TypedCache::Namespace.at("users", "sessions") # => #<TypedCache::Namespace namespace=users:sessions>
|
26
27
|
#
|
27
28
|
# The returned Namespace can be further nested or used to generate cache keys.
|
28
29
|
#
|
29
|
-
# @rbs (String) -> Namespace
|
30
|
-
def self.at: (String) -> Namespace
|
30
|
+
# @rbs (String, *String) -> Namespace
|
31
|
+
def self.at: (String, *String) -> Namespace
|
31
32
|
|
32
33
|
# Returns the root Namespace instance (with an empty namespace).
|
33
34
|
#
|
@@ -69,6 +70,21 @@ module TypedCache
|
|
69
70
|
# @rbs (String) ?{ (Namespace, String) -> CacheKey } -> Namespace
|
70
71
|
def nested: (String) ?{ (Namespace, String) -> CacheKey } -> Namespace
|
71
72
|
|
73
|
+
# Creates a new namespace by joining the current namespace with the given namespaces.
|
74
|
+
#
|
75
|
+
# @param namespaces [Array<String>] the namespaces to join
|
76
|
+
# @param key_factory [Proc, nil] optional custom key factory for the joined namespace
|
77
|
+
# @return [Namespace] a new Namespace instance with the combined namespace
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
# ns = Namespace.at("users")
|
81
|
+
# ns.join("sessions", "admin") # => #<TypedCache::Namespace namespace=users:sessions:admin>
|
82
|
+
#
|
83
|
+
# If no key_factory is provided, the parent's key factory is inherited.
|
84
|
+
#
|
85
|
+
# @rbs (*String) ?{ (Namespace, String) -> CacheKey } -> Namespace
|
86
|
+
def join: (*String) ?{ (Namespace, String) -> CacheKey } -> Namespace
|
87
|
+
|
72
88
|
# Returns the parent namespace by removing the last namespace segment.
|
73
89
|
#
|
74
90
|
# @return [Namespace] the parent namespace, or self if already at root
|
@@ -126,5 +142,10 @@ module TypedCache
|
|
126
142
|
def ==: (Object) -> bool
|
127
143
|
|
128
144
|
alias eql? ==
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
# @rbs (String) -> String
|
149
|
+
def delimiter: (String) -> String
|
129
150
|
end
|
130
151
|
end
|
@@ -4,14 +4,16 @@ module TypedCache
|
|
4
4
|
# Immutable snapshot of a cached value with metadata about its source and age
|
5
5
|
# @rbs generic V
|
6
6
|
class Snapshot[V]
|
7
|
+
attr_reader key: CacheKey
|
8
|
+
|
7
9
|
attr_reader value: V
|
8
10
|
|
9
11
|
attr_reader retrieved_at: Time
|
10
12
|
|
11
13
|
attr_reader source: Symbol
|
12
14
|
|
13
|
-
# : (V, source: Symbol, retrieved_at: Time) -> void
|
14
|
-
def initialize: (V, source: Symbol, retrieved_at: Time) -> void
|
15
|
+
# : (CacheKey, V, source: Symbol, retrieved_at: Time) -> void
|
16
|
+
def initialize: (CacheKey, V, source: Symbol, retrieved_at: Time) -> void
|
15
17
|
|
16
18
|
# Age of the snapshot in seconds
|
17
19
|
# : -> Float
|
@@ -39,12 +41,16 @@ module TypedCache
|
|
39
41
|
|
40
42
|
alias flat_map bind
|
41
43
|
|
44
|
+
# Creates a snapshot for a cached value
|
45
|
+
# : [V] (CacheKey, V) -> Snapshot[V]
|
46
|
+
def self.cached: [V] (CacheKey, V) -> Snapshot[V]
|
47
|
+
|
42
48
|
# Creates a snapshot for a computed value
|
43
|
-
# : [V] (V) -> Snapshot[V]
|
44
|
-
def self.computed: [V] (V) -> Snapshot[V]
|
49
|
+
# : [V] (CacheKey, V) -> Snapshot[V]
|
50
|
+
def self.computed: [V] (CacheKey, V) -> Snapshot[V]
|
45
51
|
|
46
52
|
# Creates a snapshot for an updated value
|
47
|
-
# : [V] (V) -> Snapshot[V]
|
48
|
-
def self.updated: [V] (V) -> Snapshot[V]
|
53
|
+
# : [V] (CacheKey, V) -> Snapshot[V]
|
54
|
+
def self.updated: [V] (CacheKey, V) -> Snapshot[V]
|
49
55
|
end
|
50
56
|
end
|
@@ -13,11 +13,15 @@ module TypedCache
|
|
13
13
|
type cache_key = String | CacheKey
|
14
14
|
|
15
15
|
interface _Store[V]
|
16
|
-
def
|
16
|
+
def read: (cache_key) -> either[Error, Snapshot[V]]
|
17
|
+
|
18
|
+
def read_all: (Array[cache_key]) -> either[Error, Array[Snapshot[V]]]
|
17
19
|
|
18
20
|
def ref: (cache_key) -> CacheRef[V]
|
19
21
|
|
20
|
-
def
|
22
|
+
def write: (cache_key, V) -> either[Error, Snapshot[V]]
|
23
|
+
|
24
|
+
def write_all: (Hash[cache_key, V]) -> either[Error, Array[Snapshot[V]]]
|
21
25
|
|
22
26
|
def delete: (cache_key) -> either[Error, Snapshot[V]]
|
23
27
|
|
@@ -25,7 +29,9 @@ module TypedCache
|
|
25
29
|
|
26
30
|
def clear: () -> maybe[Error]
|
27
31
|
|
28
|
-
def fetch: (cache_key) { () -> V } -> either[Error, Snapshot[V]]
|
32
|
+
def fetch: (cache_key) { () -> V? } -> either[Error, Snapshot[maybe[V]]]
|
33
|
+
|
34
|
+
def fetch_all: (Array[cache_key]) { (CacheKey) -> V? } -> either[Error, Array[Snapshot[V]]]
|
29
35
|
|
30
36
|
def namespace: () -> Namespace
|
31
37
|
|
@@ -45,7 +51,10 @@ module TypedCache
|
|
45
51
|
|
46
52
|
# Retrieves a value from the cache
|
47
53
|
# @rbs (cache_key) -> either[Error, Snapshot[V]]
|
48
|
-
def
|
54
|
+
def read: (cache_key) -> either[Error, Snapshot[V]]
|
55
|
+
|
56
|
+
# @rbs (Array[cache_key]) -> either[Error, Hash[cache_key, Snapshot[V]]]
|
57
|
+
def read_all: (Array[cache_key]) -> either[Error, Hash[cache_key, Snapshot[V]]]
|
49
58
|
|
50
59
|
# Retrieves a cache reference for a key
|
51
60
|
# @rbs (cache_key) -> CacheRef[V]
|
@@ -53,7 +62,10 @@ module TypedCache
|
|
53
62
|
|
54
63
|
# Stores a value in the cache
|
55
64
|
# @rbs (cache_key, V) -> either[Error, Snapshot[V]]
|
56
|
-
def
|
65
|
+
def write: (cache_key, V) -> either[Error, Snapshot[V]]
|
66
|
+
|
67
|
+
# @rbs (Hash[cache_key, V]) -> either[Error, Hash[cache_key, Snapshot[V]]]
|
68
|
+
def write_all: (Hash[cache_key, V]) -> either[Error, Hash[cache_key, Snapshot[V]]]
|
57
69
|
|
58
70
|
# Removes a value from the cache, returning the removed value
|
59
71
|
# @rbs (cache_key) -> either[Error, Snapshot[V]]
|
@@ -68,10 +80,13 @@ module TypedCache
|
|
68
80
|
def clear: () -> maybe[Error]
|
69
81
|
|
70
82
|
# Fetches a value from cache, computing and storing it if not found
|
71
|
-
# This is an atomic operation that combines
|
83
|
+
# This is an atomic operation that combines read and write
|
72
84
|
# @rbs (cache_key) { () -> V } -> either[Error, Snapshot[V]]
|
73
85
|
def fetch: (cache_key) { () -> V } -> either[Error, Snapshot[V]]
|
74
86
|
|
87
|
+
# @rbs (Array[cache_key]) { (CacheKey) -> V } -> either[Error, Array[Snapshot[V]]]
|
88
|
+
def fetch_all: (Array[cache_key]) { (CacheKey) -> V } -> either[Error, Array[Snapshot[V]]]
|
89
|
+
|
75
90
|
# @rbs () -> Instrumenter
|
76
91
|
def instrumenter: () -> Instrumenter
|
77
92
|
|
@@ -81,8 +96,8 @@ module TypedCache
|
|
81
96
|
|
82
97
|
# Accepts a String segment or a fully-formed Namespace and returns a cloned
|
83
98
|
# store scoped to that namespace.
|
84
|
-
#
|
85
|
-
def with_namespace: (Namespace | String) -> Store[V]
|
99
|
+
# : (Namespace | String | Array[String]) -> Store[V]
|
100
|
+
def with_namespace: (Namespace | String | Array[String]) -> Store[V]
|
86
101
|
|
87
102
|
# Returns the store type identifier for instrumentation/debugging
|
88
103
|
# @rbs () -> String
|
data/typed_cache.gemspec
CHANGED
@@ -39,5 +39,5 @@ Gem::Specification.new do |spec|
|
|
39
39
|
spec.add_dependency('multi_json', '~> 1.17')
|
40
40
|
|
41
41
|
spec.extra_rdoc_files = Dir['README*', 'LICENSE*', 'examples*']
|
42
|
-
spec.files = Dir['*.gemspec', 'lib/**/*', 'sig/**/*']
|
42
|
+
spec.files = Dir['*.gemspec', 'lib/**/*', 'sig/**/*', 'rbi/**/*']
|
43
43
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Autumn Winter
|
@@ -35,7 +35,7 @@ cert_chain:
|
|
35
35
|
uuhUq525FXA3gYeLAfnCSHNm7D1H7whsXYd3z+gKvEhSEQPb3mSBQ+31TDFmA5k7
|
36
36
|
q7YL3dwhxKPXtnAboI+30XBImpdizFG9Nyqgzgj/kD0QwVzL0QsVdVWnNTJXrm4n
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date: 2025-08-
|
38
|
+
date: 2025-08-16 00:00:00.000000000 Z
|
39
39
|
dependencies:
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: concurrent-ruby
|
@@ -159,10 +159,32 @@ files:
|
|
159
159
|
- lib/typed_cache/instrumenters/null.rb
|
160
160
|
- lib/typed_cache/maybe.rb
|
161
161
|
- lib/typed_cache/namespace.rb
|
162
|
+
- lib/typed_cache/railtie.rb
|
162
163
|
- lib/typed_cache/registry.rb
|
163
164
|
- lib/typed_cache/snapshot.rb
|
164
165
|
- lib/typed_cache/store.rb
|
165
166
|
- lib/typed_cache/version.rb
|
167
|
+
- rbi/typed_cache.rbi
|
168
|
+
- rbi/typed_cache/backend.rbi
|
169
|
+
- rbi/typed_cache/backends.rbi
|
170
|
+
- rbi/typed_cache/backends/active_support.rbi
|
171
|
+
- rbi/typed_cache/backends/memory.rbi
|
172
|
+
- rbi/typed_cache/cache_builder.rbi
|
173
|
+
- rbi/typed_cache/cache_key.rbi
|
174
|
+
- rbi/typed_cache/cache_ref.rbi
|
175
|
+
- rbi/typed_cache/decorator.rbi
|
176
|
+
- rbi/typed_cache/decorators.rbi
|
177
|
+
- rbi/typed_cache/decorators/instrumented.rbi
|
178
|
+
- rbi/typed_cache/either.rbi
|
179
|
+
- rbi/typed_cache/errors.rbi
|
180
|
+
- rbi/typed_cache/instrumenter.rbi
|
181
|
+
- rbi/typed_cache/instrumenters.rbi
|
182
|
+
- rbi/typed_cache/instrumenters/mixins/namedspaced_singleton.rbi
|
183
|
+
- rbi/typed_cache/maybe.rbi
|
184
|
+
- rbi/typed_cache/namespace.rbi
|
185
|
+
- rbi/typed_cache/snapshot.rbi
|
186
|
+
- rbi/typed_cache/store.rbi
|
187
|
+
- rbi/typed_cache/version.rbi
|
166
188
|
- sig/generated/typed_cache.rbs
|
167
189
|
- sig/generated/typed_cache/backend.rbs
|
168
190
|
- sig/generated/typed_cache/backends.rbs
|
@@ -186,6 +208,7 @@ files:
|
|
186
208
|
- sig/generated/typed_cache/instrumenters/null.rbs
|
187
209
|
- sig/generated/typed_cache/maybe.rbs
|
188
210
|
- sig/generated/typed_cache/namespace.rbs
|
211
|
+
- sig/generated/typed_cache/railtie.rbs
|
189
212
|
- sig/generated/typed_cache/registry.rbs
|
190
213
|
- sig/generated/typed_cache/snapshot.rbs
|
191
214
|
- sig/generated/typed_cache/store.rbs
|
@@ -197,7 +220,7 @@ licenses:
|
|
197
220
|
- Apache-2.0
|
198
221
|
metadata:
|
199
222
|
issue_tracker_uri: https://github.com/glossawy/typed_cache/issues
|
200
|
-
changelog_uri: https://github.com/glossawy/typed_cache/blob/main/VERSIONS.adoc#
|
223
|
+
changelog_uri: https://github.com/glossawy/typed_cache/blob/main/VERSIONS.adoc#030
|
201
224
|
license_uri: https://github.com/glossawy/typed_cache/blob/main/LICENSE
|
202
225
|
label: caching
|
203
226
|
labels: typed_cache,ruby,caching,type-safety,rails,rbs
|
metadata.gz.sig
CHANGED
Binary file
|