Skip to content

Instantly share code, notes, and snippets.

@iain
Created April 22, 2012 12:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save iain/2463912 to your computer and use it in GitHub Desktop.
Save iain/2463912 to your computer and use it in GitHub Desktop.
A small sample of Faraday using Etags for caching
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
require 'sinatra'
get "/" do
puts "Got this If-None-Match header: #{env["HTTP_IF_NONE_MATCH"].inspect}"
etag "abcdef"
"Hello from Sinatra"
end
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
- -> /
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"
---
@grosser
Copy link

grosser commented Jun 15, 2012

I made this into a pull request for faraday_middleware (also added some specs and fixed a lil bug)

https://github.com/pengwynn/faraday_middleware/pull/42

@iain
Copy link
Author

iain commented Jun 15, 2012

Nice! Thanks! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment