sin_lru_redux 2.0.0 → 2.1.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: e3321a712e82f58a5a56395d60775b0ca747408656e82accb07fe9a6a9a3b0d4
4
- data.tar.gz: 83da0216f823ae497b57957c2f267d79d08299e59fba4735ba10e43018a72773
3
+ metadata.gz: 10d8a07bc1dcf5957a24804163d1ac62086dac85b3cefce3ff305757231a3696
4
+ data.tar.gz: 4820d035651c937d4486800a4ca3b1fda48ccfb1118d0bcd420bf49b88c5583e
5
5
  SHA512:
6
- metadata.gz: 5cb2a8d0c6b9e66e20c72f88c12c578aff52f6e07a70450969e1f2ec9fc209fb95afd63627630dac1d6c459d5723ffaa7001aa1525ac384491625df4a6b81a8b
7
- data.tar.gz: ef5f5c33c8a24d3769bbfa0bf7fa64c9ef1c01465d48d4119242b727b4b01a4b8f545f880d80be30796de819270dcc069336936b6dc2ecf2ad6dab47ec79c767
6
+ metadata.gz: 3cf6042d5c6cb2203e2505a4db581263e4bbe99475b1928558ee4a541a73d60b16c3d162199a11b103240e02f9ec09ece7c93f0b05fed10660c5fd84cec32907
7
+ data.tar.gz: 22d4e5addfb2efd94536334fddbb97f6cdd99237ca5215f6132b683ecce46bdae00d8e002239cfcc40ec26d0a37ff7b6fff08cb2a001518bb47a1a7bfaafc0f7
data/.rubocop.yml CHANGED
@@ -4,7 +4,7 @@ require:
4
4
  - rubocop-rake
5
5
 
6
6
  AllCops:
7
- TargetRubyVersion: 3.4
7
+ TargetRubyVersion: 2.3
8
8
  NewCops: enable
9
9
 
10
10
  # https://docs.rubocop.org/rubocop/cops_gemspec.html#gemspecrequiredrubyversion
data/CHANGELOG.md ADDED
@@ -0,0 +1,68 @@
1
+ # Changelog
2
+
3
+ ## [2.1.0] - 2024-12-30
4
+
5
+ - New: Set default max_size to 1000
6
+ - Fix: Fix a bug where ttl of item was not extended during LruRedux::TTL::Cache#getset, #fetch, and #[] execution
7
+ - Fix: Fix a bug where items with nil value were not deleted when ignore_nil was changed
8
+ - Refactor: Improve implementation
9
+ - Test: Improve tests
10
+ - Other: Update .gitignore
11
+ - Other: Add bin
12
+ - Other: Update Gemfile
13
+ - Other: Improve gemspec
14
+ - Other: Improve documents
15
+
16
+ ## [2.0.1] - 2024-12-28
17
+
18
+ - Refactor: Make valid_xxxx? methods private
19
+ - Fix: Fix lint CI
20
+ - Other: Update gemspec
21
+
22
+ ## [2.0.0] - 2024-12-28
23
+
24
+ - New: Add ignore_nil argument to cache initialize arguments. If true, blocks called by getset yielding nil values will be returned but not stored in the cache.
25
+ - Fix: Fix LruRedux::TTL::ThreadSafeCache#delete to return deleted value
26
+ - Ruby Support: Drop runtime support for Ruby 2.2 and below and JRuby
27
+
28
+ ## [1.1.0] - 2015-3-30
29
+
30
+ - New: TTL cache added. This cache is LRU like with the addition of time-based eviction. Check the Usage -> TTL Cache section in README.md for details.
31
+
32
+ ## [1.0.0] - 2015-3-26
33
+
34
+ - Ruby Support: Ruby 1.9+ is now required by LruRedux. If you need to use LruRedux in Ruby 1.8, please specify gem version 0.8.4 in your Gemfile. v0.8.4 is the last 1.8 compatible release and included a number of fixes and performance improvements for the Ruby 1.8 implementation. @Seberius
35
+ - Perf: improve performance in Ruby 2.1+ on the MRI @Seberius
36
+
37
+ ## [0.8.4] - 2015-2-20
38
+
39
+ - Fix: regression of ThreadSafeCache under JRuby 1.7 @Seberius
40
+
41
+ ## [0.8.3] - 2015-2-20
42
+
43
+ - Perf: improve ThreadSafeCache performance @Seberius
44
+
45
+ ## [0.8.2] - 2015-2-16
46
+
47
+ - Perf: use #size instead of #count when checking length @Seberius
48
+ - Fix: Cache could grow beyond its size in Ruby 1.8 @Seberius
49
+ - Fix: #each could deadlock in Ruby 1.8 @Seberius
50
+
51
+ ## [0.8.1] - 2013-9-7
52
+
53
+ - Fix #each implementation
54
+ - Fix deadlocks with ThreadSafeCache
55
+ - Version jump is because its been used in production for quite a while now
56
+
57
+ ## [0.0.6] - 2013-4-23
58
+
59
+ - Fix bug in getset, overflow was not returning the yeilded val
60
+
61
+ ## [0.0.5] - 2013-4-23
62
+
63
+ - Added getset and fetch
64
+ - Optimised implementation so it 20-30% faster on Ruby 1.9+
65
+
66
+ ## [0.0.4] - 2013-4-23
67
+
68
+ - Initial version
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt CHANGED
@@ -1,23 +1,22 @@
1
+ The MIT License (MIT)
2
+
1
3
  Copyright (c) 2013 Sam Saffron
2
4
  Copyright (c) 2024 Masahiro
3
5
 
4
- MIT License
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining
7
- a copy of this software and associated documentation files (the
8
- "Software"), to deal in the Software without restriction, including
9
- without limitation the rights to use, copy, modify, merge, publish,
10
- distribute, sublicense, and/or sell copies of the Software, and to
11
- permit persons to whom the Software is furnished to do so, subject to
12
- the following conditions:
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
13
12
 
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
16
15
 
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # SinLruRedux [![Gem Version](https://badge.fury.io/rb/sin_lru_redux.svg)](http://badge.fury.io/rb/sin_lru_redux)
1
+ # SinLruRedux
2
2
 
3
- An efficient, thread safe LRU cache.
3
+ [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cadenza-tech/sin_lru_redux/blob/main/LICENSE.txt) [![tag](https://img.shields.io/github/tag/cadenza-tech/sin_lru_redux.svg?logo=github&color=2EBC4F)](https://github.com/cadenza-tech/sin_lru_redux/blob/main/CHANGELOG.md) [![release](https://github.com/cadenza-tech/sin_lru_redux/actions/workflows/release.yml/badge.svg)](https://github.com/cadenza-tech/sin_lru_redux/actions?query=workflow%3Arelease) [![test](https://github.com/cadenza-tech/sin_lru_redux/actions/workflows/test.yml/badge.svg)](https://github.com/cadenza-tech/sin_lru_redux/actions?query=workflow%3Arelease) [![lint](https://github.com/cadenza-tech/sin_lru_redux/actions/workflows/lint.yml/badge.svg)](https://github.com/cadenza-tech/sin_lru_redux/actions?query=workflow%3Arelease)
4
+
5
+ Efficient and thread-safe LRU cache.
4
6
 
5
7
  Forked from [LruRedux](https://github.com/SamSaffron/lru_redux).
6
8
 
@@ -8,24 +10,24 @@ Forked from [LruRedux](https://github.com/SamSaffron/lru_redux).
8
10
  - [Usage](#usage)
9
11
  - [TTL Cache](#ttl-cache)
10
12
  - [Cache Methods](#cache-methods)
11
- - [Benchmarks](#benchmarks)
12
- - [Other Caches](#other-caches)
13
13
  - [Contributing](#contributing)
14
- - [Changelog](#changelog)
14
+ - [Changelog](https://github.com/cadenza-tech/sin_lru_redux/blob/main/CHANGELOG.md)
15
+ - [License](#license)
16
+ - [Code of Conduct](#code-of-conduct)
15
17
 
16
18
  ## Installation
17
19
 
18
- Add this line to your application's Gemfile:
19
-
20
- gem 'sin_lru_redux'
21
-
22
- And then execute:
20
+ Install the gem and add to the application's Gemfile by executing:
23
21
 
24
- $ bundle install
22
+ ```bash
23
+ bundle add sin_lru_redux
24
+ ```
25
25
 
26
- Or install it yourself as:
26
+ If bundler is not being used to manage dependencies, install the gem by executing:
27
27
 
28
- $ gem install sin_lru_redux
28
+ ```bash
29
+ gem install sin_lru_redux
30
+ ```
29
31
 
30
32
  ## Usage
31
33
 
@@ -67,7 +69,7 @@ cache.to_a
67
69
  cache = LruRedux::ThreadSafeCache.new(100)
68
70
  ```
69
71
 
70
- #### TTL Cache
72
+ **TTL Cache**
71
73
 
72
74
  The TTL cache extends the functionality of the LRU cache with a Time To Live eviction strategy. TTL eviction occurs on every access and takes precedence over LRU eviction, meaning a 'live' value will never be evicted over an expired one.
73
75
 
@@ -152,137 +154,20 @@ cache = LruRedux::TTL::ThreadSafeCache.new(100, 5 * 60)
152
154
  - `#ignore_nil` Returns the current ignore nil setting.
153
155
  - `#ignore_nil=` Takes true or false. Changes the current ignore nil setting.
154
156
 
155
- #### TTL Cache Specific
157
+ **TTL Cache Specific**
156
158
 
157
159
  - `#ttl` Returns the current TTL of the cache.
158
160
  - `#ttl=` Takes `:none` or a positive number. Changes the current ttl and triggers a TTL eviction.
159
161
  - `#expire` Triggers a TTL eviction.
160
162
 
161
- ## Benchmarks
162
-
163
- see: benchmark directory (a million random lookup / store)
164
-
165
- #### LRU
166
-
167
- ##### Ruby 2.2.1
168
-
169
- ```shell
170
- $ ruby ./bench/bench.rb
171
-
172
- Rehearsal -------------------------------------------------------------
173
- ThreadSafeLru 4.500000 0.030000 4.530000 ( 4.524213)
174
- LRU 2.250000 0.000000 2.250000 ( 2.249670)
175
- LRUCache 1.720000 0.010000 1.730000 ( 1.728243)
176
- LruRedux::Cache 0.960000 0.000000 0.960000 ( 0.961292)
177
- LruRedux::ThreadSafeCache 2.180000 0.000000 2.180000 ( 2.187714)
178
- --------------------------------------------------- total: 11.650000sec
179
-
180
- user system total real
181
- ThreadSafeLru 4.390000 0.020000 4.410000 ( 4.415703)
182
- LRU 2.140000 0.010000 2.150000 ( 2.149626)
183
- LRUCache 1.680000 0.010000 1.690000 ( 1.688564)
184
- LruRedux::Cache 0.910000 0.000000 0.910000 ( 0.913108)
185
- LruRedux::ThreadSafeCache 2.200000 0.010000 2.210000 ( 2.212108)
186
- ```
187
-
188
- #### TTL
189
-
190
- ##### Ruby 2.2.1
191
-
192
- ```shell
193
- $ ruby ./bench/bench_ttl.rb
194
- Rehearsal -----------------------------------------------------------------------
195
- FastCache 6.240000 0.070000 6.310000 ( 6.302569)
196
- LruRedux::TTL::Cache 4.700000 0.010000 4.710000 ( 4.712858)
197
- LruRedux::TTL::ThreadSafeCache 6.300000 0.010000 6.310000 ( 6.319032)
198
- LruRedux::TTL::Cache (TTL disabled) 2.460000 0.010000 2.470000 ( 2.470629)
199
- ------------------------------------------------------------- total: 19.800000sec
200
-
201
- user system total real
202
- FastCache 6.470000 0.070000 6.540000 ( 6.536193)
203
- LruRedux::TTL::Cache 4.640000 0.010000 4.650000 ( 4.661793)
204
- LruRedux::TTL::ThreadSafeCache 6.310000 0.020000 6.330000 ( 6.328840)
205
- LruRedux::TTL::Cache (TTL disabled) 2.440000 0.000000 2.440000 ( 2.446269)
206
- ```
207
-
208
- ## Other Caches
209
-
210
- This is a list of the caches that are used in the benchmarks.
211
-
212
- #### LRU
213
-
214
- - RubyGems: https://rubygems.org/gems/lru
215
- - Homepage: http://lru.rubyforge.org/
216
-
217
- #### LRUCache
218
-
219
- - RubyGems: https://rubygems.org/gems/lru_cache
220
- - Homepage: https://github.com/brendan/lru_cache
221
-
222
- #### ThreadSafeLru
223
-
224
- - RubyGems: https://rubygems.org/gems/threadsafe-lru
225
- - Homepage: https://github.com/draganm/threadsafe-lru
226
-
227
- #### FastCache
228
-
229
- - RubyGems: https://rubygems.org/gems/fast_cache
230
- - Homepage: https://github.com/swoop-inc/fast_cache
231
-
232
163
  ## Contributing
233
164
 
234
- 1. Fork it
235
- 2. Create your feature branch (`git checkout -b my-new-feature`)
236
- 3. Commit your changes (`git commit -am 'Add some feature'`)
237
- 4. Push to the branch (`git push origin my-new-feature`)
238
- 5. Create new Pull Request
239
-
240
- ## Changelog
241
-
242
- ### version 2.0.0 - 28-Dec-2024
243
-
244
- - New: Add ignore_nil argument to cache initialize arguments. If true, blocks called by getset yielding nil values will be returned but not stored in the cache.
245
- - Fix: Fix LruRedux::TTL::ThreadSafeCache#delete to return deleted value.
246
- - Ruby Support: Drop runtime support for Ruby 2.3 and below and JRuby.
247
-
248
- ### version 1.1.0 - 30-Mar-2015
249
-
250
- - New: TTL cache added. This cache is LRU like with the addition of time-based eviction. Check the Usage -> TTL Cache section in README.md for details.
251
-
252
- ### version 1.0.0 - 26-Mar-2015
253
-
254
- - Ruby Support: Ruby 1.9+ is now required by LruRedux. If you need to use LruRedux in Ruby 1.8, please specify gem version 0.8.4 in your Gemfile. v0.8.4 is the last 1.8 compatible release and included a number of fixes and performance improvements for the Ruby 1.8 implementation. @Seberius
255
- - Perf: improve performance in Ruby 2.1+ on the MRI @Seberius
256
-
257
- ### version 0.8.4 - 20-Feb-2015
258
-
259
- - Fix: regression of ThreadSafeCache under JRuby 1.7 @Seberius
260
-
261
- ### version 0.8.3 - 20-Feb-2015
262
-
263
- - Perf: improve ThreadSafeCache performance @Seberius
264
-
265
- ### version 0.8.2 - 16-Feb-2015
266
-
267
- - Perf: use #size instead of #count when checking length @Seberius
268
- - Fix: Cache could grow beyond its size in Ruby 1.8 @Seberius
269
- - Fix: #each could deadlock in Ruby 1.8 @Seberius
270
-
271
- ### version 0.8.1 - 7-Sep-2013
272
-
273
- - Fix #each implementation
274
- - Fix deadlocks with ThreadSafeCache
275
- - Version jump is because its been used in production for quite a while now
276
-
277
- ### version 0.0.6 - 24-April-2013
278
-
279
- - Fix bug in getset, overflow was not returning the yeilded val
165
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/abcd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cadenza-tech/sin_lru_redux/blob/main/CODE_OF_CONDUCT.md).
280
166
 
281
- ### version 0.0.5 - 23-April-2013
167
+ ## License
282
168
 
283
- - Added getset and fetch
284
- - Optimised implementation so it 20-30% faster on Ruby 1.9+
169
+ The gem is available as open source under the terms of the [MIT License](https://github.com/cadenza-tech/sin_lru_redux/blob/main/LICENSE.txt).
285
170
 
286
- ### version 0.0.4 - 23-April-2013
171
+ ## Code of Conduct
287
172
 
288
- - Initial version
173
+ Everyone interacting in the SinLruRedux project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cadenza-tech/sin_lru_redux/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/testtask'
5
5
  require 'rubocop/rake_task'
6
6
 
7
7
  Rake::TestTask.new(:test) do |t|
8
- t.pattern = 'test/**/*_test.rb'
8
+ t.pattern = 'test/**/test_*.rb'
9
9
  end
10
10
 
11
11
  RuboCop::RakeTask.new
@@ -1,56 +1,62 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class LruRedux::Cache
4
+ attr_reader :max_size, :ignore_nil
5
+
4
6
  def initialize(*args)
5
7
  max_size, ignore_nil, _ = args
6
8
 
9
+ max_size ||= 1000
7
10
  ignore_nil ||= false
8
11
 
9
- raise ArgumentError.new(:max_size) unless valid_max_size?(max_size)
10
- raise ArgumentError.new(:ignore_nil) unless valid_ignore_nil?(ignore_nil)
12
+ validate_max_size!(max_size)
13
+ validate_ignore_nil!(ignore_nil)
11
14
 
12
15
  @max_size = max_size
13
16
  @ignore_nil = ignore_nil
14
17
  @data = {}
15
18
  end
16
19
 
17
- def max_size=(max_size)
18
- max_size ||= @max_size
19
-
20
- raise ArgumentError.new(:max_size) unless valid_max_size?(max_size)
20
+ def max_size=(new_max_size)
21
+ new_max_size ||= @max_size
21
22
 
22
- @max_size = max_size
23
+ validate_max_size!(new_max_size)
23
24
 
24
- @data.shift while @data.size > @max_size
25
+ @max_size = new_max_size
26
+ evict_excess
25
27
  end
26
28
 
27
29
  def ttl=(_)
28
30
  nil
29
31
  end
30
32
 
31
- def ignore_nil=(ignore_nil)
32
- ignore_nil ||= @ignore_nil
33
- raise ArgumentError.new(:ignore_nil) unless valid_ignore_nil?(ignore_nil)
33
+ def ignore_nil=(new_ignore_nil)
34
+ new_ignore_nil ||= @ignore_nil
34
35
 
35
- @ignore_nil = ignore_nil
36
+ validate_ignore_nil!(new_ignore_nil)
37
+
38
+ @ignore_nil = new_ignore_nil
39
+ evict_nil
36
40
  end
37
41
 
38
42
  def getset(key)
39
- found = true
40
- value = @data.delete(key) { found = false }
41
- if found
43
+ key_found = true
44
+ value = @data.delete(key) { key_found = false }
45
+
46
+ if key_found
42
47
  @data[key] = value
43
48
  else
44
- result = @data[key] = yield
45
- @data.shift if @data.length > @max_size
49
+ result = yield
50
+ store_item(key, result)
46
51
  result
47
52
  end
48
53
  end
49
54
 
50
55
  def fetch(key)
51
- found = true
52
- value = @data.delete(key) { found = false }
53
- if found
56
+ key_found = true
57
+ value = @data.delete(key) { key_found = false }
58
+
59
+ if key_found
54
60
  @data[key] = value
55
61
  else
56
62
  yield if block_given? # rubocop:disable Style/IfInsideElse
@@ -58,46 +64,39 @@ class LruRedux::Cache
58
64
  end
59
65
 
60
66
  def [](key)
61
- found = true
62
- value = @data.delete(key) { found = false }
63
- @data[key] = value if found
67
+ key_found = true
68
+ value = @data.delete(key) { key_found = false }
69
+ return unless key_found
70
+
71
+ @data[key] = value
64
72
  end
65
73
 
66
74
  def []=(key, val)
67
- @data.delete(key)
68
- @data[key] = val
69
- @data.shift if @data.length > @max_size
70
- val # rubocop:disable Lint/Void
75
+ store_item(key, val)
71
76
  end
72
77
 
73
78
  def each(&block)
74
- array = @data.to_a
75
- array.reverse!.each(&block)
79
+ @data.to_a.reverse_each(&block)
76
80
  end
77
-
78
- # used further up the chain, non thread safe each
81
+ # Used further up the chain, non thread safe each
79
82
  alias_method :each_unsafe, :each
80
83
 
81
84
  def to_a
82
- array = @data.to_a
83
- array.reverse!
85
+ @data.to_a.reverse
84
86
  end
85
87
 
86
88
  def values
87
- vals = @data.values
88
- vals.reverse!
89
+ @data.values.reverse
89
90
  end
90
91
 
91
92
  def delete(key)
92
93
  @data.delete(key)
93
94
  end
94
-
95
95
  alias_method :evict, :delete
96
96
 
97
97
  def key?(key)
98
98
  @data.key?(key)
99
99
  end
100
-
101
100
  alias_method :has_key?, :key?
102
101
 
103
102
  def clear
@@ -108,22 +107,55 @@ class LruRedux::Cache
108
107
  @data.size
109
108
  end
110
109
 
111
- protected
110
+ private
111
+
112
+ # For cache validation only, ensure all is valid
113
+ def valid?
114
+ true
115
+ end
116
+
117
+ def validate_max_size!(max_size)
118
+ unless max_size.is_a?(Numeric)
119
+ raise ArgumentError.new(<<~ERROR)
120
+ Invalid max_size: #{max_size.inspect}
121
+ max_size must be a number.
122
+ ERROR
123
+ end
124
+ return if max_size >= 1
125
+
126
+ raise ArgumentError.new(<<~ERROR)
127
+ Invalid max_size: #{max_size.inspect}
128
+ max_size must be greater than or equal to 1.
129
+ ERROR
130
+ end
112
131
 
113
- def valid_max_size?(max_size)
114
- return true if max_size.is_a?(Integer) && max_size >= 1
132
+ def validate_ignore_nil!(ignore_nil)
133
+ return if [true, false].include?(ignore_nil)
115
134
 
116
- false
135
+ raise ArgumentError.new(<<~ERROR)
136
+ Invalid ignore_nil: #{ignore_nil.inspect}
137
+ ignore_nil must be a boolean value.
138
+ ERROR
117
139
  end
118
140
 
119
- def valid_ignore_nil?(ignore_nil)
120
- return true if [true, false].include?(ignore_nil)
141
+ def evict_excess
142
+ @data.shift while @data.size > @max_size
143
+ end
121
144
 
122
- false
145
+ if RUBY_VERSION >= '2.6.0'
146
+ def evict_nil
147
+ @data.compact!
148
+ end
149
+ else
150
+ def evict_nil
151
+ @data.reject! { |_key, value| value.nil? }
152
+ end
123
153
  end
124
154
 
125
- # for cache validation only, ensures all is sound
126
- def valid?
127
- true
155
+ def store_item(key, val)
156
+ @data.delete(key)
157
+ @data[key] = val if !val.nil? || !@ignore_nil
158
+ evict_excess
159
+ val
128
160
  end
129
161
  end