volatile_hash 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,2 +1,2 @@
1
- rvm use ruby-1.9.2-p180@colorize --create
1
+ rvm use ruby-1.9.2-p180@volatile_hash --create
2
2
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- volatile_hash (0.0.0)
4
+ volatile_hash (0.0.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Satya P
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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 THE
21
+ SOFTWARE.
data/README CHANGED
@@ -1,8 +1,35 @@
1
1
  Implements a Time-to-live based cache and a Least-recently-used cache.
2
2
 
3
+ = Usage
4
+
5
+ cache = VolatileHash.new(:strategy => 'ttl', :ttl => 3600)
6
+ # TTL-based, default TTL is 3600 seconds. The value will be cached no longer
7
+ # than 3600 seconds after it is inserted into the cache.
8
+
9
+ cache = VolatileHash.new(:strategy => 'ttl', :ttl => 3600, :refresh => true)
10
+ # TTL-based, default TTL is 3600 seconds. The value will be cached no longer
11
+ # than 3600 seconds after it is inserted or accessed.
12
+
13
+ cache = VolatileHash.new(:strategy => 'lru', :max => 10)
14
+ # LRU-based, default max is 10. That's the maximum keys to remember.
15
+
16
+ cache[:key] = "some expensive-to-calculate value, like a database query result"
17
+ cache["any key"] = "or a remote api call"
18
+
19
+ # memo-ization:
20
+ def get_value_for(key)
21
+ @cache ||= VolatileHash.new(:strategy => 'ttl', :ttl => 3600)
22
+ @cache[key] ||= get_from_api(key)
23
+ end
3
24
 
4
25
  = Test/Development
5
26
 
6
27
  gem install bundler
7
28
  bundle install
8
29
  rake
30
+
31
+ = Credits
32
+
33
+ Based on a gist by github.com user joshaven at https://gist.github.com/184837
34
+
35
+ Some input from github user supertaz, considerable help on the TTL-based version.
data/lib/volatile_hash.rb CHANGED
@@ -9,6 +9,7 @@ class VolatileHash
9
9
  @max_items = options[:max] || 10
10
10
  @item_order = []
11
11
  end
12
+ @refresh = options[:refresh] or false
12
13
  end
13
14
 
14
15
  def [](key)
@@ -19,6 +20,9 @@ class VolatileHash
19
20
  @registry.delete key
20
21
  value = nil
21
22
  end
23
+ if @refresh and @registry[key]
24
+ set_ttl(key)
25
+ end
22
26
  else
23
27
  lru_update key if @cache.has_key?(key)
24
28
  end
@@ -27,7 +31,7 @@ class VolatileHash
27
31
 
28
32
  def []=(key, value)
29
33
  if @strategy == 'ttl'
30
- @registry[key] = Time.now + @ttl.to_f
34
+ set_ttl(key)
31
35
  @cache[key] = value
32
36
  else
33
37
  @item_order.unshift key
@@ -37,6 +41,10 @@ class VolatileHash
37
41
  end
38
42
 
39
43
  private
44
+ def set_ttl(key)
45
+ @registry[key] = Time.now + @ttl.to_f
46
+ end
47
+
40
48
  def expired?(key)
41
49
  Time.now > @registry[key]
42
50
  end
@@ -1,9 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe VolatileHash do
4
+ class Volatilehash
5
+ attr_reader :cache
6
+ end
7
+
4
8
  describe "TTL mode" do
5
9
  before do
6
- @cache = VolatileHash.new(:strategy => 'ttl', :ttl => 0.5, :max => 1)
10
+ @cache = VolatileHash.new(:strategy => 'ttl', :ttl => 0.7, :max => 1)
7
11
  @x = Object.new
8
12
  @cache[:x] = @x.to_s
9
13
  end
@@ -20,17 +24,36 @@ describe VolatileHash do
20
24
  it "should forget cached values after the TTL expires" do
21
25
  @cache[:x].should == @x.to_s
22
26
 
23
- sleep(0.6)
27
+ sleep(0.8)
24
28
 
25
29
  @cache[:x].should be_nil
26
30
  end
27
31
 
28
32
  it "should not throw out least-recently used value" do
29
- @cache[:y] =1
33
+ @cache[:y] = 1
30
34
 
31
35
  @cache[:x].should_not be_nil
32
36
  @cache[:y].should_not be_nil
33
37
  end
38
+
39
+ it "should not reset TTL when an item is accessed" do
40
+ sleep(0.4)
41
+ @cache[:x].should == @x.to_s
42
+ sleep(0.4)
43
+ @cache[:x].should be_nil
44
+ end
45
+
46
+ context "when asked to refresh TTL on access" do
47
+ it "should not forget cached values after TTL expires" do
48
+ cache = VolatileHash.new(:strategy => 'ttl', :ttl => 0.7, :refresh => true)
49
+ x = Object.new
50
+ cache[:x] = @x.to_s
51
+ sleep(0.4)
52
+ cache[:x].should == @x.to_s
53
+ sleep(0.4)
54
+ cache[:x].should == @x.to_s
55
+ end
56
+ end
34
57
  end
35
58
 
36
59
  describe "LRU mode" do
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'volatile_hash'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = '2012-02-22'
5
5
  s.summary = "Implements LRU and TTL caches"
6
6
  s.description = "Implements key-based cache that can have a least-recently-used or time-to-live expiration strategy"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volatile_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000Z
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &74297420 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *74297420
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rr
27
- requirement: &74297180 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 1.0.4
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *74297180
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.4
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rake
38
- requirement: &74296950 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: 0.9.2
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *74296950
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
47
62
  description: Implements key-based cache that can have a least-recently-used or time-to-live
48
63
  expiration strategy
49
64
  email: github@thesatya.com
@@ -54,6 +69,7 @@ files:
54
69
  - .rvmrc
55
70
  - Gemfile
56
71
  - Gemfile.lock
72
+ - LICENSE
57
73
  - README
58
74
  - Rakefile
59
75
  - lib/volatile_hash.rb
@@ -80,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
96
  version: '0'
81
97
  requirements: []
82
98
  rubyforge_project:
83
- rubygems_version: 1.8.10
99
+ rubygems_version: 1.8.24
84
100
  signing_key:
85
101
  specification_version: 3
86
102
  summary: Implements LRU and TTL caches