class Async::HTTP::Cache::Response

Constants

CACHE_CONTROL
ETAG
X_CACHE

Attributes

generated_at[R]

Public Class Methods

new(response, body) click to toggle source
Calls superclass method
# File lib/async/http/cache/response.rb, line 36
def initialize(response, body)
        @generated_at = Async::Clock.now
        
        super(
                response.version,
                response.status,
                response.headers.flatten,
                body,
                response.protocol
        )
        
        @max_age = @headers[CACHE_CONTROL]&.max_age
        @etag = nil
        
        @headers.set(X_CACHE, 'hit')
end

Public Instance Methods

age() click to toggle source
# File lib/async/http/cache/response.rb, line 77
def age
        Async::Clock.now - @generated_at
end
cachable?() click to toggle source
# File lib/async/http/cache/response.rb, line 59
def cachable?
        if cache_control = @headers[CACHE_CONTROL]
                if cache_control.private? || !cache_control.public?
                        return false
                end
        else
                # No cache control header...
                return false
        end
        
        if set_cookie = @headers[SET_COOKIE]
                Console.logger.warn(self) {"Cannot cache response with set-cookie header!"}
                return false
        end
        
        return true
end
dup() click to toggle source
Calls superclass method
# File lib/async/http/cache/response.rb, line 87
def dup
        dup = super
        
        dup.body = @body.dup
        dup.headers = @headers.dup
        
        return dup
end
etag() click to toggle source
# File lib/async/http/cache/response.rb, line 55
def etag
        @etag ||= @headers[ETAG]
end
expired?() click to toggle source
# File lib/async/http/cache/response.rb, line 81
def expired?
        if @max_age
                self.age > @max_age
        end
end