replace in-memory cache with size-limited lrucache

Fixes #4
This commit is contained in:
Will Norris 2017-09-17 11:00:49 +00:00
parent db55cb60f8
commit a57047ff22
2 changed files with 33 additions and 4 deletions

View file

@ -23,12 +23,14 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/PaulARoy/azurestoragecache"
"github.com/die-net/lrucache"
"github.com/diegomarangoni/gcscache"
"github.com/garyburd/redigo/redis"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
rediscache "github.com/gregjones/httpcache/redis"
"github.com/peterbourgon/diskv"
@ -36,6 +38,8 @@ import (
"willnorris.com/go/imageproxy/internal/s3cache"
)
const defaultMemorySize = 100
var addr = flag.String("addr", "localhost:8080", "TCP address to listen on")
var whitelist = flag.String("whitelist", "", "comma separated list of allowed remote hosts")
var referrers = flag.String("referrers", "", "comma separated list of allowed referring hosts")
@ -102,7 +106,7 @@ func parseCache() (imageproxy.Cache, error) {
}
if *cache == "memory" {
return httpcache.NewMemoryCache(), nil
*cache = fmt.Sprintf("memory:%d", defaultMemorySize)
}
u, err := url.Parse(*cache)
@ -115,6 +119,8 @@ func parseCache() (imageproxy.Cache, error) {
return azurestoragecache.New("", "", u.Host)
case "gcs":
return gcscache.New(u.String()), nil
case "memory":
return lruCache(u.Opaque)
case "redis":
conn, err := redis.DialURL(u.String(), redis.DialPassword(os.Getenv("REDIS_PASSWORD")))
if err != nil {
@ -130,6 +136,26 @@ func parseCache() (imageproxy.Cache, error) {
}
}
// lruCache creates an LRU Cache with the specified options of the form
// "maxSize:maxAge". maxSize is specified in megabytes, maxAge is a duration.
func lruCache(options string) (*lrucache.LruCache, error) {
parts := strings.SplitN(options, ":", 2)
size, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, err
}
var age time.Duration
if len(parts) > 1 {
age, err = time.ParseDuration(parts[1])
if err != nil {
return nil, err
}
}
return lrucache.New(size*1e6, int64(age.Seconds())), nil
}
func diskCache(path string) *diskcache.Cache {
d := diskv.New(diskv.Options{
BasePath: path,