lru_redux 0.8.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +190 -29
- data/Rakefile +1 -1
- data/bench/bench.rb +27 -29
- data/bench/bench_ttl.rb +33 -0
- data/lib/lru_redux.rb +9 -14
- data/lib/lru_redux/cache.rb +54 -109
- data/lib/lru_redux/{cache19.rb → cache_legacy.rb} +0 -53
- data/lib/lru_redux/thread_safe_cache.rb +1 -73
- data/lib/lru_redux/ttl.rb +4 -0
- data/lib/lru_redux/ttl/cache.rb +192 -0
- data/lib/lru_redux/ttl/thread_safe_cache.rb +3 -0
- data/lib/lru_redux/util.rb +4 -0
- data/lib/lru_redux/util/safe_sync.rb +103 -0
- data/lib/lru_redux/util/safe_sync_jruby.rb +17 -0
- data/lib/lru_redux/version.rb +1 -1
- data/lru_redux.gemspec +6 -3
- data/test/cache_test.rb +10 -2
- data/test/thread_safe_cache_test.rb +1 -3
- data/test/ttl/cache_test.rb +91 -0
- data/test/ttl/thread_safe_cache_test.rb +16 -0
- metadata +33 -7
- data/lib/lru_redux/thread_safe_cache_jruby.rb +0 -13
data/lib/lru_redux/version.rb
CHANGED
data/lru_redux.gemspec
CHANGED
@@ -6,13 +6,15 @@ require 'lru_redux/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "lru_redux"
|
8
8
|
spec.version = LruRedux::VERSION
|
9
|
-
spec.authors = ["Sam Saffron"]
|
10
|
-
spec.email = ["sam.saffron@gmail.com"]
|
9
|
+
spec.authors = ["Sam Saffron", "Kaijah Hougham"]
|
10
|
+
spec.email = ["sam.saffron@gmail.com", "github@seberius.com"]
|
11
11
|
spec.description = %q{An efficient implementation of an lru cache}
|
12
12
|
spec.summary = %q{An efficient implementation of an lru cache}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/SamSaffron/lru_redux"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
+
spec.required_ruby_version = ">= 1.9.3"
|
17
|
+
|
16
18
|
spec.files = `git ls-files`.split($/)
|
17
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
20
|
spec.test_files = spec.files.grep(%r{^(test)/})
|
@@ -24,4 +26,5 @@ Gem::Specification.new do |spec|
|
|
24
26
|
spec.add_development_dependency "guard-minitest"
|
25
27
|
spec.add_development_dependency "guard"
|
26
28
|
spec.add_development_dependency "rb-inotify"
|
29
|
+
spec.add_development_dependency "timecop", "~> 0.7"
|
27
30
|
end
|
data/test/cache_test.rb
CHANGED
@@ -2,13 +2,13 @@ require 'lru_redux'
|
|
2
2
|
require 'minitest/autorun'
|
3
3
|
require 'minitest/pride'
|
4
4
|
|
5
|
-
class CacheTest < MiniTest::
|
5
|
+
class CacheTest < MiniTest::Test
|
6
6
|
def setup
|
7
7
|
@c = LruRedux::Cache.new(3)
|
8
8
|
end
|
9
9
|
|
10
10
|
def teardown
|
11
|
-
assert_equal true, @c.valid?
|
11
|
+
assert_equal true, @c.send(:valid?)
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_drops_old
|
@@ -72,6 +72,14 @@ class CacheTest < MiniTest::Unit::TestCase
|
|
72
72
|
assert_nil @c[:b]
|
73
73
|
end
|
74
74
|
|
75
|
+
def test_key?
|
76
|
+
@c[:a] = 1
|
77
|
+
@c[:b] = 2
|
78
|
+
|
79
|
+
assert_equal true, @c.key?(:a)
|
80
|
+
assert_equal false, @c.key?(:c)
|
81
|
+
end
|
82
|
+
|
75
83
|
def test_update
|
76
84
|
@c[:a] = 1
|
77
85
|
@c[:b] = 2
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'cache_test'
|
2
|
-
|
3
1
|
class ThreadSafeCacheTest < CacheTest
|
4
2
|
def setup
|
5
3
|
@c = LruRedux::ThreadSafeCache.new(3)
|
@@ -10,7 +8,7 @@ class ThreadSafeCacheTest < CacheTest
|
|
10
8
|
@c[:b] = 2
|
11
9
|
|
12
10
|
# should not blow up
|
13
|
-
@c.each do |k,
|
11
|
+
@c.each do |k, _|
|
14
12
|
@c[k]
|
15
13
|
end
|
16
14
|
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'timecop'
|
2
|
+
|
3
|
+
class TTLCacheTest < CacheTest
|
4
|
+
def setup
|
5
|
+
Timecop.freeze(Time.now)
|
6
|
+
@c = LruRedux::TTL::Cache.new 3, 5 * 60
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
Timecop.return
|
11
|
+
assert_equal true, @c.send(:valid?)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ttl
|
15
|
+
assert_equal 300, @c.ttl
|
16
|
+
|
17
|
+
@c.ttl = 10 * 60
|
18
|
+
|
19
|
+
assert_equal 600, @c.ttl
|
20
|
+
end
|
21
|
+
|
22
|
+
# TTL tests using Timecop
|
23
|
+
def test_ttl_eviction_on_access
|
24
|
+
@c[:a] = 1
|
25
|
+
@c[:b] = 2
|
26
|
+
|
27
|
+
Timecop.freeze(Time.now + 330)
|
28
|
+
|
29
|
+
@c[:c] = 3
|
30
|
+
|
31
|
+
assert_equal([[:c, 3]], @c.to_a)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_ttl_eviction_on_expire
|
35
|
+
@c[:a] = 1
|
36
|
+
@c[:b] = 2
|
37
|
+
|
38
|
+
Timecop.freeze(Time.now + 330)
|
39
|
+
|
40
|
+
@c.expire
|
41
|
+
|
42
|
+
assert_equal([], @c.to_a)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_ttl_eviction_on_new_max_size
|
46
|
+
@c[:a] = 1
|
47
|
+
@c[:b] = 2
|
48
|
+
|
49
|
+
Timecop.freeze(Time.now + 330)
|
50
|
+
|
51
|
+
@c.max_size = 10
|
52
|
+
|
53
|
+
assert_equal([], @c.to_a)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_ttl_eviction_on_new_ttl
|
57
|
+
@c[:a] = 1
|
58
|
+
@c[:b] = 2
|
59
|
+
|
60
|
+
Timecop.freeze(Time.now + 330)
|
61
|
+
|
62
|
+
@c.ttl = 10 * 60
|
63
|
+
|
64
|
+
assert_equal([[:b, 2], [:a, 1]], @c.to_a)
|
65
|
+
|
66
|
+
@c.ttl = 2 * 60
|
67
|
+
|
68
|
+
assert_equal([], @c.to_a)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_ttl_precedence_over_lru
|
72
|
+
@c[:a] = 1
|
73
|
+
|
74
|
+
Timecop.freeze(Time.now + 60)
|
75
|
+
|
76
|
+
@c[:b] = 2
|
77
|
+
@c[:c] = 3
|
78
|
+
|
79
|
+
@c[:a]
|
80
|
+
|
81
|
+
assert_equal [[:a, 1], [:c, 3], [:b, 2]],
|
82
|
+
@c.to_a
|
83
|
+
|
84
|
+
Timecop.freeze(Time.now + 270)
|
85
|
+
|
86
|
+
@c[:d] = 4
|
87
|
+
|
88
|
+
assert_equal [[:d, 4], [:c, 3], [:b, 2]],
|
89
|
+
@c.to_a
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class TTLThreadSafeCacheTest < TTLCacheTest
|
2
|
+
def setup
|
3
|
+
Timecop.freeze(Time.now)
|
4
|
+
@c = LruRedux::TTL::ThreadSafeCache.new 3, 5 * 60
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_recursion
|
8
|
+
@c[:a] = 1
|
9
|
+
@c[:b] = 2
|
10
|
+
|
11
|
+
# should not blow up
|
12
|
+
@c.each do |k, _|
|
13
|
+
@c[k]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lru_redux
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Saffron
|
8
|
+
- Kaijah Hougham
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2015-
|
12
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
@@ -94,9 +95,24 @@ dependencies:
|
|
94
95
|
- - ">="
|
95
96
|
- !ruby/object:Gem::Version
|
96
97
|
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: timecop
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - "~>"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0.7'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - "~>"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0.7'
|
97
112
|
description: An efficient implementation of an lru cache
|
98
113
|
email:
|
99
114
|
- sam.saffron@gmail.com
|
115
|
+
- github@seberius.com
|
100
116
|
executables: []
|
101
117
|
extensions: []
|
102
118
|
extra_rdoc_files: []
|
@@ -108,16 +124,24 @@ files:
|
|
108
124
|
- README.md
|
109
125
|
- Rakefile
|
110
126
|
- bench/bench.rb
|
127
|
+
- bench/bench_ttl.rb
|
111
128
|
- lib/lru_redux.rb
|
112
129
|
- lib/lru_redux/cache.rb
|
113
|
-
- lib/lru_redux/
|
130
|
+
- lib/lru_redux/cache_legacy.rb
|
114
131
|
- lib/lru_redux/thread_safe_cache.rb
|
115
|
-
- lib/lru_redux/
|
132
|
+
- lib/lru_redux/ttl.rb
|
133
|
+
- lib/lru_redux/ttl/cache.rb
|
134
|
+
- lib/lru_redux/ttl/thread_safe_cache.rb
|
135
|
+
- lib/lru_redux/util.rb
|
136
|
+
- lib/lru_redux/util/safe_sync.rb
|
137
|
+
- lib/lru_redux/util/safe_sync_jruby.rb
|
116
138
|
- lib/lru_redux/version.rb
|
117
139
|
- lru_redux.gemspec
|
118
140
|
- test/cache_test.rb
|
119
141
|
- test/thread_safe_cache_test.rb
|
120
|
-
|
142
|
+
- test/ttl/cache_test.rb
|
143
|
+
- test/ttl/thread_safe_cache_test.rb
|
144
|
+
homepage: https://github.com/SamSaffron/lru_redux
|
121
145
|
licenses:
|
122
146
|
- MIT
|
123
147
|
metadata: {}
|
@@ -129,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
153
|
requirements:
|
130
154
|
- - ">="
|
131
155
|
- !ruby/object:Gem::Version
|
132
|
-
version:
|
156
|
+
version: 1.9.3
|
133
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
158
|
requirements:
|
135
159
|
- - ">="
|
@@ -137,10 +161,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
161
|
version: '0'
|
138
162
|
requirements: []
|
139
163
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.
|
164
|
+
rubygems_version: 2.4.5
|
141
165
|
signing_key:
|
142
166
|
specification_version: 4
|
143
167
|
summary: An efficient implementation of an lru cache
|
144
168
|
test_files:
|
145
169
|
- test/cache_test.rb
|
146
170
|
- test/thread_safe_cache_test.rb
|
171
|
+
- test/ttl/cache_test.rb
|
172
|
+
- test/ttl/thread_safe_cache_test.rb
|