Class: Cache::YAMLCache
- Inherits:
-
Object
- Object
- Cache::YAMLCache
- Defined in:
- lib/jiji/util/cache.rb
Instance Method Summary collapse
-
#initialize(delegate, range, cache_file_prefix, regex = ".*", &block) ⇒ YAMLCache
constructor
A new instance of YAMLCache.
- #method_missing(name, *args) ⇒ Object
Constructor Details
#initialize(delegate, range, cache_file_prefix, regex = ".*", &block) ⇒ YAMLCache
Returns a new instance of YAMLCache.
43 44 45 46 47 48 49 |
# File 'lib/jiji/util/cache.rb', line 43 def initialize(delegate, range, cache_file_prefix, regex=".*", &block) @delegate = delegate @range = range @cache_file_prefix = cache_file_prefix @regex = regex @block = block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jiji/util/cache.rb', line 51 def method_missing(name, *args ) return @delegate.send( name, *args ) if ( !name.to_s.match(@regex) ) cache_file = @cache_file_prefix + "_" + name.to_s cache_file += "_" + @block.call(name,*args) if @block != nil cache_file += ".yaml" flock = FileLock.new(cache_file + ".lock") current = Time.new res = nil flock.writelock(){ # キャッシュがあればキャッシュから読む if ( File.exist?(cache_file) && current.to_i < (File.mtime(cache_file).to_i + @range) ) res = YAML.load_file cache_file elsif res = @delegate.send( name, *args ) File.open( cache_file, "w" ) { |f| f.write( YAML.dump(res) ) } File.utime(current, current, cache_file) end } return res end |