Module: PersistentMemoize
- Defined in:
- lib/persistent_memoize.rb,
lib/persistent_memoize/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#memoize(name, path) ⇒ Object
Memoize the method ‘name’, with results stored in files under ‘path’.
Instance Method Details
#memoize(name, path) ⇒ Object
Memoize the method ‘name’, with results stored in files under ‘path’
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/persistent_memoize.rb', line 7 def memoize(name, path) unless File.exists?(path) FileUtils.mkdir_p(path) end (class<<self; self; end).send(:define_method, name) do |*args| key = Digest::MD5.hexdigest(Marshal.dump(args)) cacheFile = File.join(path, key) if File.exists?(cacheFile) results = File.open(cacheFile, 'rb'){ |f| Marshal.load(f) } else results = super(*args) File.open(cacheFile, 'wb'){ |f| Marshal.dump(results, f) } end results end end |