sin_lru_redux 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './test/cache_test'
4
+
5
+ class ThreadSafeCacheTest < CacheTest
6
+ def setup
7
+ @c = LruRedux::ThreadSafeCache.new(3)
8
+ end
9
+
10
+ def test_recursion
11
+ @c[:a] = 1
12
+ @c[:b] = 2
13
+
14
+ # should not blow up
15
+ @c.each do |k, _| # rubocop:disable Style/HashEachMethods
16
+ @c[k]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'timecop'
4
+ require './test/cache_test'
5
+
6
+ class TTLCacheTest < CacheTest
7
+ def setup
8
+ Timecop.freeze(Time.now)
9
+ @c = LruRedux::TTL::Cache.new 3, 5 * 60
10
+ end
11
+
12
+ def teardown
13
+ Timecop.return
14
+
15
+ assert @c.send(:valid?) # rubocop:disable Minitest/AssertionInLifecycleHook
16
+ end
17
+
18
+ def test_ttl
19
+ assert_equal 300, @c.ttl
20
+
21
+ @c.ttl = 10 * 60
22
+
23
+ assert_equal 600, @c.ttl
24
+ end
25
+
26
+ # TTL tests using Timecop
27
+ def test_ttl_eviction_on_access
28
+ @c[:a] = 1
29
+ @c[:b] = 2
30
+
31
+ Timecop.freeze(Time.now + 330)
32
+
33
+ @c[:c] = 3
34
+
35
+ assert_equal([[:c, 3]], @c.to_a)
36
+ end
37
+
38
+ def test_ttl_eviction_on_expire
39
+ @c[:a] = 1
40
+ @c[:b] = 2
41
+
42
+ Timecop.freeze(Time.now + 330)
43
+
44
+ @c.expire
45
+
46
+ assert_empty @c.to_a
47
+ end
48
+
49
+ def test_ttl_eviction_on_new_max_size
50
+ @c[:a] = 1
51
+ @c[:b] = 2
52
+
53
+ Timecop.freeze(Time.now + 330)
54
+
55
+ @c.max_size = 10
56
+
57
+ assert_empty @c.to_a
58
+ end
59
+
60
+ def test_ttl_eviction_on_new_ttl
61
+ @c[:a] = 1
62
+ @c[:b] = 2
63
+
64
+ Timecop.freeze(Time.now + 330)
65
+
66
+ @c.ttl = 10 * 60
67
+
68
+ assert_equal([[:b, 2], [:a, 1]], @c.to_a)
69
+
70
+ @c.ttl = 2 * 60
71
+
72
+ assert_empty @c.to_a
73
+ end
74
+
75
+ def test_ttl_precedence_over_lru
76
+ @c[:a] = 1
77
+
78
+ Timecop.freeze(Time.now + 60)
79
+
80
+ @c[:b] = 2
81
+ @c[:c] = 3
82
+
83
+ @c[:a]
84
+
85
+ assert_equal [[:a, 1], [:c, 3], [:b, 2]], @c.to_a
86
+
87
+ Timecop.freeze(Time.now + 270)
88
+
89
+ @c[:d] = 4
90
+
91
+ assert_equal [[:d, 4], [:c, 3], [:b, 2]], @c.to_a
92
+ end
93
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './test/ttl/cache_test'
4
+
5
+ class TTLThreadSafeCacheTest < TTLCacheTest
6
+ def setup
7
+ Timecop.freeze(Time.now)
8
+ @c = LruRedux::TTL::ThreadSafeCache.new 3, 5 * 60
9
+ end
10
+
11
+ def test_recursion
12
+ @c[:a] = 1
13
+ @c[:b] = 2
14
+
15
+ # should not blow up
16
+ @c.each do |k, _| # rubocop:disable Style/HashEachMethods
17
+ @c[k]
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sin_lru_redux
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Masahiro
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-12-28 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: An efficient implementation of an lru cache
13
+ email:
14
+ - watanabe@cadenza-tech.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".github/workflows/lint.yml"
20
+ - ".github/workflows/test.yml"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - Gemfile
24
+ - Guardfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bench/bench.rb
29
+ - bench/bench_ttl.rb
30
+ - lib/lru_redux.rb
31
+ - lib/lru_redux/cache.rb
32
+ - lib/lru_redux/thread_safe_cache.rb
33
+ - lib/lru_redux/ttl.rb
34
+ - lib/lru_redux/ttl/cache.rb
35
+ - lib/lru_redux/ttl/thread_safe_cache.rb
36
+ - lib/lru_redux/util.rb
37
+ - lib/lru_redux/util/safe_sync.rb
38
+ - lib/lru_redux/version.rb
39
+ - sin_lru_redux.gemspec
40
+ - test/cache_test.rb
41
+ - test/thread_safe_cache_test.rb
42
+ - test/ttl/cache_test.rb
43
+ - test/ttl/thread_safe_cache_test.rb
44
+ homepage: https://github.com/cadenza-tech/sin_lru_redux/tree/2.0.0/sin_lru_redux
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/cadenza-tech/sin_lru_redux/tree/2.0.0/sin_lru_redux
49
+ source_code_uri: https://github.com/cadenza-tech/sin_lru_redux/tree/2.0.0/sin_lru_redux
50
+ changelog_uri: https://github.com/cadenza-tech/sin_lru_redux/blob/2.0.0#changelog
51
+ bug_tracker_uri: https://github.com/cadenza-tech/sin_lru_redux/issues
52
+ documentation_uri: https://rubydoc.info/gems/sin_lru_redux/2.0.0
53
+ rubygems_mfa_required: 'true'
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.3.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.6.2
69
+ specification_version: 4
70
+ summary: An efficient implementation of an lru cache
71
+ test_files: []