Skip to content

Commit

Permalink
moved caching to middleware. implemented a response writer intercept …
Browse files Browse the repository at this point in the history
…strategy. the cacher interface needs to be thought about a bit more. #96
  • Loading branch information
ARolek committed Oct 16, 2017
1 parent 2eaaf5c commit 7fba4f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
5 changes: 3 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

// Cacher defines a cache back end
type Cacher interface {
Read(key string) (io.Reader, error)
Write(key string, value io.Reader) error
Get(key string) (io.Reader, error)
Set(key string, value io.Reader) error
Purge(key string) error
GetWriter(key string) (io.Writer, error)
}

// InitFunc initilize a cache given a config map.
Expand Down
19 changes: 17 additions & 2 deletions cache/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ type Filecache struct {
Basepath string
}

func (fc *Filecache) Read(key string) (io.Reader, error) {
func (fc *Filecache) Get(key string) (io.Reader, error) {
path := filepath.Join(fc.Basepath, key)

return os.Open(path)
}

func (fc *Filecache) Write(key string, value io.Reader) error {
func (fc *Filecache) Set(key string, value io.Reader) error {
var err error

// build our filepath
Expand All @@ -90,6 +90,21 @@ func (fc *Filecache) Write(key string, value io.Reader) error {
return nil
}

func (fc *Filecache) GetWriter(key string) (io.Writer, error) {
var err error

// build our filepath
path := filepath.Join(fc.Basepath, key)

// the key can have a directory syntax so we need to makeAll
if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return nil, err
}

// create the file
return os.Create(path)
}

func (fc *Filecache) Purge(key string) error {
path := filepath.Join(fc.Basepath, key)

Expand Down
46 changes: 9 additions & 37 deletions server/handle_map_zxy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package server

import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"strconv"
Expand Down Expand Up @@ -116,29 +114,6 @@ func (req HandleMapZXY) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// check if we have a cache backend setup
if Cache != nil {
key := fmt.Sprintf("/%v/%v/%v.pbf", req.z, req.x, req.y)
cachedTile, err := Cache.Read(key)
if err != nil {
// TODO: this should be a debug warning
// log.Printf("cache err: %v", err)
} else {
// TODO: how configurable do we want the CORS policy to be?
// set CORS header
w.Header().Add("Access-Control-Allow-Origin", "*")

// mimetype for protocol buffers
w.Header().Add("Content-Type", "application/x-protobuf")

// communicate the cache is being used
w.Header().Add("Tegola-Cache", "HIT")

io.Copy(w, cachedTile)
return
}
}

// new tile
tile := tegola.Tile{
Z: req.z,
Expand Down Expand Up @@ -177,7 +152,7 @@ func (req HandleMapZXY) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
// TODO: should we return an error to the response or just log the error?
// we can't just write to the response as the waitgroup is going to write to the respons as well
// we can't just write to the response as the waitgroup is going to write to the response as well
log.Printf("Error Getting MVTLayer for tile Z: %v, X: %v, Y: %v: %v", tile.Z, tile.X, tile.Y, err)
return
}
Expand Down Expand Up @@ -243,18 +218,15 @@ func (req HandleMapZXY) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(pbyte) > MaxTileSize {
log.Printf("tile z:%v, x:%v, y:%v is rather large - %v", tile.Z, tile.X, tile.Y, len(pbyte))
}

// check if we have a cache
if Cache != nil {
key := fmt.Sprintf("/%v/%v/%v.pbf", req.z, req.x, req.y)
r := bytes.NewReader(pbyte)

// write to our cache
if err := Cache.Write(key, r); err != nil {
log.Printf("cache err: %v", err)
/*
// check if we have a cache
if Cache != nil {
// write to our cache using the url path as the key
if err := Cache.Set(r.URL.Path, bytes.NewReader(pbyte)); err != nil {
log.Printf("cache err: %v", err)
}
}
}

*/
// log the request
L.Log(logItem{
X: tile.X,
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Start(port string) {
group.UsingContext().Handler("OPTIONS", "/capabilities/:map_name", HandleMapCapabilities{})

// map tiles
group.UsingContext().Handler("GET", "/maps/:map_name/:z/:x/:y", HandleMapZXY{})
group.UsingContext().Handler("GET", "/maps/:map_name/:z/:x/:y", CacheHandler(HandleMapZXY{}))
group.UsingContext().Handler("OPTIONS", "/maps/:map_name/:z/:x/:y", HandleMapZXY{})
group.UsingContext().Handler("GET", "/maps/:map_name/style.json", HandleMapStyle{})

Expand Down

0 comments on commit 7fba4f5

Please sign in to comment.