rufus-lru 1.0.4 → 1.0.5

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.
@@ -2,7 +2,12 @@
2
2
  = rufus-lru CHANGELOG.txt
3
3
 
4
4
 
5
- == rufus-lru - 1.0.4 not yet released
5
+ == rufus-lru - 1.0.5 released 2012/02/28
6
+
7
+ - introduced Rufus::Lru::SynchronizedHash (thread-safe)
8
+
9
+
10
+ == rufus-lru - 1.0.4 released 2012/02/28
6
11
 
7
12
  - fixed issue issue false values
8
13
  - 2012 refresh
data/README.md CHANGED
@@ -17,7 +17,7 @@ Once the maxsize is reached, the hash will discard the element that was the
17
17
  least recently used (hence LRU).
18
18
 
19
19
  require 'rubygems'
20
- require 'rufus/lru'
20
+ require 'rufus-lru'
21
21
 
22
22
  h = Rufus::Lru::Hash.new(3)
23
23
 
@@ -29,6 +29,15 @@ least recently used (hence LRU).
29
29
 
30
30
  puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
31
31
 
32
+ Rufus::Lru::Hash isn't thread-safe, if you need something that is, use Rufus::Lru::SynchronizedHash
33
+
34
+ require 'rubygems'
35
+ require 'rufus-lru'
36
+
37
+ h = Rufus::Lru::SyncrhonizedHash.new(3)
38
+
39
+ # ...
40
+
32
41
 
33
42
  ## dependencies
34
43
 
@@ -22,11 +22,13 @@
22
22
  # "made in Japan"
23
23
  #++
24
24
 
25
+ require 'thread'
26
+
25
27
 
26
28
  module Rufus
27
29
  module Lru
28
30
 
29
- VERSION = '1.0.4'
31
+ VERSION = '1.0.5'
30
32
 
31
33
  #
32
34
  # A Hash that has a max size. After the maxsize has been reached, the
@@ -46,7 +48,8 @@ module Lru
46
48
  #
47
49
  # puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
48
50
  #
49
- # Nota bene: this class is not threadsafe.
51
+ # Nota bene: this class is not thread-safe. If you need something thread-safe,
52
+ # use Rufus::Lru::SynchronizedHash.
50
53
  #
51
54
  class Hash < ::Hash
52
55
 
@@ -141,9 +144,34 @@ module Lru
141
144
  end
142
145
  end
143
146
  end
147
+
148
+ #
149
+ # A thread-safe version of the lru hash.
150
+ #
151
+ class SynchronizedHash < Hash
152
+
153
+ def initialize(maxsize)
154
+
155
+ super
156
+ @mutex = Mutex.new
157
+ end
158
+
159
+ def [](key)
160
+
161
+ @mutex.synchronize { super }
162
+ end
163
+
164
+ def []=(key, value)
165
+
166
+ @mutex.synchronize { super }
167
+ end
168
+ end
144
169
  end
145
170
  end
146
171
 
147
- class LruHash < Rufus::Lru::Hash
148
- end
172
+
173
+ #
174
+ # The original LruHash class, kept for backward compatibility.
175
+ #
176
+ class LruHash < Rufus::Lru::Hash; end
149
177
 
@@ -0,0 +1,23 @@
1
+
2
+ require 'spec_helper'
3
+
4
+
5
+ describe Rufus::Lru::SynchronizedHash do
6
+
7
+ # well, you've probably seen better specs...
8
+
9
+ let(:hash) { Rufus::Lru::SynchronizedHash.new(3) }
10
+
11
+ it 'sports a mutex' do
12
+
13
+ hash.instance_variable_get(:@mutex).class.should == Mutex
14
+ end
15
+
16
+ it 'works' do
17
+
18
+ 4.times { |i| hash[i] = i }
19
+
20
+ hash.size.should == 3
21
+ end
22
+ end
23
+
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
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &2161499280 !ruby/object:Gem::Requirement
17
+ requirement: &2153411200 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2161499280
25
+ version_requirements: *2153411200
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &2161498640 !ruby/object:Gem::Requirement
28
+ requirement: &2153410580 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 2.7.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2161498640
36
+ version_requirements: *2153410580
37
37
  description: LruHash class, a Hash with a max size, controlled by a LRU mechanism
38
38
  email:
39
39
  - jmettraux@gmail.com
@@ -47,6 +47,7 @@ files:
47
47
  - spec/hash_spec.rb
48
48
  - spec/lru_hash_spec.rb
49
49
  - spec/spec_helper.rb
50
+ - spec/synchronized_hash_spec.rb
50
51
  - rufus-lru.gemspec
51
52
  - CHANGELOG.txt
52
53
  - LICENSE.txt