Class: Staticd::CacheEngine
- Inherits:
-
Object
- Object
- Staticd::CacheEngine
- Defined in:
- lib/staticd/cache_engine.rb
Overview
Class to manage HTTP resources caching.
Example:
cache_engine = CacheEngine.new("/tmp/cache")
unless cache.cached?("/index.html")
cache_engine.cache("/index.html", "http://storage.tld/0000000")
end
TODO: add a purge method based on file’s atime attribute
Instance Method Summary collapse
- #cache(resource_path, resource_url) ⇒ Object
- #cached?(resource_path) ⇒ Boolean
-
#initialize(http_root) ⇒ CacheEngine
constructor
A new instance of CacheEngine.
Constructor Details
#initialize(http_root) ⇒ CacheEngine
Returns a new instance of CacheEngine.
17 18 19 20 |
# File 'lib/staticd/cache_engine.rb', line 17 def initialize(http_root) @http_root = http_root check_cache_directory end |
Instance Method Details
#cache(resource_path, resource_url) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/staticd/cache_engine.rb', line 22 def cache(resource_path, resource_url) open(resource_url, "rb") do |resource| FileUtils.mkdir_p(File.dirname(local_path(resource_path))) File.open(local_path(resource_path), "w+") do |file| resource.each { |chunk| file.write(chunk) } end end end |
#cached?(resource_path) ⇒ Boolean
31 32 33 |
# File 'lib/staticd/cache_engine.rb', line 31 def cached?(resource_path) File.exist?(local_path(resource_path)) end |