ttl_memoizeable 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5763160f9370ac1bc7695b8299a6528953e6b90cd30806784b45763be6eae8c
4
- data.tar.gz: 04e05378018dc76ad58718a170d2212f554a8838b53d071ec9ac2943f8a2bdf3
3
+ metadata.gz: 3b581b14f0f85cb39ade5f62d67f65406da66b93c781949df441d29f0674a109
4
+ data.tar.gz: 18c9172f7a764aefd95372bfc7b4d2ed2d44b110bd1dfc3aff306bcec37be9b3
5
5
  SHA512:
6
- metadata.gz: 358dde19960ec800c5f58eddbd414a2126a866302c12c350714f355e0f29e049dadd055aa7584ba83e6e89717d5713e1f073718dc17276efb0827238340a89a4
7
- data.tar.gz: 77486fa2db3df97f9c2b68217838ad36ed75c678f8c1e67b781f2618eacaf3253406e8344aba51f973b85afed2ac4bd26c5aec1c48b13839aa53e0957e4b810c
6
+ metadata.gz: fa200db2d8e1c62773f338a8496a17e169717a1581b7922c96ce2b45359f702920aff7f9ae724cdabf9124161bbecc38c844f1233ab596afa91bb7dc3243565f
7
+ data.tar.gz: 9ed75c14082a23065875df1beee159725806f895f8da147b96c3cbd13e9ac2b5b1b230fafd47b4c169eba6f190a6e8e59bd924be3a9f2d6ba9150b30edbb9517
data/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
1
  ## [Unreleased]
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
2
5
  ## [0.3.0] - 2024-01-04
3
6
  - Bug fixes
4
7
  ## [0.2.0] - 2023-11-27
data/Gemfile CHANGED
@@ -7,4 +7,4 @@ gemspec
7
7
 
8
8
  gem "rake"
9
9
  gem "rspec"
10
- gem "standard"
10
+ gem "standard", "1.40.0"
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 two approaches, depending on your preference of flavor.
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. Conditionally TTL memoize the method based on test environment or some other condition.
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"))
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TTLMemoizeable
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -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.3.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-01-04 00:00:00.000000000 Z
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.4.1
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.