module HeapInfo::Cache::ClassMethods
Define class methods.
Public Instance Methods
clear_all()
click to toggle source
Clear the cache directory. @return [void]
# File lib/heapinfo/cache.rb, line 49 def clear_all FileUtils.rm_rf(CACHE_DIR) end
key_libc_info(libc_path)
click to toggle source
Get the key for storing libc info.
@param [String] libc_path The realpath to libc file. @return [String] The key for cache to read/write.
# File lib/heapinfo/cache.rb, line 19 def key_libc_info(libc_path) File.join('libc', Digest::MD5.hexdigest(IO.binread(libc_path)), 'info') end
read(key)
click to toggle source
Read cache from file.
@param [String] key In file path format, only accept +[w/]+ to prevent horrible things. @return [Object, nil] Value that recorded, return nil
when cache miss.
# File lib/heapinfo/cache.rb, line 39 def read(key) filepath = realpath(key) return unless File.file?(filepath) Marshal.load(IO.binread(filepath)) rescue TypeError, ArgumentError nil # handle if file content is invalid end
write(key, value)
click to toggle source
Write cache to file.
@param [String] key In file path format, only accept +[w/]+ to prevent horrible things. @param [Object] value value
will be stored with +Marshal#dump+. @return [Boolean] true
# File lib/heapinfo/cache.rb, line 28 def write(key, value) filepath = realpath(key) FileUtils.mkdir_p(File.dirname(filepath)) IO.binwrite(filepath, Marshal.dump(value)) true end
Private Instance Methods
init()
click to toggle source
@return [void]
# File lib/heapinfo/cache.rb, line 56 def init FileUtils.mkdir_p(CACHE_DIR) rescue Errno::EACCES # To prevent ~/ is not writable. __send__(:remove_const, :CACHE_DIR) const_set(:CACHE_DIR, File.join(HeapInfo::TMP_DIR, '.cache/heapinfo')) FileUtils.mkdir_p(CACHE_DIR) end
realpath(key)
click to toggle source
@param [String] key @return [String] Prepend with {HeapInfo::Cache::CACHE_DIR}.
# File lib/heapinfo/cache.rb, line 67 def realpath(key) raise ArgumentError, 'Invalid key(file path)' if key =~ %r{[^\w/]} File.join(CACHE_DIR, key) end