rufus-lru 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +16 -5
- data/lib/rufus/lru.rb +20 -10
- data/test/test.rb +13 -1
- metadata +2 -2
data/README.txt
CHANGED
@@ -10,7 +10,7 @@ LruHash class, a Hash with a max size, controlled by a LRU mechanism
|
|
10
10
|
|
11
11
|
or at
|
12
12
|
|
13
|
-
|
13
|
+
http://rubyforge.org/frs/?group_id=4812
|
14
14
|
|
15
15
|
|
16
16
|
== usage
|
@@ -34,23 +34,34 @@ least recently used (hence LRU).
|
|
34
34
|
puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
|
35
35
|
|
36
36
|
|
37
|
+
== dependencies
|
38
|
+
|
39
|
+
None.
|
40
|
+
|
41
|
+
|
37
42
|
== mailing list
|
38
43
|
|
39
|
-
On the OpenWFEru-user for now :
|
44
|
+
On the OpenWFEru-user list for now :
|
45
|
+
|
46
|
+
http://groups.google.com/group/openwferu-users
|
47
|
+
|
48
|
+
|
49
|
+
== issue tracker
|
40
50
|
|
41
|
-
|
51
|
+
http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
|
42
52
|
|
43
53
|
|
44
54
|
== source
|
45
55
|
|
46
|
-
|
56
|
+
http://rufus.rubyforge.org/svn/trunk/lru
|
47
57
|
|
48
58
|
svn checkout http://rufus.rubyforge.org/svn/trunk/lru
|
49
59
|
|
50
60
|
|
51
61
|
== author
|
52
62
|
|
53
|
-
John Mettraux, jmettraux@gmail.com
|
63
|
+
John Mettraux, jmettraux@gmail.com
|
64
|
+
http://jmettraux.wordpress.com
|
54
65
|
|
55
66
|
|
56
67
|
== license
|
data/lib/rufus/lru.rb
CHANGED
@@ -75,7 +75,7 @@ class LruHash < Hash
|
|
75
75
|
def maxsize= (s)
|
76
76
|
|
77
77
|
@maxsize = s
|
78
|
-
remove_lru
|
78
|
+
remove_lru
|
79
79
|
end
|
80
80
|
|
81
81
|
def clear
|
@@ -96,24 +96,31 @@ class LruHash < Hash
|
|
96
96
|
|
97
97
|
value = super
|
98
98
|
return nil unless value
|
99
|
-
touch
|
99
|
+
touch key
|
100
100
|
|
101
101
|
value
|
102
102
|
end
|
103
103
|
|
104
104
|
def []= (key, value)
|
105
105
|
|
106
|
-
remove_lru
|
106
|
+
remove_lru
|
107
107
|
super
|
108
|
-
touch
|
108
|
+
touch key
|
109
109
|
|
110
110
|
value
|
111
111
|
end
|
112
112
|
|
113
|
+
def merge! (hash)
|
114
|
+
|
115
|
+
hash.each { |k, v| self[k] = v }
|
116
|
+
|
117
|
+
# not using 'super', but in order not guaranteed at all...
|
118
|
+
end
|
119
|
+
|
113
120
|
def delete (key)
|
114
121
|
|
115
122
|
value = super
|
116
|
-
@lru_keys.delete
|
123
|
+
@lru_keys.delete key
|
117
124
|
|
118
125
|
value
|
119
126
|
end
|
@@ -126,18 +133,21 @@ class LruHash < Hash
|
|
126
133
|
#
|
127
134
|
def touch (key)
|
128
135
|
|
129
|
-
@lru_keys.delete
|
136
|
+
@lru_keys.delete key
|
130
137
|
@lru_keys << key
|
131
138
|
end
|
132
139
|
|
133
140
|
#
|
134
|
-
#
|
135
|
-
#
|
141
|
+
# Makes sure that the hash fits its maxsize. If not, will remove
|
142
|
+
# the least recently used items.
|
136
143
|
#
|
137
144
|
def remove_lru
|
138
145
|
|
139
|
-
|
140
|
-
|
146
|
+
while size >= @maxsize
|
147
|
+
|
148
|
+
key = @lru_keys.delete_at 0
|
149
|
+
delete key
|
150
|
+
end
|
141
151
|
end
|
142
152
|
end
|
143
153
|
|
data/test/test.rb
CHANGED
@@ -20,7 +20,7 @@ class LruTest < Test::Unit::TestCase
|
|
20
20
|
#def teardown
|
21
21
|
#end
|
22
22
|
|
23
|
-
def
|
23
|
+
def test_0
|
24
24
|
|
25
25
|
h = LruHash.new 3
|
26
26
|
|
@@ -70,4 +70,16 @@ class LruTest < Test::Unit::TestCase
|
|
70
70
|
assert_equal [ :b, :a, :d ], h.ordered_keys
|
71
71
|
end
|
72
72
|
|
73
|
+
def test_1
|
74
|
+
|
75
|
+
h = LruHash.new 3
|
76
|
+
|
77
|
+
h[1] = 10
|
78
|
+
|
79
|
+
h.merge!({ 2 => 20, 3 => 30, 4 => 40, 5 => 50 })
|
80
|
+
|
81
|
+
assert_nil h[1]
|
82
|
+
assert_equal 3, h.size
|
83
|
+
end
|
84
|
+
|
73
85
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufus-lru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Mettraux
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-01-
|
12
|
+
date: 2008-01-18 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|