ttl_memoizeable 0.2.0 → 0.3.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: e905652905199d704403098c3e9ac0ff4c6e405cd1a888a949134a434a030856
4
- data.tar.gz: b21c2af2bddddbcb19e83887c3d0f20e482d80e53835b92ea406d3e0f5ae54e6
3
+ metadata.gz: b5763160f9370ac1bc7695b8299a6528953e6b90cd30806784b45763be6eae8c
4
+ data.tar.gz: 04e05378018dc76ad58718a170d2212f554a8838b53d071ec9ac2943f8a2bdf3
5
5
  SHA512:
6
- metadata.gz: 37c2e2d28b4c4b3f5ffaa8143228db8f5063353dbcdb65c0265ef856bc6b473661069b10e183965db23dd8e1cc92dca978b8517e1f99bdb5f34c4809d228fcc2
7
- data.tar.gz: c73e41dee8ac3a01a8ede3c8783dc7424739fa489abfa4a661c9a72b126b70cb92e52f93fee7de773525aeede26460cd287198e22ace5b269b148ed77744873e
6
+ metadata.gz: 358dde19960ec800c5f58eddbd414a2126a866302c12c350714f355e0f29e049dadd055aa7584ba83e6e89717d5713e1f073718dc17276efb0827238340a89a4
7
+ data.tar.gz: 77486fa2db3df97f9c2b68217838ad36ed75c678f8c1e67b781f2618eacaf3253406e8344aba51f973b85afed2ac4bd26c5aec1c48b13839aa53e0957e4b810c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,6 @@
1
1
  ## [Unreleased]
2
+ ## [0.3.0] - 2024-01-04
3
+ - Bug fixes
2
4
  ## [0.2.0] - 2023-11-27
3
5
  - Publish to rubygems
4
6
 
data/README.md CHANGED
@@ -81,15 +81,16 @@ ttl_memoized: 0.008847 0.000755 0.009602 ( 0.221783)
81
81
 
82
82
  ### TTL Types:
83
83
  Two methods of TTL expiration are available
84
- 1. Time Duration (i.e `5.minutes`). This will ensure the process will cache your method
85
- for that given amount of time. This option is likely best when you can quantify the
86
- acceptable threshold for stale data. Every time the memoized method is called, the date
87
- the current memoized value was fetched + your ttl value will be compared to the current time.
88
84
 
89
- 2. Accessor count (i.e. 10_000). This will ensure the process will cache your method
90
- for that number of attempts to access the data. This option is likely best when you
91
- want to TTL to expire based of volume. Every time the memoized method is called, the counter
92
- will decrement by 1.
85
+ 1. Time Duration (i.e `5.minutes`). This will ensure the process will cache your method
86
+ for that given amount of time. This option is likely best when you can quantify the
87
+ acceptable threshold for stale data. Every time the memoized method is called, the date
88
+ the current memoized value was fetched + your ttl value will be compared to the current time.
89
+
90
+ 2. Accessor count (i.e. 10_000). This will ensure the process will cache your method
91
+ for that number of attempts to access the data. This option is likely best when you
92
+ want to TTL to expire based of volume. Every time the memoized method is called, the counter
93
+ will decrement by 1.
93
94
 
94
95
 
95
96
  ### Dont's
@@ -98,6 +99,8 @@ ttl_memoized: 0.008847 0.000755 0.009602 ( 0.221783)
98
99
  2. Use this library on methods that accept parameters, as that introduces state; see above
99
100
 
100
101
 
102
+ ## How to use
103
+
101
104
  Using this library is most effective on class methods.
102
105
 
103
106
  ```ruby
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TTLMemoizeable
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -7,6 +7,7 @@ require_relative "ttl_memoizeable/version"
7
7
 
8
8
  module TTLMemoizeable
9
9
  TTLMemoizationError = Class.new(StandardError)
10
+ SetupMutex = Mutex.new
10
11
 
11
12
  def ttl_memoized_method(method_name, ttl: 1000)
12
13
  raise TTLMemoizationError, "Method not defined: #{method_name}" unless method_defined?(method_name) || private_method_defined?(method_name)
@@ -15,15 +16,15 @@ module TTLMemoizeable
15
16
  time_based_ttl = ttl.is_a?(ActiveSupport::Duration)
16
17
  expired_ttl = time_based_ttl ? 1.year.ago : 1
17
18
 
18
- ttl_variable_name = "@_ttl_for_#{ivar_name}".to_sym
19
- mutex_variable_name = "@_mutex_for_#{ivar_name}".to_sym
20
- value_variable_name = "@_value_for_#{ivar_name}".to_sym
19
+ ttl_variable_name = :"@_ttl_for_#{ivar_name}"
20
+ mutex_variable_name = :"@_mutex_for_#{ivar_name}"
21
+ value_variable_name = :"@_value_for_#{ivar_name}"
21
22
 
22
- reset_memoized_value_method_name = "reset_memoized_value_for_#{method_name}".to_sym
23
- setup_memoization_method_name = "_setup_memoization_for_#{method_name}".to_sym
24
- decrement_ttl_method_name = "_decrement_ttl_for_#{method_name}".to_sym
25
- ttl_exceeded_method_name = "_ttl_exceeded_for_#{method_name}".to_sym
26
- extend_ttl_method_name = "_extend_ttl_for_#{method_name}".to_sym
23
+ reset_memoized_value_method_name = :"reset_memoized_value_for_#{method_name}"
24
+ setup_memoization_method_name = :"_setup_memoization_for_#{method_name}"
25
+ decrement_ttl_method_name = :"_decrement_ttl_for_#{method_name}"
26
+ ttl_exceeded_method_name = :"_ttl_exceeded_for_#{method_name}"
27
+ extend_ttl_method_name = :"_extend_ttl_for_#{method_name}"
27
28
 
28
29
  [
29
30
  reset_memoized_value_method_name, setup_memoization_method_name,
@@ -44,9 +45,12 @@ module TTLMemoizeable
44
45
  end
45
46
 
46
47
  define_method setup_memoization_method_name do
47
- instance_variable_set(ttl_variable_name, expired_ttl) unless instance_variable_defined?(ttl_variable_name)
48
- instance_variable_set(mutex_variable_name, Mutex.new) unless instance_variable_defined?(mutex_variable_name)
49
- instance_variable_set(value_variable_name, nil) unless instance_variable_defined?(value_variable_name)
48
+ return if instance_variable_defined?(ttl_variable_name) && instance_variable_defined?(mutex_variable_name)
49
+
50
+ ::TTLMemoizeable::SetupMutex.synchronize do
51
+ instance_variable_set(ttl_variable_name, expired_ttl) unless instance_variable_defined?(ttl_variable_name)
52
+ instance_variable_set(mutex_variable_name, Mutex.new) unless instance_variable_defined?(mutex_variable_name)
53
+ end
50
54
  end
51
55
 
52
56
  define_method decrement_ttl_method_name do
@@ -56,11 +60,10 @@ module TTLMemoizeable
56
60
  end
57
61
 
58
62
  define_method ttl_exceeded_method_name do
59
- instance_variable_get(ttl_variable_name) <= if time_based_ttl
60
- ttl.ago
61
- else
62
- 0
63
- end
63
+ return true unless instance_variable_defined?(value_variable_name)
64
+
65
+ compared_to = time_based_ttl ? ttl.ago : 0
66
+ instance_variable_get(ttl_variable_name) <= compared_to
64
67
  end
65
68
 
66
69
  define_method extend_ttl_method_name do
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.2.0
4
+ version: 0.3.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: 2023-11-28 00:00:00.000000000 Z
11
+ date: 2024-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport