Skip to content

Commit

Permalink
feat(dependencies): bump souin v1.7.5 (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak authored Nov 6, 2024
1 parent 283ea9b commit 507cbf6
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 13 deletions.
74 changes: 71 additions & 3 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type DefaultCache struct {
Timeout configurationtypes.Timeout `json:"timeout"`
// Time to live.
TTL configurationtypes.Duration `json:"ttl"`
// SimpleFS provider configuration.
SimpleFS configurationtypes.CacheProvider `json:"simplefs"`
// Stale time to live.
Stale configurationtypes.Duration `json:"stale"`
// Disable the coalescing system.
Expand Down Expand Up @@ -103,7 +105,7 @@ func (d *DefaultCache) GetMode() string {
return d.Mode
}

// GetNats returns nuts configuration
// GetNats returns nats configuration
func (d *DefaultCache) GetNats() configurationtypes.CacheProvider {
return d.Nats
}
Expand Down Expand Up @@ -133,6 +135,11 @@ func (d *DefaultCache) GetRegex() configurationtypes.Regex {
return d.Regex
}

// GetSimpleFS returns simplefs configuration
func (d *DefaultCache) GetSimpleFS() configurationtypes.CacheProvider {
return d.SimpleFS
}

// GetStorers returns the chianed storers
func (d *DefaultCache) GetStorers() []string {
return d.Storers
Expand Down Expand Up @@ -291,7 +298,7 @@ func parseRedisConfiguration(c map[string]interface{}) map[string]interface{} {
}
case "Username", "Password", "ClientName", "ClientSetInfo", "ClientTrackingOptions", "SentinelUsername", "SentinelPassword", "MasterName", "IdentitySuffix":
c[k] = v
case "SendToReplicas", "ShuffleInit", "ClientNoTouch", "DisableRetry", "DisableCache", "AlwaysPipelining", "AlwaysRESP2", "ForceSingleClient", "ReplicaOnly", "ClientNoEvict":
case "SendToReplicas", "ShuffleInit", "ClientNoTouch", "DisableRetry", "DisableCache", "AlwaysPipelining", "AlwaysRESP2", "ForceSingleClient", "ReplicaOnly", "ClientNoEvict", "ContextTimeoutEnabled", "PoolFIFO", "ReadOnly", "RouteByLatency", "RouteRandomly", "DisableIndentity":
c[k] = true
case "SelectDB", "CacheSizeEachConn", "RingScaleEachConn", "ReadBufferEachConn", "WriteBufferEachConn", "BlockingPoolSize", "PipelineMultiplex", "DB", "Protocol", "MaxRetries", "PoolSize", "MinIdleConns", "MaxIdleConns", "MaxActiveConns", "MaxRedirects":
if v == false {
Expand All @@ -303,6 +310,45 @@ func parseRedisConfiguration(c map[string]interface{}) map[string]interface{} {
}
case "ConnWriteTimeout", "MaxFlushDelay", "MinRetryBackoff", "MaxRetryBackoff", "DialTimeout", "ReadTimeout", "WriteTimeout", "PoolTimeout", "ConnMaxIdleTime", "ConnMaxLifetime":
c[k], _ = time.ParseDuration(v.(string))
case "MaxVersion", "MinVersion":
strV, _ := v.(string)
if strings.HasPrefix(strV, "TLS") {
strV = strings.Trim(strings.TrimPrefix(strV, "TLS"), " ")
}

switch strV {
case "0x0300", "SSLv3":
c[k] = 0x0300
case "0x0301", "1.0":
c[k] = 0x0301
case "0x0302", "1.1":
c[k] = 0x0302
case "0x0303", "1.2":
c[k] = 0x0303
case "0x0304", "1.3":
c[k] = 0x0304
}
case "TLSConfig":
c[k] = parseRedisConfiguration(v.(map[string]interface{}))
}
}

return c
}

func parseSimpleFSConfiguration(c map[string]interface{}) map[string]interface{} {
for k, v := range c {
switch k {
case "path":
c[k] = v
case "size":
if v == false {
c[k] = 0
} else if v == true {
c[k] = 1
} else {
c[k], _ = strconv.Atoi(v.(string))
}
}
}

Expand Down Expand Up @@ -441,14 +487,20 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
if len(args) > 0 {
cdn.Dynamic, _ = strconv.ParseBool(args[0])
}
case "email":
cdn.Email = h.RemainingArgs()[0]
case "hostname":
cdn.Hostname = h.RemainingArgs()[0]
case "network":
cdn.Network = h.RemainingArgs()[0]
case "provider":
cdn.Provider = h.RemainingArgs()[0]
case "service_id":
cdn.ServiceID = h.RemainingArgs()[0]
case "strategy":
cdn.Strategy = h.RemainingArgs()[0]
case "zone_id":
cdn.ZoneID = h.RemainingArgs()[0]
default:
return h.Errf("unsupported cdn directive: %s", directive)
}
Expand Down Expand Up @@ -531,7 +583,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
return h.Errf("unsupported nats directive: %s", directive)
}
}
cfg.DefaultCache.Nuts = provider
cfg.DefaultCache.Nats = provider
case "nuts":
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
Expand Down Expand Up @@ -611,6 +663,22 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
return h.Errf("unsupported regex directive: %s", directive)
}
}
case "simplefs":
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
case "path":
urlArgs := h.RemainingArgs()
provider.Path = urlArgs[0]
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
provider.Configuration = parseSimpleFSConfiguration(provider.Configuration.(map[string]interface{}))
default:
return h.Errf("unsupported simplefs directive: %s", directive)
}
}
cfg.DefaultCache.SimpleFS = provider
case "stale":
args := h.RemainingArgs()
stale, err := time.ParseDuration(args[0])
Expand Down
38 changes: 35 additions & 3 deletions dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpcache

import (
"fmt"
"os"
"strings"

"github.com/caddyserver/caddy/v2"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) {
if e != nil {
s.logger.Errorf("Error during Nats init, did you include the Nats storage (--with github.com/darkweak/storages/nats/caddy)? %v", e)
} else {
s.Configuration.DefaultCache.Nuts.Uuid = fmt.Sprintf("NATS-%s-%s", s.Configuration.DefaultCache.Nats.URL, s.Configuration.DefaultCache.GetStale())
s.Configuration.DefaultCache.Nats.Uuid = fmt.Sprintf("NATS-%s-%s", s.Configuration.DefaultCache.Nats.URL, s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Nuts.Found {
Expand Down Expand Up @@ -101,7 +102,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) {
if e != nil {
s.logger.Errorf("Error during Olric init, did you include the Olric storage (--with github.com/darkweak/storages/olric/caddy)? %v", e)
} else {
s.Configuration.DefaultCache.Nuts.Uuid = fmt.Sprintf("OLRIC-%s-%s", s.Configuration.DefaultCache.Olric.URL, s.Configuration.DefaultCache.GetStale())
s.Configuration.DefaultCache.Olric.Uuid = fmt.Sprintf("OLRIC-%s-%s", s.Configuration.DefaultCache.Olric.URL, s.Configuration.DefaultCache.GetStale())
}
}
if s.Configuration.DefaultCache.Otter.Found {
Expand All @@ -121,7 +122,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) {
address := redis.URL
username := ""
dbname := "0"
cname := ""
cname := "souin-redis"
if c := redis.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
Expand Down Expand Up @@ -172,4 +173,35 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) {
)
}
}
if s.Configuration.DefaultCache.SimpleFS.Found {
e := dispatchStorage(ctx, "simplefs", s.Configuration.DefaultCache.SimpleFS, s.Configuration.DefaultCache.GetStale())
if e != nil {
s.logger.Errorf("Error during SimpleFS init, did you include the SimpleFS storage (--with github.com/darkweak/storages/simplefs/caddy)? %v", e)
} else {
simplefs := s.Configuration.DefaultCache.SimpleFS
path := simplefs.Path
size := "0"
if c := simplefs.Configuration; c != nil {
p, ok := c.(map[string]interface{})
if ok {
if d, ok := p["path"]; path == "" && ok {
path = fmt.Sprint(d)
}
if d, ok := p["size"]; ok {
size = fmt.Sprint(d)
}
}
}

if path == "" {
path, _ = os.Getwd()
}

s.Configuration.DefaultCache.SimpleFS.Uuid = fmt.Sprintf(
"SIMPLEFS-%s-%s",
path,
size,
)
}
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.22.1

require (
github.com/caddyserver/caddy/v2 v2.8.4
github.com/darkweak/souin v1.7.0
github.com/darkweak/storages/core v0.0.8
github.com/darkweak/souin v1.7.5
github.com/darkweak/storages/core v0.0.11
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns=
github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654=
github.com/darkweak/souin v1.7.0 h1:QeSxwHECzZPlYHTGYDw4xQ6EBJY94f/nfqW4BLc3YQ0=
github.com/darkweak/souin v1.7.0/go.mod h1:XXmhB+QIiZ/lkESd1izzqCI7QWxmX0of01QA+xxgogc=
github.com/darkweak/storages/core v0.0.8 h1:9e7rOxHiJwnvADDVCZ7LFRnUnOHGT+UMpNOFlR8BOiw=
github.com/darkweak/storages/core v0.0.8/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
github.com/darkweak/souin v1.7.5 h1:drNhZc0GhSbGcugiGfcYdLDTcx3DCZW6o13wwRj5o5Y=
github.com/darkweak/souin v1.7.5/go.mod h1:PcP+hhvYOdqn4OmeScKKvit0TihYVYS1o154mhfWT/s=
github.com/darkweak/storages/core v0.0.11 h1:IwvpAtkhOmxC5pIffJ8opW6erpTnIi5zqPveiAQs8ew=
github.com/darkweak/storages/core v0.0.11/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
14 changes: 13 additions & 1 deletion httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type SouinCaddyMiddleware struct {
Key configurationtypes.Key `json:"key,omitempty"`
// Override the cache key generation matching the pattern.
CacheKeys configurationtypes.CacheKeys `json:"cache_keys,omitempty"`
// Configure the Nats cache storage.
Nats configurationtypes.CacheProvider `json:"nats,omitempty"`
// Configure the Nuts cache storage.
Nuts configurationtypes.CacheProvider `json:"nuts,omitempty"`
// Configure the Otter cache storage.
Expand All @@ -61,6 +63,8 @@ type SouinCaddyMiddleware struct {
Timeout configurationtypes.Timeout `json:"timeout,omitempty"`
// Time to live for a key, using time.duration.
TTL configurationtypes.Duration `json:"ttl,omitempty"`
// Configure the SimpleFS cache storage.
SimpleFS configurationtypes.CacheProvider `json:"simplefs,omitempty"`
// Time to live for a stale key, using time.duration.
Stale configurationtypes.Duration `json:"stale,omitempty"`
// Storage providers chaining and order.
Expand Down Expand Up @@ -90,7 +94,9 @@ func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
if s.Configuration.GetDefaultCache() == nil {
defaultCache := DefaultCache{
Badger: s.Badger,
Nats: s.Nats,
Nuts: s.Nuts,
SimpleFS: s.SimpleFS,
Otter: s.Otter,
Key: s.Key,
DefaultCacheControl: s.DefaultCacheControl,
Expand All @@ -115,6 +121,10 @@ func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
return nil
}

func isProviderEmpty(c configurationtypes.CacheProvider) bool {
return !c.Found
}

// FromApp to initialize configuration from App structure.
func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
if s.Configuration.GetDefaultCache() == nil {
Expand Down Expand Up @@ -198,14 +208,16 @@ func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
if dc.CacheName == "" {
s.Configuration.DefaultCache.CacheName = appDc.CacheName
}
if !s.Configuration.DefaultCache.Distributed && !dc.Olric.Found && !dc.Redis.Found && !dc.Etcd.Found && !dc.Badger.Found && !dc.Nuts.Found && !dc.Otter.Found {
if isProviderEmpty(dc.Badger) && isProviderEmpty(dc.Etcd) && isProviderEmpty(dc.Nats) && isProviderEmpty(dc.Nuts) && isProviderEmpty(dc.Olric) && isProviderEmpty(dc.Otter) && isProviderEmpty(dc.Redis) && isProviderEmpty(dc.SimpleFS) {
s.Configuration.DefaultCache.Distributed = appDc.Distributed
s.Configuration.DefaultCache.Olric = appDc.Olric
s.Configuration.DefaultCache.Redis = appDc.Redis
s.Configuration.DefaultCache.Etcd = appDc.Etcd
s.Configuration.DefaultCache.Badger = appDc.Badger
s.Configuration.DefaultCache.Nats = appDc.Nats
s.Configuration.DefaultCache.Nuts = appDc.Nuts
s.Configuration.DefaultCache.Otter = appDc.Otter
s.Configuration.DefaultCache.SimpleFS = appDc.SimpleFS
}
if dc.Regex.Exclude == "" {
s.Configuration.DefaultCache.Regex.Exclude = appDc.Regex.Exclude
Expand Down
Loading

0 comments on commit 507cbf6

Please sign in to comment.