Skip to content

Commit

Permalink
TTL is now using date type instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
Houziaux mike / Jenaye committed Aug 4, 2021
1 parent ab38615 commit b1f738b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cache/coalescing/requestCoalescing.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ServeResponse(
}
if "" != regexpURL {
u := retriever.GetConfiguration().GetUrls()[regexpURL]
if u.TTL != "" {
if u.TTL != 0 {
url.TTL = u.TTL
}
if len(u.Headers) != 0 {
Expand Down
7 changes: 1 addition & 6 deletions cache/providers/badgerProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ func (provider *Badger) Prefix(key string, req *http.Request) []byte {
// Set method will store the response in Badger provider
func (provider *Badger) Set(key string, value []byte, url t.URL, duration time.Duration) {
if duration == 0 {
ttl, err := time.ParseDuration(url.TTL)
if err != nil {
ttl = 0
fmt.Println(err)
}
duration = ttl
duration = url.TTL
}

err := provider.DB.Update(func(txn *badger.Txn) error {
Expand Down
7 changes: 1 addition & 6 deletions cache/providers/embeddedOlricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ func (provider *EmbeddedOlric) Get(key string) []byte {
// Set method will store the response in EmbeddedOlric provider
func (provider *EmbeddedOlric) Set(key string, value []byte, url t.URL, duration time.Duration) {
if duration == 0 {
ttl, err := time.ParseDuration(url.TTL)
if err != nil {
ttl = 0
fmt.Println(err)
}
duration = ttl
duration = url.TTL
}

err := provider.dm.PutEx(key, value, duration)
Expand Down
7 changes: 1 addition & 6 deletions cache/providers/olricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ func (provider *Olric) Get(key string) []byte {
// Set method will store the response in Olric provider
func (provider *Olric) Set(key string, value []byte, url t.URL, duration time.Duration) {
if duration == 0 {
ttl, err := time.ParseDuration(url.TTL)
if err != nil {
ttl = 0
fmt.Println(err)
}
duration = ttl
duration = url.TTL
}

err := provider.dm.PutEx(key, value, duration)
Expand Down
15 changes: 9 additions & 6 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package configurationtypes

import "go.uber.org/zap"
import (
"go.uber.org/zap"
"time"
)

// Port config
type Port struct {
Expand All @@ -21,8 +24,8 @@ type Regex struct {

//URL configuration
type URL struct {
TTL string `yaml:"ttl"`
Headers []string `yaml:"headers"`
TTL time.Duration `yaml:"ttl"`
Headers []string `yaml:"headers"`
}

//CacheProvider config
Expand All @@ -39,7 +42,7 @@ type DefaultCache struct {
Olric CacheProvider `yaml:"olric"`
Port Port `yaml:"port"`
Regex Regex `yaml:"regex"`
TTL string `yaml:"ttl"`
TTL time.Duration `yaml:"ttl"`
}

// GetDistributed returns if it uses Olric or not as provider
Expand All @@ -63,7 +66,7 @@ func (d *DefaultCache) GetRegex() Regex {
}

// GetTTL returns the default TTL
func (d *DefaultCache) GetTTL() string {
func (d *DefaultCache) GetTTL() time.Duration {
return d.TTL
}

Expand All @@ -73,7 +76,7 @@ type DefaultCacheInterface interface {
GetOlric() CacheProvider
GetHeaders() []string
GetRegex() Regex
GetTTL() string
GetTTL() time.Duration
}

// APIEndpoint is the minimal structure to define an endpoint
Expand Down
2 changes: 1 addition & 1 deletion plugins/caddy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *SouinApp) Provision(_ caddy.Context) error {

// Start will start the App
func (s SouinApp) Start() error {
if s.DefaultCache != nil && s.DefaultCache.TTL == "" {
if s.DefaultCache != nil && s.DefaultCache.TTL == 0 {
return new(defaultCacheError)
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions plugins/caddy/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caddy
import (
"github.com/darkweak/souin/configurationtypes"
"go.uber.org/zap"
"time"
)

// DefaultCache the struct
Expand All @@ -11,7 +12,7 @@ type DefaultCache struct {
Headers []string
Olric configurationtypes.CacheProvider
Regex configurationtypes.Regex
TTL string
TTL time.Duration
}

// GetDistributed returns if it uses Olric or not as provider
Expand All @@ -35,7 +36,7 @@ func (d *DefaultCache) GetRegex() configurationtypes.Regex {
}

// GetTTL returns the default TTL
func (d *DefaultCache) GetTTL() string {
func (d *DefaultCache) GetTTL() time.Duration {
return d.TTL
}

Expand Down
15 changes: 11 additions & 4 deletions plugins/caddy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http"
"net/http/httptest"
"sync"
"time"
)

type key string
Expand All @@ -40,7 +41,7 @@ type SouinCaddyPlugin struct {
bufPool *sync.Pool
Headers []string `json:"headers,omitempty"`
Olric configurationtypes.CacheProvider `json:"olric,omitempty"`
TTL string `json:"ttl,omitempty"`
TTL time.Duration `json:"ttl,omitempty"`
YKeys map[string]configurationtypes.YKey `json:"ykeys,omitempty"`
}

Expand Down Expand Up @@ -138,7 +139,7 @@ func (s *SouinCaddyPlugin) FromApp(app *SouinApp) error {
if dc.Headers == nil {
s.Configuration.DefaultCache.Headers = appDc.Headers
}
if dc.TTL == "" {
if dc.TTL == 0 {
s.Configuration.DefaultCache.TTL = appDc.TTL
}
if dc.Olric.URL == "" || dc.Olric.Path == "" || dc.Olric.Configuration == nil {
Expand Down Expand Up @@ -231,7 +232,10 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser, _ interface{}) (interfac
cfg.DefaultCache.Olric = provider
case "ttl":
args := h.RemainingArgs()
cfg.DefaultCache.TTL = args[0]
ttl, err := time.ParseDuration(args[0])
if err == nil {
cfg.DefaultCache.TTL = ttl
}
default:
return nil, h.Errf("unsupported root directive: %s", rootOption)
}
Expand All @@ -258,7 +262,10 @@ func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.Middlewar
case "headers":
sc.DefaultCache.Headers = h.RemainingArgs()
case "ttl":
sc.DefaultCache.TTL = h.RemainingArgs()[0]
ttl, err := time.ParseDuration(h.RemainingArgs()[0])
if err == nil {
sc.DefaultCache.TTL = ttl
}
}
}

Expand Down

0 comments on commit b1f738b

Please sign in to comment.