middleware/cache: cache 0 will be capped at 5 (#408)

* middleware/cache: cache 0 will be capped at 5

cache 0 would return TTL=0 records, up that to the documented minimum of
5 seconds.

* middleware/cache: check for 0 TTL

Handle 0 TTL differently and return an error, we might need to
special case this in the future.
This commit is contained in:
Miek Gieben 2016-11-09 10:01:26 +00:00 committed by GitHub
parent da742ed596
commit 6abbe231e5
5 changed files with 27 additions and 6 deletions

View file

@ -1,6 +1,7 @@
package cache
import (
"fmt"
"strconv"
"time"
@ -49,6 +50,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
// first args may be just a number, then it is the ttl, if not it is a zone
ttl, err := strconv.Atoi(args[0])
if err == nil {
// Reserve 0 (and smaller for future things)
if ttl <= 0 {
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", ttl)
}
ca.pttl = time.Duration(ttl) * time.Second
ca.nttl = time.Duration(ttl) * time.Second
args = args[1:]
@ -77,6 +82,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
if err != nil {
return nil, err
}
// Reserve 0 (and smaller for future things)
if pttl <= 0 {
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", pttl)
}
ca.pttl = time.Duration(pttl) * time.Second
}
case Denial:
@ -94,6 +103,10 @@ func cacheParse(c *caddy.Controller) (*Cache, error) {
if err != nil {
return nil, err
}
// Reserve 0 (and smaller for future things)
if nttl <= 0 {
return nil, fmt.Errorf("cache TTL can not be zero or negative: %d", nttl)
}
ca.nttl = time.Duration(nttl) * time.Second
}
default: