Class: Gapi::FileCacheMiddle

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

Overview

wraps another middle, caches using MD5 hexdigest of path as key

Instance Method Summary collapse

Constructor Details

#initialize(actual_middle, cache_dir, uid) ⇒ FileCacheMiddle

Returns a new instance of FileCacheMiddle.



8
9
10
11
12
# File 'lib/gapi/file_cache_middle.rb', line 8

def initialize( actual_middle, cache_dir, uid )
  @actual_middle = actual_middle
  @cache_dir = Pathname.new( cache_dir )
  @uid = uid
end

Instance Method Details

#get(domain, path, opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gapi/file_cache_middle.rb', line 14

def get( domain, path, opts )
  data = opts.collect {|kv| kv.join('=') }.join('&')
  uri = path
  uri = "#{path}?#{data}" if data && data.length > 0
  cache_fname = cache_filename_for_uri( uri )
  code = 500
  body = ""
  if File.exists?( cache_fname )
    File.open( cache_fname, 'rb' ) do |f|
      body = f.read
      code = 200
    end
  else
    code, body = @actual_middle.get( domain, path, opts )
    if 200 == code
      File.open( cache_fname, 'wb' ) do |f|
        f.write( body )
      end
    end
  end
  return code, body
end