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 +4 -4
- data/CHANGELOG.md +2 -0
- data/README.md +11 -8
- data/lib/ttl_memoizeable/version.rb +1 -1
- data/lib/ttl_memoizeable.rb +19 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5763160f9370ac1bc7695b8299a6528953e6b90cd30806784b45763be6eae8c
|
4
|
+
data.tar.gz: 04e05378018dc76ad58718a170d2212f554a8838b53d071ec9ac2943f8a2bdf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 358dde19960ec800c5f58eddbd414a2126a866302c12c350714f355e0f29e049dadd055aa7584ba83e6e89717d5713e1f073718dc17276efb0827238340a89a4
|
7
|
+
data.tar.gz: 77486fa2db3df97f9c2b68217838ad36ed75c678f8c1e67b781f2618eacaf3253406e8344aba51f973b85afed2ac4bd26c5aec1c48b13839aa53e0957e4b810c
|
data/CHANGELOG.md
CHANGED
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
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
data/lib/ttl_memoizeable.rb
CHANGED
@@ -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}"
|
19
|
-
mutex_variable_name = "@_mutex_for_#{ivar_name}"
|
20
|
-
value_variable_name = "@_value_for_#{ivar_name}"
|
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}"
|
23
|
-
setup_memoization_method_name = "_setup_memoization_for_#{method_name}"
|
24
|
-
decrement_ttl_method_name = "_decrement_ttl_for_#{method_name}"
|
25
|
-
ttl_exceeded_method_name = "_ttl_exceeded_for_#{method_name}"
|
26
|
-
extend_ttl_method_name = "_extend_ttl_for_#{method_name}"
|
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
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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.
|
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:
|
11
|
+
date: 2024-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|