Cache responses

This commit is contained in:
Hakan Ensari 2017-01-03 13:37:40 +00:00
parent 34b631a0a4
commit 7b653b1fd8
8 changed files with 48 additions and 11 deletions

View File

@ -1,4 +1,4 @@
FROM ruby:2.3.1
FROM ruby:2.4.0
RUN mkdir /app
WORKDIR /app
@ -6,4 +6,4 @@ ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install --without development test
ADD . /app
CMD ./wait-for-it.sh db:5432 -- unicorn -c /app/config/unicorn.rb
CMD ./wait-for-it.sh db:5432 -s -- wait-for-it.sh memcache:11211 -s -- unicorn -c /app/config/unicorn.rb

View File

@ -4,7 +4,9 @@ source 'http://rubygems.org'
ruby '2.4.0'
gem 'dalli'
gem 'fixer'
gem 'kgio'
gem 'newrelic_rpm'
gem 'oj'
gem 'rack-cors'

View File

@ -4,6 +4,7 @@ GEM
ansi (1.5.0)
ast (2.3.0)
coderay (1.1.1)
dalli (2.7.6)
fixer (0.6.0)
oga (~> 2.0)
kgio (2.10.0)
@ -64,7 +65,9 @@ PLATFORMS
ruby
DEPENDENCIES
dalli
fixer
kgio
minitest
minitest-around
newrelic_rpm

View File

@ -1,11 +1,12 @@
# frozen_string_literal: true
require 'dalli'
require 'pathname'
# Encapsulates app configuration
module App
class << self
attr_reader :version, :released_at
attr_reader :cache, :version, :released_at
def env
ENV['RACK_ENV'] || 'development'
@ -16,6 +17,7 @@ module App
end
end
@version = `git rev-parse --short HEAD 2>/dev/null`.strip!
@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

@ -2,6 +2,8 @@ version: '2'
services:
db:
image: postgres
memcache:
image: memcached
web:
build: .
volumes:
@ -9,7 +11,9 @@ services:
ports:
- 8080:8080
environment:
- RACK_ENV=production
- DATABASE_URL=postgres://postgres@db/postgres
depends_on:
RACK_ENV: production
DATABASE_URL: postgres://postgres@db/postgres
MEMCACHE_SERVERS: memcache
links:
- db
- memcache

View File

@ -5,6 +5,8 @@ require 'sinatra'
require 'rack/cors'
require 'quote'
set :cache_ttl, 900 # 15 minutes
configure :development do
set :show_exceptions, :after_handler
end
@ -50,6 +52,12 @@ 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
@ -69,13 +77,17 @@ get '/' do
end
get '/latest' do
cache do
last_modified quote_attributes[:date]
jsonp quote_attributes
end
end
get(/(?<date>\d{4}-\d{2}-\d{2})/) do
cache do
last_modified quote_attributes[:date]
jsonp quote_attributes
end
end
not_found do

View File

@ -11,6 +11,10 @@ describe 'the API' do
let(:json) { Oj.load(last_response.body) }
let(:headers) { last_response.headers }
before do
App.cache.flush
end
it 'describes itself' do
get '/'
last_response.must_be :ok?
@ -86,4 +90,10 @@ describe 'the API' do
get '/latest?from=GBP&to=USD&amount=100'
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
end

View File

@ -10,6 +10,10 @@ describe 'the API' do
let(:app) { Sinatra::Application }
let(:json) { Oj.load(last_response.body) }
before do
App.cache.flush
end
it 'handles unfound pages' do
get '/foo'
last_response.status.must_equal 404