Use rack-cache

This commit is contained in:
Hakan Ensari 2017-01-09 01:30:24 +00:00
parent 7504abe6b4
commit 4dd1acec95
6 changed files with 41 additions and 42 deletions

View File

@ -8,6 +8,7 @@ gem 'fixer'
gem 'kgio'
gem 'newrelic_rpm'
gem 'oj'
gem 'rack-cache'
gem 'rack-cors'
gem 'rake'
gem 'sequel_pg'

View File

@ -21,6 +21,8 @@ GEM
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.5)
rack-cache (1.6.1)
rack (>= 0.4)
rack-cors (0.4.0)
rack-protection (1.5.3)
rack
@ -65,6 +67,7 @@ DEPENDENCIES
newrelic_rpm
oj
pry
rack-cache
rack-cors
rack-test
rake

View File

@ -1,12 +1,10 @@
# frozen_string_literal: true
require 'dalli'
require 'pathname'
# Encapsulates app configuration
module App
class << self
attr_reader :cache, :version, :released_at
attr_reader :version
def env
ENV['RACK_ENV'] || 'development'
@ -17,7 +15,5 @@ module App
end
end
@cache = Dalli::Client.new
@released_at = `git show -s --format=%ci HEAD`
@version = `git rev-parse --short HEAD 2>/dev/null`.strip!
end

View File

@ -1,11 +1,22 @@
# frozen_string_literal: true
require 'dalli'
require 'oj'
require 'sinatra'
require 'rack/cache'
require 'rack/cors'
require 'quote'
set :cache_ttl, 900 # 15 minutes
use Rack::Cache,
verbose: true,
metastore: "memcached://#{ENV['MEMCACHE_SERVERS']}/meta",
entitystore: "memcached://#{ENV['MEMCACHE_SERVERS']}/body"
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :get
end
end
configure :development do
set :show_exceptions, :after_handler
@ -46,46 +57,30 @@ helpers do
def encode_json(data)
Oj.dump(data, mode: :compat)
end
def cache
App.cache.fetch request.fullpath, settings.cache_ttl do
yield
end
end
end
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :get
end
end
options '*' do
200
end
before do
content_type 'application/json'
get '*' do
cache_control :public, :must_revalidate, max_age: 900
pass
end
get '/' do
last_modified App.released_at
etag App.version
jsonp details: 'http://fixer.io', version: App.version
end
get '/latest' do
cache do
last_modified quote_attributes[:date]
last_modified quote.date
jsonp quote_attributes
end
end
get(/(?<date>\d{4}-\d{2}-\d{2})/) do
cache do
last_modified quote_attributes[:date]
last_modified quote.date
jsonp quote_attributes
end
end
not_found do

View File

@ -1,5 +1,4 @@
# frozen_string_literal: true
require_relative 'helper'
require 'rack/test'
require 'api'
@ -12,7 +11,7 @@ describe 'the API' do
let(:headers) { last_response.headers }
before do
App.cache.flush
Dalli::Client.new.flush
end
it 'describes itself' do
@ -61,13 +60,25 @@ describe 'the API' do
json['rates'].wont_be :empty?
end
it 'returns a last modified header' do
it 'returns a cache control header' do
%w(/ /latest /2012-11-20).each do |path|
get path
headers['Cache-Control'].wont_be_nil
end
end
it 'returns a last modified header' do
%w(/latest /2012-11-20).each do |path|
get path
headers['Last-Modified'].wont_be_nil
end
end
it 'returns an etag header' do
get '/'
headers['ETag'].wont_be_nil
end
it 'allows cross-origin requests' do
%w(/ /latest /2012-11-20).each do |path|
header 'Origin', '*'
@ -91,12 +102,6 @@ describe 'the API' do
json['rates']['USD'].must_be :>, 100
end
it 'caches quotes' do
App.cache.set '/latest', 'foo'
get '/latest'
last_response.body.must_equal 'foo'
end
it 'sets Content-Type header to JSON when caching' do
2.times do
get '/latest'

View File

@ -1,5 +1,4 @@
# frozen_string_literal: true
require_relative 'helper'
require 'rack/test'
require 'api'
@ -11,7 +10,7 @@ describe 'the API' do
let(:json) { Oj.load(last_response.body) }
before do
App.cache.flush
Dalli::Client.new.flush
end
it 'handles unfound pages' do