ring_cache 1.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/lib/ring_cache.rb +4 -1
- data/lib/ring_cache/version.rb +1 -1
- data/test/test_ring_cache.rb +4 -3
- data/test/test_ring_cache_issues.rb +16 -0
- metadata +4 -2
data/.gitignore
CHANGED
data/lib/ring_cache.rb
CHANGED
@@ -99,11 +99,14 @@ class RingCache
|
|
99
99
|
end
|
100
100
|
|
101
101
|
def write(key, data)
|
102
|
-
|
102
|
+
unless evict(key)
|
103
|
+
evict_oldest if must_evict?
|
104
|
+
end
|
103
105
|
data = data.dup if @duplicate_on_store and !data.nil?
|
104
106
|
access_time = Time.now
|
105
107
|
@cache[key] = { last_accessed_at: access_time, data: data }
|
106
108
|
@access_time_index << [access_time, key]
|
109
|
+
true
|
107
110
|
end
|
108
111
|
|
109
112
|
private
|
data/lib/ring_cache/version.rb
CHANGED
data/test/test_ring_cache.rb
CHANGED
@@ -127,16 +127,17 @@ class TestRingCache < Minitest::Test
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def test_random_data
|
130
|
-
cache = RingCache.new(capacity: 1_000)
|
130
|
+
cache = RingCache.new(capacity: 1_000, target_hit_rate: 0.4)
|
131
131
|
data = random_data(2_000, 10)
|
132
132
|
|
133
|
-
|
133
|
+
10_000.times do
|
134
|
+
element = data.sample
|
134
135
|
cache.fetch(element[:key]) do
|
135
136
|
element[:content]
|
136
137
|
end
|
137
138
|
end
|
138
139
|
|
139
|
-
|
140
|
+
assert cache.size <= 1_000
|
140
141
|
access_time_index = cache.instance_variable_get(:@access_time_index)
|
141
142
|
cache_contents = cache.instance_variable_get(:@cache)
|
142
143
|
assert_equal cache_contents.size, access_time_index.size
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path('test_helper', File.dirname(__FILE__))
|
4
|
+
|
5
|
+
class TestRingCacheIssues < Minitest::Test
|
6
|
+
def test_records_one_index_per_entry
|
7
|
+
c = RingCache.new
|
8
|
+
|
9
|
+
c.write(:a, 1)
|
10
|
+
c.write(:a, 2)
|
11
|
+
|
12
|
+
assert_equal 1, c.size
|
13
|
+
access_time_index = c.instance_variable_get(:@access_time_index)
|
14
|
+
assert_equal 1, access_time_index.size
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ring_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-10-
|
12
|
+
date: 2014-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- test/random_data_generator.rb
|
63
63
|
- test/test_helper.rb
|
64
64
|
- test/test_ring_cache.rb
|
65
|
+
- test/test_ring_cache_issues.rb
|
65
66
|
- test/test_ring_cache_performance.rb
|
66
67
|
homepage: https://github.com/aredondo/ring_cache
|
67
68
|
licenses:
|
@@ -92,4 +93,5 @@ test_files:
|
|
92
93
|
- test/random_data_generator.rb
|
93
94
|
- test/test_helper.rb
|
94
95
|
- test/test_ring_cache.rb
|
96
|
+
- test/test_ring_cache_issues.rb
|
95
97
|
- test/test_ring_cache_performance.rb
|