class Staticd::CacheEngine
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
Public Class Methods
new(http_root)
click to toggle source
# File lib/staticd/cache_engine.rb, line 17 def initialize(http_root) @http_root = http_root check_cache_directory end
Public Instance Methods
cache(resource_path, resource_url)
click to toggle source
# 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)
click to toggle source
# File lib/staticd/cache_engine.rb, line 31 def cached?(resource_path) File.exist?(local_path(resource_path)) end
Private Instance Methods
check_cache_directory()
click to toggle source
# File lib/staticd/cache_engine.rb, line 41 def check_cache_directory FileUtils.mkdir_p(@http_root) unless File.directory?(@http_root) end
local_path(resource_path)
click to toggle source
# File lib/staticd/cache_engine.rb, line 37 def local_path(resource_path) @http_root + resource_path end