sized_list 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/active_support/cache/lru.rb +4 -0
- data/lib/sized_list.rb +47 -1
- data/sized_list.gemspec +10 -9
- data/spec/sized_list_spec.rb +23 -0
- metadata +68 -66
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -8,6 +8,7 @@ module ActiveSupport
|
|
8
8
|
def initialize(options)
|
9
9
|
@options = options
|
10
10
|
@sized_list = SizedList.new @options[:max_size]
|
11
|
+
@sized_list.enable_time_based_stats = !! @options[:enable_time_based_stats]
|
11
12
|
end
|
12
13
|
|
13
14
|
# Silence the logger.
|
@@ -57,6 +58,9 @@ module ActiveSupport
|
|
57
58
|
def write(name, value, options=nil)
|
58
59
|
instrument(:write, name, options) do |payload|
|
59
60
|
@sized_list[name] = value
|
61
|
+
payload[:eviction] = @sized_list.evicted?
|
62
|
+
payload[:total_evictions] = @sized_list.evictions
|
63
|
+
payload[:eviction_frequency] = @sized_list.eviction_frequency
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
data/lib/sized_list.rb
CHANGED
@@ -3,23 +3,50 @@ class SizedList
|
|
3
3
|
|
4
4
|
attr_reader :max_size
|
5
5
|
|
6
|
+
# Basic Stats
|
7
|
+
attr_accessor :enable_time_based_stats
|
8
|
+
|
9
|
+
attr_reader :hits,
|
10
|
+
:misses,
|
11
|
+
:writes,
|
12
|
+
:evictions
|
13
|
+
|
6
14
|
def initialize(max_size)
|
7
15
|
@max_size = max_size
|
8
16
|
@used = []
|
9
17
|
@items = {}
|
18
|
+
self.reset_stats
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset_stats
|
22
|
+
@hits = 0
|
23
|
+
@misses = 0
|
24
|
+
@writes = 0
|
25
|
+
@evictions = 0
|
26
|
+
@total_eviction_time = 0.0
|
27
|
+
@last_evicted_at = nil
|
10
28
|
end
|
11
29
|
|
12
30
|
def get(key)
|
13
31
|
if value = @items[key]
|
32
|
+
@hits += 1
|
14
33
|
used! key
|
34
|
+
else
|
35
|
+
@misses += 1
|
15
36
|
end
|
16
37
|
value
|
17
38
|
end
|
18
39
|
alias [] get
|
19
40
|
|
20
41
|
def set(key, value)
|
42
|
+
@writes += 1 unless exist?(key)
|
21
43
|
@items[key] = value
|
22
|
-
|
44
|
+
if @items.size > @max_size
|
45
|
+
@evicted = true
|
46
|
+
remove_least_recently_used!
|
47
|
+
else
|
48
|
+
@evicted = false
|
49
|
+
end
|
23
50
|
used! key
|
24
51
|
nil
|
25
52
|
end
|
@@ -43,11 +70,20 @@ class SizedList
|
|
43
70
|
@items.values
|
44
71
|
end
|
45
72
|
|
73
|
+
def evicted?
|
74
|
+
!! @evicted
|
75
|
+
end
|
76
|
+
|
46
77
|
def exist?(key)
|
47
78
|
@items.has_key? key
|
48
79
|
end
|
49
80
|
alias exists? exist?
|
50
81
|
|
82
|
+
def eviction_frequency
|
83
|
+
return 0.0 unless @enable_time_based_stats && @evictions > 1
|
84
|
+
@total_eviction_time / @evictions
|
85
|
+
end
|
86
|
+
|
51
87
|
private
|
52
88
|
|
53
89
|
def used!(key)
|
@@ -62,6 +98,16 @@ class SizedList
|
|
62
98
|
end
|
63
99
|
|
64
100
|
def remove_least_recently_used!
|
101
|
+
@evictions += 1
|
102
|
+
|
103
|
+
if @enable_time_based_stats
|
104
|
+
now = Time.now
|
105
|
+
if @last_evicted_at
|
106
|
+
@total_eviction_time += now - @last_evicted_at
|
107
|
+
end
|
108
|
+
@last_evicted_at = now
|
109
|
+
end
|
110
|
+
|
65
111
|
key = @used.pop
|
66
112
|
@items.delete key
|
67
113
|
end
|
data/sized_list.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.2.
|
7
|
+
s.name = %q{sized_list}
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Doug Youch"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = %q{2013-05-22}
|
13
|
+
s.description = %q{Uses LRU functionality to keep a limited size list of items}
|
14
|
+
s.email = %q{doug@sessionm.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
17
|
"README.rdoc"
|
@@ -30,16 +30,17 @@ Gem::Specification.new do |s|
|
|
30
30
|
"spec/sized_list_spec.rb",
|
31
31
|
"spec/spec_helper.rb"
|
32
32
|
]
|
33
|
-
s.homepage =
|
33
|
+
s.homepage = %q{http://github.com/dyouch5@yahoo.com/sized_list}
|
34
34
|
s.licenses = ["MIT"]
|
35
35
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version =
|
37
|
-
s.summary =
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
|
+
s.summary = %q{Limited size list}
|
38
38
|
|
39
39
|
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
41
|
s.specification_version = 3
|
41
42
|
|
42
|
-
if Gem::Version.new(Gem::
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
43
44
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
44
45
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
45
46
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
data/spec/sized_list_spec.rb
CHANGED
@@ -7,6 +7,7 @@ describe SizedList do
|
|
7
7
|
list.size.should == 5
|
8
8
|
100.times { |i| list.set "new-item-#{i}", 1 }
|
9
9
|
list.size.should == 5
|
10
|
+
list.evictions .should == 100
|
10
11
|
list['new-item-99'].should == 1
|
11
12
|
end
|
12
13
|
|
@@ -21,30 +22,52 @@ describe SizedList do
|
|
21
22
|
|
22
23
|
list['b'].should == 1
|
23
24
|
list.keys.should == ['b', 'e', 'd', 'c', 'a']
|
25
|
+
list.evictions.should == 0
|
26
|
+
list.misses.should == 0
|
27
|
+
list.hits.should == 1
|
28
|
+
list.writes.should == 5
|
24
29
|
|
25
30
|
list['b'].should == 1
|
26
31
|
list.keys.should == ['b', 'e', 'd', 'c', 'a']
|
32
|
+
list.evictions.should == 0
|
33
|
+
list.misses.should == 0
|
34
|
+
list.hits.should == 2
|
35
|
+
|
36
|
+
list['not_here'].should be_nil
|
37
|
+
list.evictions.should == 0
|
38
|
+
list.misses.should == 1
|
39
|
+
list.hits.should == 2
|
27
40
|
end
|
28
41
|
|
29
42
|
it "should remove the least accessed" do
|
30
43
|
list = SizedList.new 5
|
44
|
+
list.enable_time_based_stats = true
|
31
45
|
list['a'] = 1
|
32
46
|
list['b'] = 1
|
33
47
|
list['c'] = 1
|
34
48
|
list['d'] = 1
|
35
49
|
list['e'] = 1
|
36
50
|
list.keys.should == ['e', 'd', 'c', 'b', 'a']
|
51
|
+
list.writes.should == 5
|
37
52
|
|
38
53
|
list['a'].should == 1
|
39
54
|
list.keys.should == ['a', 'e', 'd', 'c', 'b']
|
55
|
+
list.evictions.should == 0
|
40
56
|
|
41
57
|
list['new'] = 1
|
58
|
+
list.evicted?.should be_true
|
59
|
+
list['new'] = 1
|
60
|
+
list.evicted?.should be_false
|
42
61
|
list.keys.should == ['new', 'a', 'e', 'd', 'c']
|
62
|
+
list.evictions.should == 1
|
63
|
+
list.writes.should == 6
|
43
64
|
|
44
65
|
list['d'].should == 1
|
45
66
|
list.keys.should == ['d', 'new', 'a', 'e', 'c']
|
67
|
+
list.evictions.should == 1
|
46
68
|
|
47
69
|
list['new2'] = 1
|
48
70
|
list.keys.should == ['new2', 'd', 'new', 'a', 'e']
|
71
|
+
list.evictions.should == 2
|
49
72
|
end
|
50
73
|
end
|
metadata
CHANGED
@@ -1,72 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sized_list
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- Doug Youch
|
9
13
|
autorequire:
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
|
17
|
+
date: 2013-05-22 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
type: :development
|
15
23
|
name: rdoc
|
16
|
-
|
17
|
-
|
18
|
-
requirements:
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
19
26
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 12
|
31
|
+
version: "3.12"
|
32
|
+
requirement: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
23
34
|
prerelease: false
|
24
|
-
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '3.12'
|
30
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
type: :development
|
31
36
|
name: jeweler
|
32
|
-
|
33
|
-
|
34
|
-
requirements:
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
35
39
|
- - ~>
|
36
|
-
- !ruby/object:Gem::Version
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 8
|
44
|
+
- 4
|
37
45
|
version: 1.8.4
|
38
|
-
|
46
|
+
requirement: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
39
48
|
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: 1.8.4
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: rspec
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
49
|
type: :development
|
55
|
-
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
50
|
+
name: rspec
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirement: *id003
|
62
59
|
description: Uses LRU functionality to keep a limited size list of items
|
63
60
|
email: doug@sessionm.com
|
64
61
|
executables: []
|
62
|
+
|
65
63
|
extensions: []
|
66
|
-
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
67
66
|
- LICENSE.txt
|
68
67
|
- README.rdoc
|
69
|
-
files:
|
68
|
+
files:
|
70
69
|
- .document
|
71
70
|
- Gemfile
|
72
71
|
- Gemfile.lock
|
@@ -79,32 +78,35 @@ files:
|
|
79
78
|
- sized_list.gemspec
|
80
79
|
- spec/sized_list_spec.rb
|
81
80
|
- spec/spec_helper.rb
|
81
|
+
has_rdoc: true
|
82
82
|
homepage: http://github.com/dyouch5@yahoo.com/sized_list
|
83
|
-
licenses:
|
83
|
+
licenses:
|
84
84
|
- MIT
|
85
85
|
post_install_message:
|
86
86
|
rdoc_options: []
|
87
|
-
|
87
|
+
|
88
|
+
require_paths:
|
88
89
|
- lib
|
89
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
segments:
|
96
102
|
- 0
|
97
|
-
|
98
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
-
none: false
|
100
|
-
requirements:
|
101
|
-
- - ! '>='
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
103
|
+
version: "0"
|
104
104
|
requirements: []
|
105
|
+
|
105
106
|
rubyforge_project:
|
106
|
-
rubygems_version: 1.
|
107
|
+
rubygems_version: 1.3.6
|
107
108
|
signing_key:
|
108
109
|
specification_version: 3
|
109
110
|
summary: Limited size list
|
110
111
|
test_files: []
|
112
|
+
|