Skip to content

Commit

Permalink
fix: Set duration type for TTL (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Apr 30, 2021
1 parent 21872c2 commit 3da8a5a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ default_cache: # Required
port: # Ports on which Souin will be exposed
web: 80
tls: 443
ttl: 10 # Default TTL
ttl: 10s # Default TTL
reverse_proxy_url: 'http://traefik' # If it's in the same network you can use http://your-service, otherwise just use https://yourdomain.com
```
This is a fully working minimal configuration for a Souin instance
Expand Down Expand Up @@ -78,18 +78,19 @@ default_cache:
url: 'olric:3320' # Olric server
regex:
exclude: 'ARegexHere' # Regex to exclude from cache
ttl: 1000s # Default TTL
log_level: INFO # Logs verbosity [ DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL ], case do not matter
ssl_providers: # The {providers}.json to use
- traefik
urls:
'https:\/\/domain.com\/first-.+': # First regex route configuration
ttl: 1000 # Override default TTL
ttl: 1000s # Override default TTL
'https:\/\/domain.com\/second-route': # Second regex route configuration
ttl: 10 # Override default TTL
ttl: 10s # Override default TTL
headers: # Override default headers
- Authorization
'https?:\/\/mysubdomain\.domain\.com': # Third regex route configuration
ttl: 50 # Override default TTL
ttl: 50s # Override default TTL
headers: # Override default headers
- Authorization
- 'Content-Type'
Expand Down
10 changes: 7 additions & 3 deletions cache/providers/olricProvider.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package providers

import (
"fmt"
"github.com/buraksezer/olric/client"
"github.com/buraksezer/olric/config"
"github.com/darkweak/souin/cache/keysaver"
t "github.com/darkweak/souin/configurationtypes"
"strconv"
"time"
)

Expand Down Expand Up @@ -64,8 +64,12 @@ func (provider *Olric) Get(key string) []byte {
// Set method will store the response in Redis provider
func (provider *Olric) Set(key string, value []byte, url t.URL, duration time.Duration) {
if duration == 0 {
ttl, _ := strconv.Atoi(url.TTL)
duration = time.Duration(ttl) * time.Second
ttl, err := time.ParseDuration(url.TTL)
if err != nil {
ttl = 0
fmt.Println(err)
}
duration = ttl
}

err := provider.dm.PutEx(key, value, duration)
Expand Down
10 changes: 7 additions & 3 deletions cache/providers/ristrettoProvider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package providers

import (
"fmt"
"github.com/darkweak/souin/cache/keysaver"
t "github.com/darkweak/souin/configurationtypes"
"github.com/dgraph-io/ristretto"
"strconv"
"time"
)

Expand Down Expand Up @@ -55,8 +55,12 @@ func (provider *Ristretto) Get(key string) []byte {
// Set method will store the response in Ristretto provider
func (provider *Ristretto) Set(key string, value []byte, url t.URL, duration time.Duration) {
if duration == 0 {
ttl, _ := strconv.Atoi(url.TTL)
duration = time.Duration(ttl) * time.Second
ttl, err := time.ParseDuration(url.TTL)
if err != nil {
ttl = 0
fmt.Println(err)
}
duration = ttl
}
isSet := provider.SetWithTTL(key, value, 1, duration)
if !isSet {
Expand Down
8 changes: 4 additions & 4 deletions configuration/configuration.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ default_cache: # Required part
tls: 443
regex: # Regex configuration
exclude: 'ARegexHere' # Regex to exclude from cache
ttl: 10 # Default TTL
ttl: 10s # Default TTL
log_level: INFO # Logs verbosity [ DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL, debug, info, warn, error, dpanic, panic, fatal ]
reverse_proxy_url: 'http://traefik' # If it's in the same network you can use http://your-service. Then just use https://yourdomain.com
ssl_providers: # The {providers}.json to usee
- traefik
urls:
'https:\/\/domain.com\/first-.+': # First regex route configuration
ttl: 1000 # Override default TTL
ttl: 1000s # Override default TTL
'https:\/\/domain.com\/second-route': # Second regex route configuration
ttl: 10 # Override default TTL
ttl: 10s # Override default TTL
headers: # Override default headers
- Authorization
'https?:\/\/mysubdomain\.domain\.com': # Third regex route configuration
ttl: 50
ttl: 50s
headers: # Override default headers
- Authorization
- 'Content-Type'
6 changes: 3 additions & 3 deletions configuration/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ default_cache:
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 10
ttl: 10s
log_level: debug
reverse_proxy_url: 'http://traefik'
ssl_providers:
- traefik
urls:
'domain.com/testing':
ttl: 100
ttl: 100s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50
ttl: 50s
headers:
- Authorization
- 'Content-Type'

0 comments on commit 3da8a5a

Please sign in to comment.