Permalink
Browse files

Use rack-cache

  • Loading branch information...
1 parent 7504abe commit 4dd1acec95937434a6c0d35fc0d7843188abead7 @hakanensari committed Jan 9, 2017
Showing with 41 additions and 42 deletions.
  1. +1 −0 Gemfile
  2. +3 −0 Gemfile.lock
  3. +1 −5 config/app.rb
  4. +21 −26 lib/api.rb
  5. +14 −9 spec/api_spec.rb
  6. +1 −2 spec/edge_cases_spec.rb
View
@@ -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
@@ -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
@@ -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 @@ def root
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
@@ -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 @@ def jsonp(data)
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]
- jsonp quote_attributes
- end
+ last_modified quote.date
+ jsonp quote_attributes
end
get(/(?<date>\d{4}-\d{2}-\d{2})/) do
- cache do
- last_modified quote_attributes[:date]
- jsonp quote_attributes
- end
+ last_modified quote.date
+ jsonp quote_attributes
end
not_found do
View
@@ -1,5 +1,4 @@
# frozen_string_literal: true
-
require_relative 'helper'
require 'rack/test'
require 'api'
@@ -12,7 +11,7 @@
let(:headers) { last_response.headers }
before do
- App.cache.flush
+ Dalli::Client.new.flush
end
it 'describes itself' do
@@ -61,13 +60,25 @@
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 @@
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'
@@ -1,5 +1,4 @@
# frozen_string_literal: true
-
require_relative 'helper'
require 'rack/test'
require 'api'
@@ -11,7 +10,7 @@
let(:json) { Oj.load(last_response.body) }
before do
- App.cache.flush
+ Dalli::Client.new.flush
end
it 'handles unfound pages' do

0 comments on commit 4dd1ace

Please sign in to comment.