Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e77bb11
commit eae9552
Showing
9 changed files
with
106 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
class Restfulie::Client::Feature::Cache | ||
|
||
def execute(flow, request, response, env) | ||
Restfulie::Client.cache_provider.get([request.host, request.path], request) | ||
resp = flow.continue(request, response, env) | ||
unless resp.kind_of? Exception | ||
Restfulie::Client.cache_provider.put([request.host, request.path], request, response) | ||
end | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
lib/restfulie/server/action_controller/cacheable_responder.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Restfulie | ||
module Server | ||
module ActionController #:nodoc: | ||
module Trait #:nodoc: | ||
puts "loading trait..." | ||
Dir["#{File.dirname(__FILE__)}/trait/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class Expires | ||
def do_http_cache(responder, headers) | ||
if responder.options[:expires_in] | ||
headers << "max-age=#{responder.options[:expires_in]}" | ||
true | ||
else | ||
false | ||
end | ||
end | ||
end | ||
|
||
class LastModifieds | ||
# default implementation that will check whether caching can be applied | ||
def accepts?(responder) | ||
responder.resources.flatten.select do |resource| | ||
resource.respond_to?(:updated_at) | ||
end && | ||
responder.controller.response.last_modified.nil? && | ||
!new_record?(responder) | ||
end | ||
|
||
def do_http_cache(responder, headers) | ||
return false unless accepts?(responder) | ||
|
||
timestamps = responder.resources.flatten.select do |resource| | ||
resource.respond_to?(:updated_at) | ||
end.map do |resource| | ||
(resource.updated_at || Time.now).utc | ||
end | ||
|
||
timestamp = timestamps.max | ||
|
||
responder.controller.response.last_modified = timestamp if timestamp | ||
end | ||
|
||
def new_record?(responder) | ||
responder.resource.respond_to?(:new_record?) && responder.resource.new_record? | ||
end | ||
|
||
end | ||
|
||
module Restfulie::Server::ActionController | ||
module Trait | ||
module Cacheable | ||
|
||
CACHES = [::Expires.new, ::LastModifieds.new] | ||
|
||
def to_format | ||
headers = cache_control_headers | ||
cached = CACHES.inject(false) do |cached, cache| | ||
cached || cache.do_http_cache(self, headers) | ||
end | ||
if ::ActionController::Base.perform_caching && cached | ||
set_public_cache_control(headers) | ||
controller.response.headers["Cache-Control"] = headers.join(', ') | ||
fresh = request.fresh?(controller.response) | ||
if fresh | ||
head :not_modified | ||
else | ||
super | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_public_cache_control(headers) | ||
headers.delete("private") | ||
headers.delete("no-cache") | ||
headers << "public" | ||
end | ||
|
||
def cache_control_headers | ||
(controller.response.headers["Cache-Control"] || "").split(",").map {|k| k.strip } | ||
end | ||
|
||
end | ||
end | ||
end |