This repository has been archived on 2021-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
d20pfsrd/lib/cache.rb

19 lines
373 B
Ruby

require "fileutils"
module Cache
DIR = File.join(ENV["HOME"], ".cache", "d20pfsrd")
def cache(k, &block)
FileUtils.mkdir_p(DIR)
encoded_k = k.to_s.gsub(/\W/, "_")
cache_file = File.join(DIR, encoded_k)
if File.exist?(cache_file)
File.read(cache_file)
else
out = block.call
File.write(cache_file, out)
out
end
end
end