Skip to content

Commit

Permalink
Use rack-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Jan 10, 2017
1 parent 7504abe commit 4dd1ace
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -8,6 +8,7 @@ gem 'fixer'
gem 'kgio'
gem 'newrelic_rpm'
gem 'oj'
gem 'rack-cache'
gem 'rack-cors'
gem 'rake'
gem 'sequel_pg'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +67,7 @@ DEPENDENCIES
newrelic_rpm
oj
pry
rack-cache
rack-cors
rack-test
rake
Expand Down
6 changes: 1 addition & 5 deletions config/app.rb
@@ -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'
Expand All @@ -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
47 changes: 21 additions & 26 deletions lib/api.rb
@@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions spec/api_spec.rb
@@ -1,5 +1,4 @@
# frozen_string_literal: true

require_relative 'helper'
require 'rack/test'
require 'api'
Expand All @@ -12,7 +11,7 @@
let(:headers) { last_response.headers }

before do
App.cache.flush
Dalli::Client.new.flush
end

it 'describes itself' do
Expand Down Expand Up @@ -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', '*'
Expand All @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions spec/edge_cases_spec.rb
@@ -1,5 +1,4 @@
# frozen_string_literal: true

require_relative 'helper'
require 'rack/test'
require 'api'
Expand All @@ -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
Expand Down

0 comments on commit 4dd1ace

Please sign in to comment.