rufus-lru 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.txt +59 -0
  2. data/lib/rufus/lru.rb +124 -0
  3. data/test/test.rb +73 -0
  4. metadata +56 -0
data/README.txt ADDED
@@ -0,0 +1,59 @@
1
+
2
+ = rufus-lru
3
+
4
+ LruHash class, a Hash with a max size, controlled by a LRU mechanism
5
+
6
+
7
+ == getting it
8
+
9
+ sudo gem install rufus-lru
10
+
11
+ or at
12
+
13
+ http://rubyforge.org/frs/?group_id=4812
14
+
15
+
16
+ == usage
17
+
18
+ It's a regular hash, but you have to set a maxsize at instantiation.
19
+
20
+ Once the maxsize is reached, the hash will discard the element that was the
21
+ least recently used (hence LRU).
22
+
23
+ require 'rubygems'
24
+ require 'rufus/lru'
25
+
26
+ h = LruHash.new 3
27
+
28
+ 5.times { |i| h[i] = "a" * i }
29
+
30
+ puts h.inspect # >> {2=>"aa", 3=>"aaa", 4=>"aaaa"}
31
+
32
+ h[:newer] = "b"
33
+
34
+ puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
35
+
36
+
37
+ == mailing list
38
+
39
+ On the OpenWFEru-user for now :
40
+
41
+ http://groups.google.com/group/openwferu-users
42
+
43
+
44
+ == source
45
+
46
+ http://rufus.rubyforge.org/svn/trunk/lru
47
+
48
+ svn checkout http://rufus.rubyforge.org/svn/trunk/lru
49
+
50
+
51
+ == author
52
+
53
+ John Mettraux, jmettraux@gmail.com
54
+
55
+
56
+ == license
57
+
58
+ MIT
59
+
data/lib/rufus/lru.rb ADDED
@@ -0,0 +1,124 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2008, John Mettraux, jmettraux@gmail.com
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+ #++
23
+ #
24
+
25
+ #
26
+ # "made in Japan"
27
+ #
28
+ # John Mettraux
29
+ #
30
+
31
+ #require 'monitor'
32
+
33
+
34
+ #
35
+ # A Hash that has a max size. After the maxsize has been reached, the
36
+ # least recently used entries (LRU hence), will be discared to make
37
+ # room for the new entries.
38
+ #
39
+ class LruHash < Hash
40
+
41
+ #include MonitorMixin
42
+ #
43
+ # seems not necessary for now, and it collides with expool's
44
+ # @monitors own sync
45
+
46
+ attr_reader :maxsize
47
+
48
+ def initialize (maxsize)
49
+
50
+ super()
51
+
52
+ @maxsize = maxsize
53
+ @lru_keys = []
54
+ end
55
+
56
+ def maxsize= (s)
57
+
58
+ @maxsize = s
59
+ remove_lru while size > @maxsize
60
+ end
61
+
62
+ def clear
63
+
64
+ super
65
+ @lru_keys.clear
66
+ end
67
+
68
+ #
69
+ # Returns the keys with the lru in front.
70
+ #
71
+ def ordered_keys
72
+
73
+ @lru_keys
74
+ end
75
+
76
+ def [] (key)
77
+
78
+ value = super
79
+ return nil unless value
80
+ touch(key)
81
+
82
+ value
83
+ end
84
+
85
+ def []= (key, value)
86
+
87
+ remove_lru while size >= @maxsize
88
+ super
89
+ touch(key)
90
+
91
+ value
92
+ end
93
+
94
+ def delete (key)
95
+
96
+ value = super
97
+ @lru_keys.delete(key)
98
+
99
+ value
100
+ end
101
+
102
+ protected
103
+
104
+ #
105
+ # Puts the key on top of the lru 'stack'.
106
+ # The bottom being the lru place.
107
+ #
108
+ def touch (key)
109
+
110
+ @lru_keys.delete(key)
111
+ @lru_keys << key
112
+ end
113
+
114
+ #
115
+ # Removes the lru value and returns it.
116
+ # Returns nil if the cache is currently empty.
117
+ #
118
+ def remove_lru
119
+
120
+ key = @lru_keys.delete_at 0
121
+ delete key
122
+ end
123
+ end
124
+
data/test/test.rb ADDED
@@ -0,0 +1,73 @@
1
+
2
+ #
3
+ # Testing rufus-lru
4
+ #
5
+ # jmettraux@gmail.com
6
+ #
7
+ # Sun Oct 29 16:18:25 JST 2006
8
+ # then Tue Jan 15 12:53:04 JST 2008
9
+ #
10
+
11
+ require 'test/unit'
12
+ require 'rufus/lru'
13
+
14
+
15
+ class LruTest < Test::Unit::TestCase
16
+
17
+ #def setup
18
+ #end
19
+
20
+ #def teardown
21
+ #end
22
+
23
+ def test_lru_0
24
+
25
+ h = LruHash.new 3
26
+
27
+ assert_equal 0, h.size
28
+
29
+ h[:a] = "A"
30
+
31
+ assert_equal 1, h.size
32
+
33
+ h[:b] = "B"
34
+ h[:c] = "C"
35
+
36
+ assert_equal [ :a, :b, :c ], h.ordered_keys
37
+
38
+ h[:d] = "D"
39
+
40
+ assert_equal 3, h.size
41
+ assert_equal [ :b, :c, :d ], h.ordered_keys
42
+ assert_equal nil, h[:a]
43
+ assert_equal "B", h[:b]
44
+ assert_equal [ :c, :d, :b ], h.ordered_keys
45
+
46
+ h.delete :d
47
+
48
+ #require 'pp'
49
+ #puts "lru keys :"
50
+ #pp h.ordered_keys
51
+
52
+ assert_equal 2, h.size
53
+ assert_equal [ :c, :b ], h.ordered_keys
54
+
55
+ h[:a] = "A"
56
+
57
+ assert_equal 3, h.size
58
+ assert_equal [ :c, :b, :a ], h.ordered_keys
59
+
60
+ h[:d] = "D"
61
+
62
+
63
+ assert_equal 3, h.size
64
+ assert_equal [ :b, :a, :d ], h.ordered_keys
65
+
66
+ assert_equal "B", h[:b]
67
+ assert_equal "A", h[:a]
68
+ assert_equal "D", h[:d]
69
+ assert_equal nil, h[:c]
70
+ assert_equal [ :b, :a, :d ], h.ordered_keys
71
+ end
72
+
73
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rufus-lru
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - John Mettraux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-15 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: john at gmail dot com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - lib/rufus
26
+ - lib/rufus/lru.rb
27
+ - test/test.rb
28
+ - README.txt
29
+ has_rdoc: true
30
+ homepage: http://rufus.rubyforge.org/rufus-lru
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 0.9.5
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: LruHash class, a Hash with a max size, controlled by a LRU mechanism
55
+ test_files:
56
+ - test/test.rb