Class: Cacho::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/cacho.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DB

Returns a new instance of DB.



182
183
184
185
186
# File 'lib/cacho.rb', line 182

def initialize(path)
  @path = File.expand_path(path)

  FileUtils.mkdir_p(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



180
181
182
# File 'lib/cacho.rb', line 180

def path
  @path
end

Instance Method Details

#get(verb, uri) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cacho.rb', line 188

def get(verb, uri)
  parts = [
    "#{uri.host}-#{uri.port}",
    verb.to_s,
    Digest::MD5.hexdigest(uri.to_s)
  ]

  doc_path = File.join(@path, *parts)

  FileUtils.mkdir_p(File.dirname(doc_path)) if doc_path.start_with?(@path)

  if File.exist?(doc_path)
    str = File.read(doc_path)

    return Marshal.load(str)
  else
    value = yield

    File.write(doc_path, Marshal.dump(value))

    return value
  end
end