proc-cache 0.1.0 → 0.1.2
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/README.rdoc +21 -13
- data/lib/proc-cache.rb +8 -5
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,18 +1,26 @@
|
|
1
1
|
= proc-cache
|
2
|
+
プロシージャ(Proc)の実行結果をキャッシュします
|
3
|
+
proc{ sleep 3; "heavy task" }.cache!
|
4
|
+
=> "heavy task" # 3秒後に"heavy task"という文字列が戻ってくる
|
5
|
+
proc{ sleep 3; "heavy task" }.cache!
|
6
|
+
=> "heavy task" # ただちに"heavy task"という文字列が戻ってくる
|
2
7
|
|
3
|
-
|
8
|
+
= いろんな書き方
|
9
|
+
proc{ ... }.cache!
|
10
|
+
lambda{ ... }.cache!
|
11
|
+
Proc.new{ ... }.cache!
|
4
12
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
future version unintentionally.
|
11
|
-
* Commit, do not mess with rakefile, version, or history.
|
12
|
-
(if you want to have your own version, that is fine but
|
13
|
-
bump version in a commit by itself I can ignore when I pull)
|
14
|
-
* Send me a pull request. Bonus points for topic branches.
|
13
|
+
= オプション
|
14
|
+
== キャッシュの有効期限を設定する
|
15
|
+
Proc#cache!(:expires => キャッシュを保持する秒数)
|
16
|
+
require 'active_support/all'
|
17
|
+
proc{...}.cache! :expires => 2.hours # 2時間経過するまでキャッシュを使う
|
15
18
|
|
16
|
-
==
|
19
|
+
== 内部動作を確認したいとき
|
20
|
+
ロガー渡せます
|
21
|
+
proc{...}.cache! :logger => Logger.new(STDERR)
|
22
|
+
|
23
|
+
== キャッシュのパスを変更したいとき
|
24
|
+
デフォルトだと/tmp/実行スクリプト名.cacheです。
|
25
|
+
proc{...}.cache! :cache_path => './test.cache'
|
17
26
|
|
18
|
-
Copyright (c) 2011 kimoto. See LICENSE for details.
|
data/lib/proc-cache.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'tmpdir'
|
2
|
+
require 'logger'
|
2
3
|
|
3
4
|
class Proc
|
4
5
|
def cache!(options={})
|
5
|
-
cache_path =
|
6
|
+
cache_path = make_cache_path
|
6
7
|
@logger = Logger.new(nil)
|
7
8
|
|
8
9
|
cache_path = options[:cache_path] if options[:cache_path]
|
@@ -53,11 +54,13 @@ class Proc
|
|
53
54
|
data
|
54
55
|
end
|
55
56
|
|
56
|
-
def
|
57
|
+
def make_cache_path_with_source_location
|
58
|
+
(file,line) = self.source_location
|
59
|
+
File.join(Dir.tmpdir, "#{File.basename($0)}_#{file}_#{line}.cache")
|
60
|
+
end
|
61
|
+
|
62
|
+
def make_cache_path
|
57
63
|
File.join(Dir.tmpdir, "#{File.basename($0)}.cache")
|
58
64
|
end
|
59
65
|
end
|
60
66
|
|
61
|
-
__END__
|
62
|
-
p Proc.new{ sleep 3; 1 }.cache!(:expires => 1.hours)
|
63
|
-
|