Permalink
Browse files

Setup for purge requests

  • Loading branch information...
1 parent 8915c2a commit 07937353989c74546107339c617a9e3ca2d9926d @tobi committed Jul 8, 2009
Showing with 30 additions and 10 deletions.
  1. +4 −0 config.ru
  2. +5 −9 image_server.rb
  3. +20 −0 lib/cache_purge.rb
  4. +1 −1 lib/image.rb
View
@@ -6,6 +6,8 @@ require 'rubygems'
require 'sinatra'
require 'logger'
require 'image_server'
+require 'lib/cache_purge'
+
OriginServer = 'static.shopify.com'
$logger = Logger.new(STDOUT)
@@ -15,4 +17,6 @@ use Rack::Cache,
:metastore => 'memcached://localhost:11211/meta',
:entitystore => 'file:/tmp/cache/rack/body'
+use CachePurge
+
run ImageServer.new
View
@@ -5,23 +5,20 @@
class ImageServer
NotFound = [404, {'Content-Type' => 'text/html'}, ['<h1>File not Found</h1>']]
+
def call(env)
-
+
request = Rack::Request.new(env)
- if request.request_method == "PURGE"
- return [404, {'Content-Type' => 'text/plain', 'Cache-Control' => 'private, must-revalidate, max-age=0'}, ['OK']]
- end
-
requested_file = RemoteImage.new(OriginServer, request.path, request.query_string)
# If file exists we simply sent it to the client.
if requested_file.download
$logger.info 'Hit: Direct'
- requested_file.to_response
+ return requested_file.to_response
# If it doesn't exist but it's an image and a variant was requested we will
# go look for the original image and resize it according to the request.
@@ -33,14 +30,13 @@ def call(env)
$logger.info "Hit: Origin, transformed:#{requested_file.variant}"
- origin_file.to_response
+ return origin_file.to_response
else
$logger.info 'Miss, original'
- NotFound
end
else
$logger.info 'Miss, requested'
- NotFound
end
+ NotFound
end
end
View
@@ -0,0 +1,20 @@
+
+class CachePurge
+ Success = [200, {'Content-Type' => 'text/plain'}, ['OK']]
+
+ def initialize(app)
+ @app = app
+ end
+
+ # PURGE /s/files/1/0001/4168/files/thumbs/pic_thumb.jpg?12428536032 HTTP/1.0
+
+ def call(env)
+ # Rack cache automatically invalidates resource if the verbs are not GET/POST so
+ # we don't actually have to do anything. Simply don't delegate those to the backend
+ if env['REQUEST_METHOD'] == 'PURGE'
+ Success
+ else
+ @app.call(env)
+ end
+ end
+end
View
@@ -58,7 +58,7 @@ def variant?
end
def to_response
- [200, {'Content-Type' => content_type, 'Content-Length' => content.length, 'Cache-Control' => cache_control}, [content]]
+ [200, {'Content-Type' => content_type, 'Content-Length' => content.length.to_s, 'Cache-Control' => cache_control}, [content]]
end
# Reverse the filename to find the original image from which we can generate the desired

0 comments on commit 0793735

Please sign in to comment.