rely on httpcache for response caching

This commit is contained in:
Will Norris 2013-12-25 18:48:10 -08:00
parent b9584c18cb
commit 73f6357cda
5 changed files with 43 additions and 119 deletions

37
proxy/cache.go Normal file
View file

@ -0,0 +1,37 @@
// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxy
// The Cache interface defines a cache for storing arbitrary data. The
// interface is designed to align with httpcache.Cache.
type Cache interface {
// Get retrieves the cached data for the provided key.
Get(key string) (data []byte, ok bool)
// Set caches the provided data.
Set(key string, data []byte)
// Delete deletes the cached data at the specified key.
Delete(key string)
}
// NopCache provides a no-op cache implementation that doesn't actually cache anything.
var NopCache = new(nopCache)
type nopCache struct{}
func (c nopCache) Get(string) ([]byte, bool) { return nil, false }
func (c nopCache) Set(string, []byte) {}
func (c nopCache) Delete(string) {}

View file

@ -26,7 +26,7 @@ import (
"time"
"github.com/golang/glog"
"github.com/willnorris/go-imageproxy/cache"
"github.com/gregjones/httpcache"
"github.com/willnorris/go-imageproxy/data"
"github.com/willnorris/go-imageproxy/transform"
)
@ -79,7 +79,7 @@ func NewRequest(r *http.Request) (*data.Request, error) {
// Proxy serves image requests.
type Proxy struct {
Client *http.Client // client used to fetch remote URLs
Cache cache.Cache
Cache Cache
// Whitelist specifies a list of remote hosts that images can be proxied from. An empty list means all hosts are allowed.
Whitelist []string
@ -90,11 +90,24 @@ type Proxy struct {
// NewProxy constructs a new proxy. The provided http Client will be used to
// fetch remote URLs. If nil is provided, http.DefaultClient will be used.
func NewProxy(client *http.Client) *Proxy {
func NewProxy(client *http.Client, cache Cache) *Proxy {
if client == nil {
client = http.DefaultClient
}
return &Proxy{Client: client, Cache: cache.NopCache}
if cache == nil {
cache = NopCache
}
return &Proxy{
Client: &http.Client{
Transport: &httpcache.Transport{
Transport: client.Transport,
Cache: cache,
MarkCachedResponses: true,
},
},
Cache: cache,
}
}
// ServeHTTP handles image requests.
@ -122,23 +135,11 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
image, ok := p.Cache.Get(u)
if !ok {
glog.Infof("image not cached")
image, err = p.fetchRemoteImage(u, nil)
if err != nil {
glog.Errorf("error fetching remote image: %v", err)
}
p.Cache.Save(image)
} else if time.Now().After(image.Expires) {
glog.Infof("cached image expired")
image, err = p.fetchRemoteImage(u, image)
if err != nil {
glog.Errorf("error fetching remote image: %v", err)
}
p.Cache.Save(image)
} else {
glog.Infof("serving from cache")
image, err := p.fetchRemoteImage(u)
if err != nil {
glog.Errorf("error fetching remote image: %v", err)
http.Error(w, fmt.Sprintf("Error fetching remote image: %v", err), http.StatusInternalServerError)
return
}
image, _ = transform.Transform(*image, req.Options)
@ -148,29 +149,13 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(image.Bytes)
}
func (p *Proxy) fetchRemoteImage(u string, cached *data.Image) (*data.Image, error) {
func (p *Proxy) fetchRemoteImage(u string) (*data.Image, error) {
glog.Infof("fetching remote image: %s", u)
req, err := http.NewRequest("GET", u, nil)
resp, err := p.Client.Get(u)
if err != nil {
return nil, err
}
if cached != nil && cached.Etag != "" {
req.Header.Add("If-None-Match", cached.Etag)
}
resp, err := p.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusNotModified {
glog.Infof("remote image not modified (304 response)")
cached.Expires = parseExpires(resp)
return cached, nil
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("HTTP status not OK: %v", resp.Status))
}