Class: Staticd::HTTPCache
- Inherits:
-
Object
- Object
- Staticd::HTTPCache
- Includes:
- Models
- Defined in:
- lib/staticd/http_cache.rb
Overview
Rack middleware to manage remote HTTP resources caching.
For each request, it will find the last release of the site associed with the requested domain name and cache the corresponding resource if not already cached.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(http_root, app) ⇒ HTTPCache
constructor
A new instance of HTTPCache.
Constructor Details
#initialize(http_root, app) ⇒ HTTPCache
Returns a new instance of HTTPCache.
14 15 16 17 18 19 20 |
# File 'lib/staticd/http_cache.rb', line 14 def initialize(http_root, app) @app = app @http_root = http_root raise "No HTTP root folder provided" unless @http_root raise "No rack app provided" unless @app end |
Instance Method Details
#_call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/staticd/http_cache.rb', line 26 def _call(env) @env = env # Change the Request Path to '/index.html' if root path is asked. @env["PATH_INFO"] = "/index.html" if @env["PATH_INFO"] == '/' # Get the release from the request Host header. release = Release.last( Release.site.domain_names.name => Rack::Request.new(@env).host ) return next_middleware unless release # Change the script name to include the site name and release version. @env["SCRIPT_NAME"] = "/#{release.site_name}/#{release}" req = Rack::Request.new(@env) cache_engine = CacheEngine.new(@http_root) # Do nothing else if the resource is already cached. return next_middleware if cache_engine.cached?(req.path) # Get the resource to cache. resource = Resource.first( Resource.routes.release_id => release.id, Resource.routes.path => req.path_info ) return next_middleware unless resource # Cache the resource. cache_engine.cache(req.path, resource.url) next_middleware end |
#call(env) ⇒ Object
22 23 24 |
# File 'lib/staticd/http_cache.rb', line 22 def call(env) dup._call(env) end |