Skip to content

Commit

Permalink
change type to time for ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
Houziaux mike / Jenaye committed Jul 22, 2021
1 parent ab38615 commit 73fb9bf
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 137 deletions.
13 changes: 6 additions & 7 deletions cache/coalescing/requestCoalescing.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package coalescing

import (
"net/http"
"strings"
"time"

"github.com/darkweak/souin/cache/types"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/rfc"
"golang.org/x/sync/singleflight"
"net/http"
"strings"
"time"
)

// Temporise will run one call to proxy then use the response for other requests that couldn't reach cached response
func (r *RequestCoalescing) Temporise(req *http.Request, rw http.ResponseWriter, nextMiddleware func(http.ResponseWriter, *http.Request) error) {
key := rfc.GetCacheKey(req)
ch := r.requestGroup.DoChan(key, func() (interface{}, error) {
defer r.requestGroup.Forget(key)
ch := r.requestGroup.DoChan(rfc.GetCacheKey(req), func() (interface{}, error) {
e := nextMiddleware(rw, req)

return nil, e
Expand Down Expand Up @@ -61,7 +60,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
37 changes: 5 additions & 32 deletions cache/providers/badgerProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package providers

import (
"fmt"
t "github.com/darkweak/souin/configurationtypes"
badger "github.com/dgraph-io/badger/v3"
"net/http"
"regexp"
"time"

t "github.com/darkweak/souin/configurationtypes"
badger "github.com/dgraph-io/badger/v3"
)

// Badger provider type
Expand All @@ -24,7 +24,7 @@ func BadgerConnectionFactory(_ t.AbstractConfigurationInterface) (*Badger, error
// ListKeys method returns the list of existing keys
func (provider *Badger) ListKeys() []string {
keys := []string{}

e := provider.DB.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
Expand Down Expand Up @@ -66,37 +66,10 @@ func (provider *Badger) Get(key string) []byte {
return result
}

// Prefix method returns the populated response if exists, empty response then
func (provider *Badger) Prefix(key string, req *http.Request) []byte {
var result []byte

_ = provider.DB.View(func(txn *badger.Txn) error {
prefix := []byte(key)
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
if varyVoter(key, req, string(it.Item().Key())) {
_ = it.Item().Value(func(val []byte) error {
result = val
return nil
})
}
}
return nil
})

return result
}

// 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
46 changes: 7 additions & 39 deletions cache/providers/embeddedOlricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package providers
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/buraksezer/olric"
"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/query"
t "github.com/darkweak/souin/configurationtypes"
"github.com/google/uuid"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"io/ioutil"
"net/http"
"os"
"time"
)

// EmbeddedOlric provider type
type EmbeddedOlric struct {
dm *olric.DMap
db *olric.Olric
dm *olric.DMap
db *olric.Olric
}

func tryToLoadConfiguration(olricInstance *config.Config, olricConfiguration t.CacheProvider, logger *zap.Logger) (*config.Config, bool) {
Expand Down Expand Up @@ -124,33 +124,6 @@ func (provider *EmbeddedOlric) ListKeys() []string {
return keys
}

// Prefix method returns the populated response if exists, empty response then
func (provider *EmbeddedOlric) Prefix(key string, req *http.Request) []byte {
c, err := provider.dm.Query(query.M{
"$onKey": query.M{
"$regexMatch": "^" + key,
},
})

if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to retrieve data in Olric: %s", err))
return []byte{}
}
defer c.Close()

res := []byte{}
err = c.Range(func(k string, v interface{}) bool {
if varyVoter(key, req, k) {
res = v.([]byte)
return false
}

return true
})

return res
}

// Get method returns the populated response if exists, empty response then
func (provider *EmbeddedOlric) Get(key string) []byte {
val2, err := provider.dm.Get(key)
Expand All @@ -165,12 +138,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
40 changes: 4 additions & 36 deletions cache/providers/olricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package providers

import (
"fmt"
"time"

"github.com/buraksezer/olric/client"
"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/query"
t "github.com/darkweak/souin/configurationtypes"
"net/http"
"time"
)

// Olric provider type
type Olric struct {
*client.Client
dm *client.DMap
dm *client.DMap
}

// OlricConnectionFactory function create new Olric instance
Expand Down Expand Up @@ -63,33 +63,6 @@ func (provider *Olric) ListKeys() []string {
return keys
}

// Prefix method returns the populated response if exists, empty response then
func (provider *Olric) Prefix(key string, req *http.Request) []byte {
c, err := provider.dm.Query(query.M{
"$onKey": query.M{
"$regexMatch": "^" + key,
},
})

if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to retrieve data in Olric: %s", err))
return []byte{}
}
defer c.Close()

res := []byte{}
err = c.Range(func(k string, v interface{}) bool {
if varyVoter(key, req, k) {
res = v.([]byte)
return false
}

return true
})

return res
}

// Get method returns the populated response if exists, empty response then
func (provider *Olric) Get(key string) []byte {
val2, err := provider.dm.Get(key)
Expand All @@ -104,12 +77,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
16 changes: 10 additions & 6 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package configurationtypes

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

"go.uber.org/zap"
)

// Port config
type Port struct {
Expand All @@ -21,8 +25,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 +43,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 +67,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 +77,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
5 changes: 2 additions & 3 deletions plugins/caddy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/caddyserver/caddy/v2"
)

// SouinApp contains the whole Souin necessary items
type SouinApp struct {
*DefaultCache
LogLevel string `json:"log_level,omitempty"`
Expand All @@ -21,7 +20,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 == "0s" {
return new(defaultCacheError)
}
return nil
Expand All @@ -33,7 +32,7 @@ func (s SouinApp) Stop() error {
}

// CaddyModule implements caddy.ModuleInfo
func (s SouinApp) CaddyModule() caddy.ModuleInfo {
func (a SouinApp) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: moduleName,
New: func() caddy.Module { return new(SouinApp) },
Expand Down
6 changes: 4 additions & 2 deletions plugins/caddy/configuration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package caddy

import (
"time"

"github.com/darkweak/souin/configurationtypes"
"go.uber.org/zap"
)
Expand All @@ -11,7 +13,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 +37,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
Loading

0 comments on commit 73fb9bf

Please sign in to comment.