Class: Resourceful::FileCacheManager

Inherits:
AbstractCacheManager show all
Defined in:
lib/resourceful/cache_manager.rb

Overview

Stores cache entries in a directory on the filesystem. Similarly to the InMemoryCacheManager there are no limits on storage, so this will eventually eat up all your disk!

Instance Method Summary collapse

Constructor Details

#initialize(location = "/tmp/resourceful") ⇒ FileCacheManager

Create a new FileCacheManager

Parameters:

  • location (String) (defaults to: "/tmp/resourceful")

    A directory on the filesystem to store cache entries. This directory will be created if it doesn’t exist



98
99
100
101
102
# File 'lib/resourceful/cache_manager.rb', line 98

def initialize(location="/tmp/resourceful")
  require 'fileutils'
  require 'yaml'
  @dir = FileUtils.mkdir_p(location)
end

Instance Method Details

#invalidate(uri) ⇒ Object



118
119
120
# File 'lib/resourceful/cache_manager.rb', line 118

def invalidate(uri);
  File.unlink(cache_file(uri));
end

#lookup(request) ⇒ Object



104
105
106
107
108
# File 'lib/resourceful/cache_manager.rb', line 104

def lookup(request)
  response = cache_entries_for(request)[request]
  response.authoritative = false if response
  response
end

#store(request, response) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/resourceful/cache_manager.rb', line 110

def store(request, response)
  return unless response.cacheable?

  entries = cache_entries_for(request)
  entries[request] = response
  File.open(cache_file(request.uri), "w") {|fh| fh.write( YAML.dump(entries) ) }
end