ttl_memoizeable 0.2.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -2
- data/Gemfile +1 -1
- data/README.md +26 -2
- data/lib/ttl_memoizeable/version.rb +1 -1
- data/lib/ttl_memoizeable.rb +17 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b581b14f0f85cb39ade5f62d67f65406da66b93c781949df441d29f0674a109
|
4
|
+
data.tar.gz: 18c9172f7a764aefd95372bfc7b4d2ed2d44b110bd1dfc3aff306bcec37be9b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa200db2d8e1c62773f338a8496a17e169717a1581b7922c96ce2b45359f702920aff7f9ae724cdabf9124161bbecc38c844f1233ab596afa91bb7dc3243565f
|
7
|
+
data.tar.gz: 9ed75c14082a23065875df1beee159725806f895f8da147b96c3cbd13e9ac2b5b1b230fafd47b4c169eba6f190a6e8e59bd924be3a9f2d6ba9150b30edbb9517
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
## [Unreleased]
|
2
|
-
## [0.
|
3
|
-
-
|
2
|
+
## [0.4.0] - 2024-09-18
|
3
|
+
- Add support for globally disabling TTLMemoizeable (#15) @danielwestendorf
|
4
|
+
- Add support for globally resetting all ttls (#13) @danielwestendorf
|
5
|
+
## [0.3.0] - 2024-01-04
|
6
|
+
- Bug fixes
|
4
7
|
## [0.2.0] - 2023-11-27
|
5
8
|
- Publish to rubygems
|
6
9
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -151,7 +151,7 @@ application_config.config # => {...} Redis/JSON.parse will be called
|
|
151
151
|
|
152
152
|
## Testing a TTLMemoized Method
|
153
153
|
|
154
|
-
You likely don't want to test the implementation of this library, but the logic of your memoized method. In that case you probably want "fresh" data on every invocation of the method. There are
|
154
|
+
You likely don't want to test the implementation of this library, but the logic of your memoized method. In that case you probably want "fresh" data on every invocation of the method. There are a few approaches, depending on your preference of flavor.
|
155
155
|
|
156
156
|
1. Use the reset method provided for you. It follows the pattern of `reset_memoized_value_for_#{method_name}`. Note that this will only reset the value for the current thread, and shouldn't be used to try and create consistent data state across processes.
|
157
157
|
```ruby
|
@@ -162,7 +162,31 @@ def test_config
|
|
162
162
|
end
|
163
163
|
```
|
164
164
|
|
165
|
-
2.
|
165
|
+
2. Disable ttl memoization globally in your tests. This will prevent a memoized value from ever being returned.
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
TTLMemoizeable.disable!
|
169
|
+
```
|
170
|
+
|
171
|
+
3. Reset ttl memoization values before/after your test runs.
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
# RSpec
|
175
|
+
RSpec.configure do |config|
|
176
|
+
config.around { TTLMemoizeable.reset!; _1.run; TTLMemoizeable.reset! }
|
177
|
+
end
|
178
|
+
|
179
|
+
# minitest
|
180
|
+
def setup
|
181
|
+
TTLMemoizeable.reset!
|
182
|
+
end
|
183
|
+
|
184
|
+
def teardown
|
185
|
+
TTLMemoizeable.reset!
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
4. Conditionally TTL memoize the method based on test environment or some other condition.
|
166
190
|
```ruby
|
167
191
|
def config
|
168
192
|
JSON.parse($redis.get("some_big_json_string"))
|
data/lib/ttl_memoizeable.rb
CHANGED
@@ -9,6 +9,17 @@ module TTLMemoizeable
|
|
9
9
|
TTLMemoizationError = Class.new(StandardError)
|
10
10
|
SetupMutex = Mutex.new
|
11
11
|
|
12
|
+
@ttl_index = 0
|
13
|
+
@disabled = false
|
14
|
+
|
15
|
+
def self.disable!
|
16
|
+
@disabled = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.reset!
|
20
|
+
@ttl_index += 1
|
21
|
+
end
|
22
|
+
|
12
23
|
def ttl_memoized_method(method_name, ttl: 1000)
|
13
24
|
raise TTLMemoizationError, "Method not defined: #{method_name}" unless method_defined?(method_name) || private_method_defined?(method_name)
|
14
25
|
|
@@ -17,6 +28,7 @@ module TTLMemoizeable
|
|
17
28
|
expired_ttl = time_based_ttl ? 1.year.ago : 1
|
18
29
|
|
19
30
|
ttl_variable_name = :"@_ttl_for_#{ivar_name}"
|
31
|
+
ttl_index_variable_name = :"@_ttl_index_for_#{ivar_name}"
|
20
32
|
mutex_variable_name = :"@_mutex_for_#{ivar_name}"
|
21
33
|
value_variable_name = :"@_value_for_#{ivar_name}"
|
22
34
|
|
@@ -60,6 +72,8 @@ module TTLMemoizeable
|
|
60
72
|
end
|
61
73
|
|
62
74
|
define_method ttl_exceeded_method_name do
|
75
|
+
return true if TTLMemoizeable.instance_variable_get(:@disabled)
|
76
|
+
return true if TTLMemoizeable.instance_variable_get(:@ttl_index) != instance_variable_get(ttl_index_variable_name)
|
63
77
|
return true unless instance_variable_defined?(value_variable_name)
|
64
78
|
|
65
79
|
compared_to = time_based_ttl ? ttl.ago : 0
|
@@ -85,6 +99,9 @@ module TTLMemoizeable
|
|
85
99
|
if send(ttl_exceeded_method_name)
|
86
100
|
send(extend_ttl_method_name)
|
87
101
|
|
102
|
+
# Synchronize the ttl index to match the module's value
|
103
|
+
instance_variable_set(ttl_index_variable_name, TTLMemoizeable.instance_variable_get(:@ttl_index))
|
104
|
+
|
88
105
|
# Refresh value from the original method
|
89
106
|
instance_variable_set(value_variable_name, super())
|
90
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ttl_memoizeable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Westendorf
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
79
|
+
rubygems_version: 3.5.11
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: Cross-thread memoization in ruby with eventual consistency.
|