diff --git a/README.md b/README.md index 0cacac7..66e26bc 100644 --- a/README.md +++ b/README.md @@ -140,12 +140,19 @@ whitelist (meaning any remote URL can be proxied). Test this by navigating to and you should see a 500px square coder octocat. -### Disk cache ### +### Cache ### -By default, the imageproxy command uses an in-memory cache that will grow -unbounded. To cache images on disk instead, include the `cacheDir` flag: +By default, the imageproxy command does not cache responses, but caching can be +enabled using the `-cache` flag. It supports the following values: - imageproxy -cacheDir /tmp/imageproxy + - `memory` - uses an in-memory cache. (This can exhaust your system's + available memory and is not recommended for production systems) + - directory on local disk (e.g. `/tmp/imageproxy`) - will cache images + on disk, limited to the size specified in the `-cacheSize` flag. + +For example, to cache files on disk, allowing up to 100MB of space: + + imageproxy -cache /tmp/imageproxy -cacheSize 100 Reload the [codercat URL][], and then inspect the contents of `/tmp/imageproxy`. There should be two files there, one for the original @@ -153,10 +160,6 @@ full-size codercat image, and one for the resized 500px version. [codercat URL]: http://localhost:8080/500/https://octodex.github.com/images/codercat.jpg -By default the disk cache will grow to 100MB. You can change this size by using the `-cacheSize` flag. The following example will allow the cache to take up 500MB of space. - - imageproxy -cacheDir /tmp/imageproxy -cacheSize 500 - ### Referrer Whitelist ### You can limit images to only be accessible for certain hosts in the HTTP referrer header. This may be useful to prevent others from hotlinking to images, and using your valuable bandwidth! It can be enabled be running: diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index ab4271e..ec7ff29 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -43,7 +43,8 @@ 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") var baseURL = flag.String("baseURL", "", "default base URL for relative remote URLs") -var cacheDir = flag.String("cacheDir", "", "directory to use for file cache") +var cache = flag.String("cache", "", "location to cache images (see https://github.com/willnorris/imageproxy#cache)") +var cacheDir = flag.String("cacheDir", "", "(Deprecated; use 'cache' instead) directory to use for file cache") var cacheSize = flag.Uint64("cacheSize", 100, "maximum size of file cache (in MB)") var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures") var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions") @@ -57,15 +58,9 @@ func main() { return } - var c httpcache.Cache - if *cacheDir != "" { - d := diskv.New(diskv.Options{ - BasePath: *cacheDir, - CacheSizeMax: *cacheSize * 1024 * 1024, - }) - c = diskcache.NewWithDiskv(d) - } else if *cacheSize != 0 { - c = httpcache.NewMemoryCache() + c, err := parseCache() + if err != nil { + log.Fatal(err) } p := imageproxy.NewProxy(nil, c) @@ -105,3 +100,37 @@ func main() { fmt.Printf("imageproxy (version %v) listening on %s\n", VERSION, server.Addr) log.Fatal(server.ListenAndServe()) } + +// parseCache parses the cache-related flags and returns the specified Cache implementation. +func parseCache() (imageproxy.Cache, error) { + if *cache == "" { + if *cacheDir != "" { + return diskCache(*cacheDir), nil + } + return nil, nil + } + + if *cache == "memory" { + return httpcache.NewMemoryCache(), nil + } + + u, err := url.Parse(*cache) + if err != nil { + return nil, fmt.Errorf("error parsing cache flag: %v", err) + } + + switch u.Scheme { + case "file": + fallthrough + default: + return diskCache(u.Path), nil + } +} + +func diskCache(path string) *diskcache.Cache { + d := diskv.New(diskv.Options{ + BasePath: path, + CacheSizeMax: *cacheSize * 1024 * 1024, + }) + return diskcache.NewWithDiskv(d) +}