tmp_cache 0.0.2 → 0.0.3
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.
- data/History.txt +4 -0
- data/lib/tmp_cache.rb +18 -4
- data/test/test_tmp_cache.rb +37 -31
- metadata +3 -3
data/History.txt
CHANGED
data/lib/tmp_cache.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
|
2
2
|
module TmpCache
|
3
|
-
VERSION = '0.0.
|
3
|
+
VERSION = '0.0.3'
|
4
4
|
|
5
5
|
def self.cache
|
6
6
|
@@cache ||= Hash.new{|h,k| h = {:expire => 0, :value => nil}}
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.set(key, value, expire=
|
10
|
-
cache[key] = {
|
11
|
-
|
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
|
12
15
|
end
|
13
16
|
|
14
17
|
def self.get(key)
|
@@ -19,19 +22,30 @@ module TmpCache
|
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
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
|
+
|
22
33
|
def self.reset
|
23
34
|
@@cache = nil
|
24
35
|
end
|
25
36
|
|
26
37
|
def self.keys
|
38
|
+
gc
|
27
39
|
cache.keys
|
28
40
|
end
|
29
41
|
|
30
42
|
def self.values
|
43
|
+
gc
|
31
44
|
cache.values.map{|v| v[:value] }
|
32
45
|
end
|
33
46
|
|
34
47
|
def self.each(&block)
|
48
|
+
gc
|
35
49
|
cache.each do |k,v|
|
36
50
|
yield k, v[:value]
|
37
51
|
end
|
data/test/test_tmp_cache.rb
CHANGED
@@ -1,31 +1,37 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
-
|
3
|
-
class TestTmpCache < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
TmpCache.set('name', 'shokai', 2)
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_get
|
10
|
-
assert TmpCache.get('name') == 'shokai'
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_expire
|
14
|
-
sleep 3
|
15
|
-
assert TmpCache.get('name') == nil
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_reset
|
19
|
-
TmpCache.reset
|
20
|
-
assert TmpCache.get('name') == nil
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_keys
|
24
|
-
TmpCache.set('mail', 'hashimoto@shokai.org')
|
25
|
-
assert TmpCache.keys.sort == ['name', 'mail'].sort
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_values
|
29
|
-
assert TmpCache.values == ['shokai']
|
30
|
-
end
|
31
|
-
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestTmpCache < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
TmpCache.set('name', 'shokai', 2)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_get
|
10
|
+
assert TmpCache.get('name') == 'shokai'
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_expire
|
14
|
+
sleep 3
|
15
|
+
assert TmpCache.get('name') == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_reset
|
19
|
+
TmpCache.reset
|
20
|
+
assert TmpCache.get('name') == nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_keys
|
24
|
+
TmpCache.set('mail', 'hashimoto@shokai.org')
|
25
|
+
assert TmpCache.keys.sort == ['name', 'mail'].sort
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_values
|
29
|
+
assert TmpCache.values == ['shokai']
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_gc
|
33
|
+
TmpCache.set('mail', 'hashimoto@shokai.org')
|
34
|
+
sleep 3
|
35
|
+
assert TmpCache.keys == ['mail']
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tmp_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sho Hashimoto
|