class Gemirro::Cache
The Cache
class contains all method to store marshal informations into files.
@!attribute [r] root_path
@return [String]
Attributes
root_path[R]
Public Class Methods
new(path)
click to toggle source
Initialize cache root path
@param [String] path
# File lib/gemirro/cache.rb, line 19 def initialize(path) @root_path = path create_root_path end
Public Instance Methods
cache(key) { || ... }
click to toggle source
Cache
data
@param [String] key
@return [Mixed]
# File lib/gemirro/cache.rb, line 56 def cache(key) key_hash = key2hash(key) read(key_hash) || (write(key_hash, yield) if block_given?) end
create_root_path()
click to toggle source
Create root path
# File lib/gemirro/cache.rb, line 27 def create_root_path FileUtils.mkdir_p(@root_path) end
flush()
click to toggle source
Flush cache directory
# File lib/gemirro/cache.rb, line 34 def flush FileUtils.rm_rf(@root_path) create_root_path end
flush_key(key)
click to toggle source
Flush key
@param [String] key
# File lib/gemirro/cache.rb, line 44 def flush_key(key) path = key_path(key2hash(key)) FileUtils.rm_f(path) end
Private Instance Methods
key2hash(key)
click to toggle source
Convert key to hash
@param [String] key
@return [String]
# File lib/gemirro/cache.rb, line 70 def key2hash(key) Digest::MD5.hexdigest(key) end
key_path(key_hash)
click to toggle source
Path from key hash
@param [String] key_hash
@return [String]
# File lib/gemirro/cache.rb, line 81 def key_path(key_hash) File.join(@root_path, key_hash) end
read(key_hash)
click to toggle source
Read cache
@param [String] key_hash
@return [Mixed]
# File lib/gemirro/cache.rb, line 92 def read(key_hash) path = key_path(key_hash) Marshal.load(File.open(path)) if File.exist?(path) end
write(key_hash, value)
click to toggle source
write cache
@param [String] key_hash @param [Mixed] value
@return [Mixed]
# File lib/gemirro/cache.rb, line 105 def write(key_hash, value) return value if value.nil? || value.empty? File.open(key_path(key_hash), 'wb') do |f| Marshal.dump(value, f) end value end