-
-
Save acnalesso/29860b8ca4814ff107e5 to your computer and use it in GitHub Desktop.
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
require 'logger' | |
$logger = Logger.new(STDOUT) | |
require 'active_support/cache' | |
$cache = ActiveSupport::Cache.lookup_store(:memory_store) | |
$cache.logger = $logger | |
$cache.clear | |
class EtagMiddleware | |
def initialize(app, options = {}, &block) | |
@app = app | |
@cache = options.fetch(:cache, &block) | |
@cache_key_prefix = options.fetch(:cache_key_prefix, :faraday_etags) | |
end | |
def call(env) | |
# Only cache "safe" requests | |
return @app.call(env) unless [:get, :head].include?(env[:method]) | |
cache_key = [ @cache_key_prefix, env[:url].to_s ] | |
cached = @cache.fetch(cache_key) | |
if cached | |
env[:request_headers]["If-None-Match"] ||= cached[:response_headers]["Etag"] | |
end | |
@app.call(env).on_complete do | |
if cached && env[:status] == 304 | |
env[:body] = cached[:body] | |
end | |
if !cached && env[:response_headers]["Etag"] | |
@cache.write(cache_key, env) | |
end | |
end | |
end | |
end | |
require 'faraday' | |
Faraday.register_middleware :request, :etag => EtagMiddleware | |
connection = Faraday.new(url: "http://localhost:4567") do |builder| | |
builder.response :logger, $logger | |
builder.request :etag, :cache => $cache | |
builder.adapter :net_http | |
end | |
2.times do | |
response = connection.get "/" | |
response.on_complete do | |
puts "---" | |
p response.body | |
puts "---" | |
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
require 'sinatra' | |
get "/" do | |
puts "Got this If-None-Match header: #{env["HTTP_IF_NONE_MATCH"].inspect}" | |
etag "abcdef" | |
"Hello from Sinatra" | |
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
Got this If-None-Match header: nil | |
localhost - - [22/Apr/2012:14:19:14 CEST] "GET / HTTP/1.1" 200 18 | |
- -> / | |
Got this If-None-Match header: "\"abcdef\"" | |
localhost - - [22/Apr/2012:14:19:14 CEST] "GET / HTTP/1.1" 304 0 | |
- -> / |
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
I, [2012-04-22T14:19:14.360599 #92297] INFO -- : get http://localhost:4567/ | |
D, [2012-04-22T14:19:14.360696 #92297] DEBUG -- request: | |
D, [2012-04-22T14:19:14.360781 #92297] DEBUG -- : Cache read: [:faraday_etags, "http://localhost:4567/"] | |
D, [2012-04-22T14:19:14.364850 #92297] DEBUG -- : Cache write: [:faraday_etags, "http://localhost:4567/"] | |
I, [2012-04-22T14:19:14.365021 #92297] INFO -- Status: 200 | |
D, [2012-04-22T14:19:14.365072 #92297] DEBUG -- response: x-frame-options: "sameorigin" | |
x-xss-protection: "1; mode=block" | |
content-type: "text/html;charset=utf-8" | |
etag: "\"abcdef\"" | |
content-length: "18" | |
server: "WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)" | |
date: "Sun, 22 Apr 2012 12:19:14 GMT" | |
connection: "close" | |
--- | |
"Hello from Sinatra" | |
--- | |
I, [2012-04-22T14:19:14.365193 #92297] INFO -- : get http://localhost:4567/ | |
D, [2012-04-22T14:19:14.365221 #92297] DEBUG -- request: | |
D, [2012-04-22T14:19:14.365262 #92297] DEBUG -- : Cache read: [:faraday_etags, "http://localhost:4567/"] | |
I, [2012-04-22T14:19:14.367825 #92297] INFO -- Status: 304 | |
D, [2012-04-22T14:19:14.367876 #92297] DEBUG -- response: x-frame-options: "sameorigin" | |
x-xss-protection: "1; mode=block" | |
etag: "\"abcdef\"" | |
server: "WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)" | |
date: "Sun, 22 Apr 2012 12:19:14 GMT" | |
connection: "close" | |
--- | |
"Hello from Sinatra" | |
--- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment