mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-04-29 06:46:23 +02:00
parent
db55cb60f8
commit
a57047ff22
2 changed files with 33 additions and 4 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue