lru_redux 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -12
- data/lib/lru_redux/cache.rb +35 -34
- data/lib/lru_redux/thread_safe_cache.rb +1 -1
- data/lib/lru_redux/version.rb +1 -1
- data/test/cache_test.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af4d80362e99b12a12ab32c6a8274f216195d666
|
4
|
+
data.tar.gz: 1d68495be201eadc3c851b4efdfb6efb7128e710
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a836ef1195b172d1d3bf9f4091e8fbfd5802546ef45504f75d0db2842d1943a83df9df83b0138eb199deea1e52449bb7ef2613a8bfb386b5e81c8d42df7a7d5a
|
7
|
+
data.tar.gz: 25cce1c929ad8eda1975d3af356b9705f114042f85d14a5b4aa7a128be89f12dc3cfe6b8a93837a692a9f7932efd6ff753592e216972760c1e20b61e4736b557
|
data/README.md
CHANGED
@@ -45,19 +45,19 @@ see: benchmark directory
|
|
45
45
|
```
|
46
46
|
sam@ubuntu:~/Source/lru_redux/bench$ ruby ./bench.rb
|
47
47
|
Rehearsal ---------------------------------------------------------
|
48
|
-
thread safe lru 27.
|
49
|
-
lru gem 2.
|
50
|
-
lru_cache gem
|
51
|
-
lru_redux gem 1.
|
52
|
-
lru_redux thread safe 2.
|
53
|
-
----------------------------------------------- total: 36.
|
48
|
+
thread safe lru 27.640000 0.370000 28.010000 ( 28.049021)
|
49
|
+
lru gem 2.460000 0.000000 2.460000 ( 2.454292)
|
50
|
+
lru_cache gem 2.170000 0.000000 2.170000 ( 2.174306)
|
51
|
+
lru_redux gem 1.530000 0.020000 1.550000 ( 1.552481)
|
52
|
+
lru_redux thread safe 2.610000 0.070000 2.680000 ( 2.684895)
|
53
|
+
----------------------------------------------- total: 36.870000sec
|
54
54
|
|
55
55
|
user system total real
|
56
|
-
thread safe lru
|
57
|
-
lru gem 2.
|
58
|
-
lru_cache gem
|
59
|
-
lru_redux gem 1.
|
60
|
-
lru_redux thread safe 2.
|
56
|
+
thread safe lru 28.170000 0.280000 28.450000 ( 28.465008)
|
57
|
+
lru gem 2.330000 0.000000 2.330000 ( 2.328316)
|
58
|
+
lru_cache gem 2.140000 0.000000 2.140000 ( 2.142749)
|
59
|
+
lru_redux gem 1.640000 0.000000 1.640000 ( 1.643732)
|
60
|
+
lru_redux thread safe 2.590000 0.000000 2.590000 ( 2.600422)
|
61
61
|
|
62
62
|
```
|
63
63
|
|
@@ -72,6 +72,11 @@ lru_redux thread safe 2.750000 0.000000 2.750000 ( 2.752526)
|
|
72
72
|
|
73
73
|
## Changlog
|
74
74
|
|
75
|
-
###
|
75
|
+
###version 0.0.2 - 23-April-2013
|
76
76
|
|
77
77
|
- Added .clear method
|
78
|
+
|
79
|
+
###version 0.0.3 - 23-April-2013
|
80
|
+
|
81
|
+
- Added .max_size= so you can resize caches on demand
|
82
|
+
- Added implemented using arrays for extra perf
|
data/lib/lru_redux/cache.rb
CHANGED
@@ -2,6 +2,8 @@ class LruRedux::Cache
|
|
2
2
|
|
3
3
|
# for high efficiency nodes in double linked list are stored in arrays
|
4
4
|
# [prev,key,val,next]
|
5
|
+
#
|
6
|
+
# This makes this much more annoying to code, but gives us a 5-10% edge
|
5
7
|
|
6
8
|
def initialize(max_size)
|
7
9
|
@max_size = max_size
|
@@ -10,11 +12,17 @@ class LruRedux::Cache
|
|
10
12
|
@tail = nil
|
11
13
|
end
|
12
14
|
|
15
|
+
def max_size=(size)
|
16
|
+
@max_size = size
|
17
|
+
while pop_tail
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
def [](key)
|
14
22
|
node = @data[key]
|
15
23
|
if node
|
16
24
|
move_to_head(node)
|
17
|
-
node
|
25
|
+
node[2]
|
18
26
|
end
|
19
27
|
end
|
20
28
|
|
@@ -22,7 +30,7 @@ class LruRedux::Cache
|
|
22
30
|
node = @data[key]
|
23
31
|
if node
|
24
32
|
move_to_head(node)
|
25
|
-
node
|
33
|
+
node[2] = val
|
26
34
|
else
|
27
35
|
pop_tail
|
28
36
|
@data[key] = add_to_head(key,val)
|
@@ -33,8 +41,8 @@ class LruRedux::Cache
|
|
33
41
|
def each
|
34
42
|
if n = @head
|
35
43
|
while n
|
36
|
-
yield [n
|
37
|
-
n = n
|
44
|
+
yield [n[1], n[2]]
|
45
|
+
n = n[0]
|
38
46
|
end
|
39
47
|
end
|
40
48
|
end
|
@@ -54,11 +62,11 @@ class LruRedux::Cache
|
|
54
62
|
node = @data[k]
|
55
63
|
if node
|
56
64
|
@data.delete(k)
|
57
|
-
prev = node
|
58
|
-
nex = node
|
65
|
+
prev = node[0]
|
66
|
+
nex = node[3]
|
59
67
|
|
60
|
-
prev
|
61
|
-
nex
|
68
|
+
prev[3] = nex if prev
|
69
|
+
nex[0] = prev if nex
|
62
70
|
end
|
63
71
|
end
|
64
72
|
|
@@ -74,60 +82,53 @@ class LruRedux::Cache
|
|
74
82
|
# for cache validation only, ensures all is sound
|
75
83
|
def valid?
|
76
84
|
expected = {}
|
85
|
+
|
86
|
+
count = 0
|
77
87
|
self.each_unsafe do |k,v|
|
78
|
-
|
88
|
+
return false if @data[k][2] != v
|
89
|
+
count += 1
|
79
90
|
end
|
80
|
-
|
91
|
+
count == @data.count
|
81
92
|
end
|
82
93
|
|
83
94
|
protected
|
84
95
|
|
85
96
|
def add_to_head(key,val)
|
86
97
|
if @head.nil?
|
87
|
-
@tail = @head =
|
98
|
+
@tail = @head = [nil,key,val,nil]
|
88
99
|
else
|
89
|
-
node =
|
90
|
-
@head = @head
|
100
|
+
node = [@head,key,val,nil]
|
101
|
+
@head = @head[3] = node
|
91
102
|
end
|
92
103
|
end
|
93
104
|
|
94
105
|
def move_to_head(node)
|
95
|
-
return unless @head && node != @head
|
106
|
+
return unless @head && node[1] != @head[1]
|
96
107
|
|
97
|
-
prev = node
|
98
|
-
nex = node
|
108
|
+
prev = node[0]
|
109
|
+
nex = node[3]
|
99
110
|
|
100
111
|
if prev
|
101
|
-
prev
|
112
|
+
prev[3] = nex
|
102
113
|
else
|
103
114
|
@tail = nex
|
104
115
|
end
|
105
116
|
|
106
117
|
if nex
|
107
|
-
nex
|
118
|
+
nex[0] = prev
|
108
119
|
end
|
109
120
|
|
110
|
-
@head
|
111
|
-
node
|
121
|
+
@head[3] = node
|
122
|
+
node[0] = @head
|
112
123
|
@head = node
|
113
124
|
end
|
114
125
|
|
115
126
|
def pop_tail
|
116
|
-
if @data.length
|
117
|
-
@data.delete(@tail
|
118
|
-
@tail = @tail
|
119
|
-
@tail
|
127
|
+
if @data.length >= @max_size
|
128
|
+
@data.delete(@tail[1])
|
129
|
+
@tail = @tail[3]
|
130
|
+
@tail[0] = nil
|
120
131
|
end
|
121
132
|
end
|
122
133
|
|
123
|
-
|
124
|
-
class Node
|
125
|
-
attr_accessor :key, :value, :next, :prev
|
126
|
-
def initialize(key,value,prev,nex)
|
127
|
-
self.key = key
|
128
|
-
self.value = value
|
129
|
-
self.prev = prev
|
130
|
-
self.next = nex
|
131
|
-
end
|
132
|
-
end
|
133
134
|
end
|
data/lib/lru_redux/version.rb
CHANGED
data/test/cache_test.rb
CHANGED
@@ -7,6 +7,10 @@ class CacheTest < MiniTest::Unit::TestCase
|
|
7
7
|
@c = LruRedux::Cache.new(3)
|
8
8
|
end
|
9
9
|
|
10
|
+
def teardown
|
11
|
+
assert_equal true, @c.valid?
|
12
|
+
end
|
13
|
+
|
10
14
|
def test_drops_old
|
11
15
|
@c[:a] = 1
|
12
16
|
@c[:b] = 2
|
@@ -56,4 +60,21 @@ class CacheTest < MiniTest::Unit::TestCase
|
|
56
60
|
@c.clear
|
57
61
|
assert_equal [], @c.to_a
|
58
62
|
end
|
63
|
+
|
64
|
+
def test_grow
|
65
|
+
@c[:a] = 1
|
66
|
+
@c[:b] = 2
|
67
|
+
@c[:c] = 3
|
68
|
+
@c.max_size = 4
|
69
|
+
@c[:d] = 4
|
70
|
+
assert_equal [[:d,4],[:c,3],[:b,2],[:a,1]], @c.to_a
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_shrink
|
74
|
+
@c[:a] = 1
|
75
|
+
@c[:b] = 2
|
76
|
+
@c[:c] = 3
|
77
|
+
@c.max_size = 2
|
78
|
+
assert_equal [[:c,3],[:b,2]], @c.to_a
|
79
|
+
end
|
59
80
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lru_redux
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Saffron
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
requirements: []
|
137
137
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.0.
|
138
|
+
rubygems_version: 2.0.3
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: An efficient implementation of an lru cache
|