Class: AgentX::Cache

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

Defined Under Namespace

Classes: Database

Class Method Summary collapse

Class Method Details

.dbObject



20
21
22
# File 'lib/agentx/cache.rb', line 20

def self.db
  @db ||= Database.new
end

.path(request) ⇒ Object



16
17
18
# File 'lib/agentx/cache.rb', line 16

def self.path(request)
  File.join(store_path, "#{request.cache_key}.json")
end

.read(request) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/agentx/cache.rb', line 36

def self.read(request)
  if h = db.read(request.cache_key)
    Response.new(
      h['response_code'],
      h['response_body'],
      Oj.load(h['response_headers']))
  end
end

.store_pathObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/agentx/cache.rb', line 4

def self.store_path
  return @store_path if @store_path

  @store_path = File.join(AgentX.root, 'cache')

  unless Dir.exists?(@store_path)
    Dir.mkdir(@store_path)
  end

  @store_path
end

.write(request, response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/agentx/cache.rb', line 24

def self.write(request, response)
  db.write(
    request_cache_key:       request.cache_key,
    request_host:            request.host,
    request_base_url:        request.base_url,
    response_code:           response.code,
    response_headers:        Oj.dump(response.headers.to_hash),
    response_content_length: response.body.length,
    response_expires_at:     response.expires_at,
    response_body:           response.body)
end