tmp_cache 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ DS_Store
2
+ *~
3
+ pkg/*
4
+ .\#*
5
+ *\#*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gem 'hoe'
3
+ gem 'newgem'
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ RedCloth (4.2.9)
5
+ activesupport (2.3.14)
6
+ hoe (3.1.1)
7
+ rake (~> 0.8)
8
+ newgem (1.5.3)
9
+ RedCloth (>= 4.1.1)
10
+ activesupport (~> 2.3.4)
11
+ hoe (>= 2.4.0)
12
+ rubigen (>= 1.5.3)
13
+ syntax (>= 1.0.0)
14
+ rake (0.9.2.2)
15
+ rubigen (1.5.8)
16
+ activesupport (>= 2.3.5, < 3.2.0)
17
+ syntax (1.0.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ hoe
24
+ newgem
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.1.0 2012-11-17
2
+
3
+ * support mix-in Cache::Prototype
4
+
1
5
  === 0.0.3 2012-05-05
2
6
 
3
7
  * GC before get Keys
data/Manifest.txt CHANGED
@@ -1,11 +1,20 @@
1
+ .gitignore
2
+ Gemfile
3
+ Gemfile.lock
1
4
  History.txt
2
5
  Manifest.txt
3
6
  README.rdoc
4
7
  Rakefile
5
8
  lib/tmp_cache.rb
9
+ lib/tmp_cache/cache.rb
10
+ samples/sample.rb
11
+ samples/sample_instance.rb
6
12
  script/console
7
13
  script/destroy
8
14
  script/generate
15
+ test/test_all.rb
9
16
  test/test_helper.rb
17
+ test/test_mixin_class.rb
18
+ test/test_mixin_instance.rb
10
19
  test/test_tmp_cache.rb
11
- samples/sample.rb
20
+ test/test_tmp_cache_instance.rb
data/README.rdoc CHANGED
@@ -6,35 +6,38 @@
6
6
 
7
7
  on memory cache.
8
8
 
9
- == SYNOPSIS:
9
+ == INSTALL:
10
10
 
11
- #!/usr/bin/env ruby
12
- require 'tmp_cache'
13
-
14
- TmpCache.set('name', 'shokai', 60) # expire 60 sec
15
-
16
- puts TmpCache.get('name') # => 'shokai'
17
- sleep 61
18
- puts TmpCache.get('name') # => nil
11
+ * gem install tmp_cache
19
12
 
13
+ == SYNOPSIS:
20
14
 
21
- TmpCache.set('name', 'shokai')
22
- TmpCache.set('mail', 'hashimoto@shokai.org')
23
- TmpCache.each do |k,v|
24
- puts "#{k} => #{v}"
25
- end
15
+ #!/usr/bin/env ruby
16
+ require 'tmp_cache'
26
17
 
18
+ TmpCache.set('name', 'shokai', 60) # expire 60 sec
27
19
 
28
- == REQUIREMENTS:
20
+ puts TmpCache.get('name') # => 'shokai'
21
+ sleep 61
22
+ puts TmpCache.get('name') # => nil
29
23
 
30
- * Ruby 1.8.7+
31
- * Ruby 1.9.3+
24
+ TmpCache.set('name', 'shokai')
25
+ TmpCache.set('mail', 'hashimoto@shokai.org')
26
+ TmpCache.each do |k,v|
27
+ puts "#{k} => #{v}"
28
+ end
32
29
 
33
30
 
34
- == INSTALL:
31
+ create new cache
35
32
 
36
- * gem install tmp_cache
33
+ cache = TmpCache::Cache.new
34
+ cache.set('foo', 'bar')
35
+ puts cache.get('foo')
36
+
37
+ == REQUIREMENTS:
37
38
 
39
+ * Ruby 1.8.7+
40
+ * Ruby 1.9.3+
38
41
 
39
42
  == LICENSE:
40
43
 
@@ -0,0 +1,68 @@
1
+ module TmpCache
2
+
3
+ module Prototype
4
+
5
+ def self.included(klass)
6
+ klass.extend Methods
7
+ klass.__send__ :include, Methods
8
+ end
9
+
10
+ def self.apply(object)
11
+ object.extend Methods
12
+ end
13
+
14
+
15
+ module Methods
16
+ def cache
17
+ @cache ||= Hash.new{|h,k| h = {:expire => 0, :value => nil}}
18
+ end
19
+
20
+ def set(key, value, expire=nil)
21
+ cache[key] = {
22
+ :value => value,
23
+ :expire => expire ? Time.now.to_i+expire.to_i : nil
24
+ }
25
+ value
26
+ end
27
+
28
+ def get(key)
29
+ if cache[key][:expire] < Time.now.to_i
30
+ return cache[key][:value] = nil
31
+ else
32
+ return cache[key][:value]
33
+ end
34
+ end
35
+
36
+ def gc
37
+ cache.each do |k,c|
38
+ if c[:expire] != nil and Time.now.to_i > c[:expire].to_i
39
+ cache.delete k
40
+ end
41
+ end
42
+ end
43
+
44
+ def reset
45
+ @cache = nil
46
+ end
47
+
48
+ def keys
49
+ gc
50
+ cache.keys
51
+ end
52
+
53
+ def values
54
+ gc
55
+ cache.values.map{|v| v[:value] }
56
+ end
57
+
58
+ def each(&block)
59
+ gc
60
+ cache.each do |k,v|
61
+ yield k, v[:value]
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+
68
+ end
data/lib/tmp_cache.rb CHANGED
@@ -1,53 +1,13 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
1
3
 
2
- module TmpCache
3
- VERSION = '0.0.3'
4
-
5
- def self.cache
6
- @@cache ||= Hash.new{|h,k| h = {:expire => 0, :value => nil}}
7
- end
8
-
9
- def self.set(key, value, expire=nil)
10
- cache[key] = {
11
- :value => value,
12
- :expire => expire ? Time.now.to_i+expire.to_i : nil
13
- }
14
- value
15
- end
16
-
17
- def self.get(key)
18
- if cache[key][:expire] < Time.now.to_i
19
- return cache[key][:value] = nil
20
- else
21
- return cache[key][:value]
22
- end
23
- end
4
+ require 'tmp_cache/cache'
24
5
 
25
- def self.gc
26
- cache.each do |k,c|
27
- if c[:expire] != nil and Time.now.to_i > c[:expire].to_i
28
- cache.delete k
29
- end
30
- end
31
- end
32
-
33
- def self.reset
34
- @@cache = nil
35
- end
36
-
37
- def self.keys
38
- gc
39
- cache.keys
40
- end
41
-
42
- def self.values
43
- gc
44
- cache.values.map{|v| v[:value] }
45
- end
6
+ module TmpCache
7
+ VERSION = '0.1.0'
8
+ include TmpCache::Prototype
46
9
 
47
- def self.each(&block)
48
- gc
49
- cache.each do |k,v|
50
- yield k, v[:value]
51
- end
10
+ class Cache
11
+ include TmpCache::Prototype
52
12
  end
53
13
  end
data/samples/sample.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'rubygems'
3
-
4
- ## $:.push File.dirname(__FILE__)+'/../lib'
5
-
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
6
4
  require 'tmp_cache'
7
5
 
8
6
  TmpCache.set('name', 'shokai', 2) # expire 2 sec
9
7
  puts TmpCache.get('name') # => 'shokai'
10
8
  sleep 3
11
- puts TmpCache.get('name') # => nil
9
+ puts TmpCache.get('name') || 'expired' # => nil
12
10
 
13
11
 
14
12
  TmpCache.set('name', 'shokai')
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'tmp_cache'
5
+
6
+ cache = TmpCache::Cache.new
7
+ cache.set('name', 'shokai', 2) # expire 2 sec
8
+ puts cache.get('name') # => 'shokai'
9
+ sleep 3
10
+ puts cache.get('name') || 'expired' # => nil
11
+
12
+
13
+ cache.set('name', 'shokai')
14
+ cache.set('mail', 'hashimoto@shokai.org')
15
+ cache.each do |k,v|
16
+ puts "#{k} => #{v}"
17
+ end
data/test/test_all.rb ADDED
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestAll < Test::Unit::TestCase
4
+
5
+ class MyCache
6
+ include TmpCache::Prototype
7
+ end
8
+
9
+ def setup
10
+ @cache = TmpCache::Cache.new
11
+ @my_cache = MyCache.new
12
+
13
+ TmpCache.set('name', 'class', 2)
14
+ @cache.set('name', 'instance', 2)
15
+ MyCache.set('name', 'class mix', 2)
16
+ @my_cache.set('name', 'instance mix', 2)
17
+ end
18
+
19
+ def test_get
20
+ assert TmpCache.get('name') == 'class'
21
+ assert @cache.get('name') == 'instance'
22
+ assert MyCache.get('name') == 'class mix'
23
+ assert @my_cache.get('name') == 'instance mix'
24
+ end
25
+
26
+ def test_expire
27
+ sleep 3
28
+ assert TmpCache.get('name') == nil
29
+ assert @cache.get('name') == nil
30
+ assert MyCache.get('name') == nil
31
+ assert @my_cache.get('name') == nil
32
+ end
33
+
34
+ def test_reset
35
+ TmpCache.reset
36
+ @cache.reset
37
+ MyCache.reset
38
+ @my_cache.reset
39
+ assert TmpCache.get('name') == nil
40
+ assert @cache.get('name') == nil
41
+ assert MyCache.get('name') == nil
42
+ assert @my_cache.get('name') == nil
43
+ end
44
+
45
+ def test_keys
46
+ [TmpCache, @cache, MyCache, @my_cache].each do |i|
47
+ i.set('mail', 'hashimoto@shokai.org')
48
+ end
49
+ [TmpCache, @cache, MyCache, @my_cache].each do |i|
50
+ assert i.keys.sort == ['name', 'mail'].sort
51
+ end
52
+ end
53
+
54
+ def test_values
55
+ assert TmpCache.values == ['class']
56
+ assert @cache.values == ['instance']
57
+ assert MyCache.values == ['class mix']
58
+ assert @my_cache.values == ['instance mix']
59
+ end
60
+
61
+ def test_gc
62
+ TmpCache.set('mail', 'hashimoto@shokai.org')
63
+ @cache.set('mail', 'hashimoto@shokai.org')
64
+ MyCache.set('mail', 'hashimoto@shokai.org')
65
+ @my_cache.set('mail', 'hashimoto@shokai.org')
66
+ sleep 3
67
+ assert TmpCache.keys == ['mail']
68
+ assert @cache.keys == ['mail']
69
+ assert MyCache.keys == ['mail']
70
+ assert @my_cache.keys == ['mail']
71
+ end
72
+ end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'stringio'
2
2
  require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/tmp_cache'
3
+ require 'tmp_cache'
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMixinClass < Test::Unit::TestCase
4
+
5
+ class MyCache
6
+ include TmpCache::Prototype
7
+
8
+ def self.size
9
+ self.keys.size
10
+ end
11
+ end
12
+
13
+ def setup
14
+ MyCache.set('name', 'shokai', 2)
15
+ end
16
+
17
+ def test_class_method
18
+ assert MyCache.size == 1
19
+ sleep 3
20
+ assert MyCache.size == 0
21
+ end
22
+
23
+ def test_get
24
+ assert MyCache.get('name') == 'shokai'
25
+ end
26
+
27
+ def test_expire
28
+ sleep 3
29
+ assert MyCache.get('name') == nil
30
+ end
31
+
32
+ def test_reset
33
+ MyCache.reset
34
+ assert MyCache.get('name') == nil
35
+ end
36
+
37
+ def test_keys
38
+ MyCache.set('mail', 'hashimoto@shokai.org')
39
+ assert MyCache.keys.sort == ['name', 'mail'].sort
40
+ end
41
+
42
+ def test_values
43
+ assert MyCache.values == ['shokai']
44
+ end
45
+
46
+ def test_gc
47
+ MyCache.set('mail', 'hashimoto@shokai.org')
48
+ sleep 3
49
+ assert MyCache.keys == ['mail']
50
+ end
51
+ end
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMixinInstance < Test::Unit::TestCase
4
+
5
+ class MyCache
6
+ include TmpCache::Prototype
7
+
8
+ def size
9
+ keys.size
10
+ end
11
+ end
12
+
13
+ def setup
14
+ @tmp_cache = MyCache.new
15
+ @tmp_cache.set('name', 'shokai', 2)
16
+ end
17
+
18
+ def test_instance_method
19
+ assert @tmp_cache.size == 1
20
+ sleep 3
21
+ assert @tmp_cache.size == 0
22
+ end
23
+
24
+ def test_get
25
+ assert @tmp_cache.get('name') == 'shokai'
26
+ end
27
+
28
+ def test_expire
29
+ sleep 3
30
+ assert @tmp_cache.get('name') == nil
31
+ end
32
+
33
+ def test_reset
34
+ @tmp_cache.reset
35
+ assert @tmp_cache.get('name') == nil
36
+ end
37
+
38
+ def test_keys
39
+ @tmp_cache.set('mail', 'hashimoto@shokai.org')
40
+ assert @tmp_cache.keys.sort == ['name', 'mail'].sort
41
+ end
42
+
43
+ def test_values
44
+ assert @tmp_cache.values == ['shokai']
45
+ end
46
+
47
+ def test_gc
48
+ @tmp_cache.set('mail', 'hashimoto@shokai.org')
49
+ sleep 3
50
+ assert @tmp_cache.keys == ['mail']
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestTmpCacheInstance < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @tmp_cache = TmpCache::Cache.new
7
+ @tmp_cache.set('name', 'shokai', 2)
8
+ end
9
+
10
+ def test_get
11
+ assert @tmp_cache.get('name') == 'shokai'
12
+ end
13
+
14
+ def test_expire
15
+ sleep 3
16
+ assert @tmp_cache.get('name') == nil
17
+ end
18
+
19
+ def test_reset
20
+ @tmp_cache.reset
21
+ assert @tmp_cache.get('name') == nil
22
+ end
23
+
24
+ def test_keys
25
+ @tmp_cache.set('mail', 'hashimoto@shokai.org')
26
+ assert @tmp_cache.keys.sort == ['name', 'mail'].sort
27
+ end
28
+
29
+ def test_values
30
+ assert @tmp_cache.values == ['shokai']
31
+ end
32
+
33
+ def test_gc
34
+ @tmp_cache.set('mail', 'hashimoto@shokai.org')
35
+ sleep 3
36
+ assert @tmp_cache.keys == ['mail']
37
+ end
38
+ end
metadata CHANGED
@@ -1,126 +1,128 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: tmp_cache
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Sho Hashimoto
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-05-05 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rdoc
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 3
31
- - 10
32
- version: "3.10"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: newgem
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.10'
30
+ - !ruby/object:Gem::Dependency
31
+ name: newgem
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 5
44
- segments:
45
- - 1
46
- - 5
47
- - 3
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
48
37
  version: 1.5.3
49
38
  type: :development
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: hoe
53
39
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
55
41
  none: false
56
- requirements:
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: hoe
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
57
51
  - - ~>
58
- - !ruby/object:Gem::Version
59
- hash: 29
60
- segments:
61
- - 2
62
- - 15
63
- version: "2.15"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.1'
64
54
  type: :development
65
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
66
62
  description: on memory cache.
67
- email:
63
+ email:
68
64
  - hashimoto@shokai.org
69
65
  executables: []
70
-
71
66
  extensions: []
72
-
73
- extra_rdoc_files:
67
+ extra_rdoc_files:
74
68
  - History.txt
75
69
  - Manifest.txt
76
70
  - README.rdoc
77
- files:
71
+ files:
72
+ - .gitignore
73
+ - Gemfile
74
+ - Gemfile.lock
78
75
  - History.txt
79
76
  - Manifest.txt
80
77
  - README.rdoc
81
78
  - Rakefile
82
79
  - lib/tmp_cache.rb
80
+ - lib/tmp_cache/cache.rb
81
+ - samples/sample.rb
82
+ - samples/sample_instance.rb
83
83
  - script/console
84
84
  - script/destroy
85
85
  - script/generate
86
+ - test/test_all.rb
86
87
  - test/test_helper.rb
88
+ - test/test_mixin_class.rb
89
+ - test/test_mixin_instance.rb
87
90
  - test/test_tmp_cache.rb
88
- - samples/sample.rb
91
+ - test/test_tmp_cache_instance.rb
89
92
  - .gemtest
90
93
  homepage: http://github.com/shokai/tmp_cache
91
94
  licenses: []
92
-
93
95
  post_install_message:
94
- rdoc_options:
96
+ rdoc_options:
95
97
  - --main
96
98
  - README.rdoc
97
- require_paths:
99
+ require_paths:
98
100
  - lib
99
- required_ruby_version: !ruby/object:Gem::Requirement
101
+ required_ruby_version: !ruby/object:Gem::Requirement
100
102
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- hash: 3
105
- segments:
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
106
108
  - 0
107
- version: "0"
108
- required_rubygems_version: !ruby/object:Gem::Requirement
109
+ hash: 4211903737220457078
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
111
  none: false
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- hash: 3
114
- segments:
115
- - 0
116
- version: "0"
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
117
116
  requirements: []
118
-
119
117
  rubyforge_project: tmp_cache
120
- rubygems_version: 1.8.17
118
+ rubygems_version: 1.8.24
121
119
  signing_key:
122
120
  specification_version: 3
123
121
  summary: on memory cache.
124
- test_files:
122
+ test_files:
123
+ - test/test_all.rb
125
124
  - test/test_helper.rb
125
+ - test/test_mixin_class.rb
126
+ - test/test_mixin_instance.rb
126
127
  - test/test_tmp_cache.rb
128
+ - test/test_tmp_cache_instance.rb