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.
Files changed (3) hide show
  1. data/README.rdoc +21 -13
  2. data/lib/proc-cache.rb +8 -5
  3. metadata +2 -2
@@ -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
- Description goes here.
8
+ = いろんな書き方
9
+ proc{ ... }.cache!
10
+ lambda{ ... }.cache!
11
+ Proc.new{ ... }.cache!
4
12
 
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
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
- == Copyright
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.
@@ -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 = tmp_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 tmp_cache_path
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
-
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - kimoto