Skip to content

Commit

Permalink
feat(dependencies): bump to Souin v1.7.0 (#99)
Browse files Browse the repository at this point in the history
* feat(dependencies): bump to Souin v1.7.0

* fix(app): remove useless provision
  • Loading branch information
darkweak authored Sep 11, 2024
1 parent 9b5553f commit 283ea9b
Show file tree
Hide file tree
Showing 11 changed files with 2,397 additions and 1,155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Run tests
run: |
go test -v -coverprofile="cover-profile.out" -short -race ./...
go test -v -coverprofile="cover-profile.out" -race ./...
golangci:
name: Lint
Expand Down
21 changes: 5 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Caddy Module: http.handlers.cache

This is a distributed HTTP cache module for Caddy based on [Souin](https://github.com/darkweak/souin) cache.

> [!WARNING]
> Since `v1.7.0` Souin (the development repository that cache-handler is based on) implements only one storage. If you need a specific storage you have to take it from [the storages repository](https://github.com/darkweak/storages) and add it either in your code, during the build otherwise.
(e.g. with otter using caddy) You have to build your caddy module with the desired storage `xcaddy build --with github.com/caddyserver/cache-handler --with github.com/darkweak/storages/otter/caddy` and configure otter in your Caddyfile/JSON configuration file.
See the [documentation about the storages](https://docs.souin.io/docs/storages).

## Features

* [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP Cache.
Expand Down Expand Up @@ -318,22 +323,6 @@ redis-url.com {

You can also use the configuration. Refer to the [Souin docs](https://docs.souin.io/docs/storages/redis/)
or [rueidis client options](https://github.com/redis/rueidis/blob/main/rueidis.go#L56) to define your config as key value.
```
redis-configuration.com {
cache {
redis {
configuration {
InitAddress 127.0.0.1:6379
Username user
Password password
SelectDB 1
ConnWriteTimeout 5s
BlockingPoolSize 99999
}
}
}
}
```

What does these directives mean?
| Key | Description | Value example |
Expand Down
2 changes: 2 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/souin/pkg/surrogate/providers"
"github.com/darkweak/storages/core"
)

// SouinApp contains the whole Souin necessary items
Expand All @@ -30,6 +31,7 @@ func init() {

// Start will start the App
func (s SouinApp) Start() error {
core.ResetRegisteredStorages()
_, _ = up.Delete(stored_providers_key)
_, _ = up.LoadOrStore(stored_providers_key, newStorageProvider())
if s.DefaultCache.GetTTL() == 0 {
Expand Down
4 changes: 2 additions & 2 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *storage_providers) Add(key interface{}) {
}

func (s *SouinCaddyMiddleware) Cleanup() error {
s.logger.Sugar().Debug("Cleanup...")
s.logger.Debug("Cleanup...")
td := []interface{}{}
sp, _ := up.LoadOrStore(stored_providers_key, newStorageProvider())
stored_providers := sp.(*storage_providers)
Expand All @@ -43,7 +43,7 @@ func (s *SouinCaddyMiddleware) Cleanup() error {
})

for _, v := range td {
s.logger.Sugar().Debugf("Cleaning %v\n", v)
s.logger.Debugf("Cleaning %v\n", v)
_, _ = up.Delete(v)
}

Expand Down
67 changes: 52 additions & 15 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/darkweak/souin/configurationtypes"
"go.uber.org/zap"
"github.com/darkweak/storages/core"
)

// DefaultCache the struct
Expand Down Expand Up @@ -38,6 +38,8 @@ type DefaultCache struct {
Redis configurationtypes.CacheProvider `json:"redis"`
// Etcd provider configuration.
Etcd configurationtypes.CacheProvider `json:"etcd"`
// Nats provider configuration.
Nats configurationtypes.CacheProvider `json:"nats"`
// NutsDB provider configuration.
Nuts configurationtypes.CacheProvider `json:"nuts"`
// Otter provider configuration.
Expand All @@ -52,6 +54,8 @@ type DefaultCache struct {
TTL configurationtypes.Duration `json:"ttl"`
// Stale time to live.
Stale configurationtypes.Duration `json:"stale"`
// Disable the coalescing system.
DisableCoalescing bool `json:"disable_coalescing"`
}

// GetAllowedHTTPVerbs returns the allowed verbs to cache
Expand Down Expand Up @@ -99,6 +103,11 @@ func (d *DefaultCache) GetMode() string {
return d.Mode
}

// GetNats returns nuts configuration
func (d *DefaultCache) GetNats() configurationtypes.CacheProvider {
return d.Nats
}

// GetNuts returns nuts configuration
func (d *DefaultCache) GetNuts() configurationtypes.CacheProvider {
return d.Nuts
Expand Down Expand Up @@ -154,6 +163,11 @@ func (d *DefaultCache) GetMaxBodyBytes() uint64 {
return d.MaxBodyBytes
}

// IsCoalescingDisable returns if the coalescing is disabled
func (d *DefaultCache) IsCoalescingDisable() bool {
return d.DisableCoalescing
}

// Configuration holder
type Configuration struct {
// Default cache to fallback on when none are redefined.
Expand All @@ -168,7 +182,7 @@ type Configuration struct {
LogLevel string
// SurrogateKeys contains the surrogate keys to use with a predefined mapping
SurrogateKeys map[string]configurationtypes.SurrogateKeys
logger *zap.Logger
logger core.Logger
}

// GetUrls get the urls list in the configuration
Expand Down Expand Up @@ -197,12 +211,12 @@ func (c *Configuration) GetLogLevel() string {
}

// GetLogger get the logger
func (c *Configuration) GetLogger() *zap.Logger {
func (c *Configuration) GetLogger() core.Logger {
return c.logger
}

// SetLogger set the logger
func (c *Configuration) SetLogger(l *zap.Logger) {
func (c *Configuration) SetLogger(l core.Logger) {
c.logger = l
}

Expand Down Expand Up @@ -269,19 +283,25 @@ func parseBadgerConfiguration(c map[string]interface{}) map[string]interface{} {
func parseRedisConfiguration(c map[string]interface{}) map[string]interface{} {
for k, v := range c {
switch k {
case "InitAddress":
case "Addrs", "InitAddress":
if s, ok := v.(string); ok {
c[k] = []string{s}
} else {
c[k] = v
}
case "Username", "Password", "ClientName", "ClientSetInfo", "ClientTrackingOptions":
case "Username", "Password", "ClientName", "ClientSetInfo", "ClientTrackingOptions", "SentinelUsername", "SentinelPassword", "MasterName", "IdentitySuffix":
c[k] = v
case "SendToReplicas", "ShuffleInit", "ClientNoTouch", "DisableRetry", "DisableCache", "AlwaysPipelining", "AlwaysRESP2", "ForceSingleClient", "ReplicaOnly", "ClientNoEvict":
c[k] = true
case "SelectDB", "CacheSizeEachConn", "RingScaleEachConn", "ReadBufferEachConn", "WriteBufferEachConn", "BlockingPoolSize", "PipelineMultiplex":
c[k], _ = strconv.Atoi(v.(string))
case "ConnWriteTimeout", "MaxFlushDelay":
case "SelectDB", "CacheSizeEachConn", "RingScaleEachConn", "ReadBufferEachConn", "WriteBufferEachConn", "BlockingPoolSize", "PipelineMultiplex", "DB", "Protocol", "MaxRetries", "PoolSize", "MinIdleConns", "MaxIdleConns", "MaxActiveConns", "MaxRedirects":
if v == false {
c[k] = 0
} else if v == true {
c[k] = 1
} else {
c[k], _ = strconv.Atoi(v.(string))
}
case "ConnWriteTimeout", "MaxFlushDelay", "MinRetryBackoff", "MaxRetryBackoff", "DialTimeout", "ReadTimeout", "WriteTimeout", "PoolTimeout", "ConnMaxIdleTime", "ConnMaxLifetime":
c[k], _ = time.ParseDuration(v.(string))
}
}
Expand Down Expand Up @@ -350,7 +370,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
}
cfg.API = apiConfiguration
case "badger":
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand Down Expand Up @@ -447,7 +467,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
}
case "etcd":
cfg.DefaultCache.Distributed = true
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand Down Expand Up @@ -497,8 +517,23 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
return h.Errf("mode must contains only one arg: %s given", args)
}
cfg.DefaultCache.Mode = args[0]
case "nats":
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
case "url":
urlArgs := h.RemainingArgs()
provider.URL = urlArgs[0]
case "configuration":
provider.Configuration = parseCaddyfileRecursively(h)
default:
return h.Errf("unsupported nats directive: %s", directive)
}
}
cfg.DefaultCache.Nuts = provider
case "nuts":
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand All @@ -516,7 +551,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
}
cfg.DefaultCache.Nuts = provider
case "otter":
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand All @@ -529,7 +564,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
cfg.DefaultCache.Otter = provider
case "olric":
cfg.DefaultCache.Distributed = true
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand All @@ -548,7 +583,7 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
cfg.DefaultCache.Olric = provider
case "redis":
cfg.DefaultCache.Distributed = true
provider := configurationtypes.CacheProvider{}
provider := configurationtypes.CacheProvider{Found: true}
for nesting := h.Nesting(); h.NextBlock(nesting); {
directive := h.Val()
switch directive {
Expand Down Expand Up @@ -615,6 +650,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
if err == nil {
cfg.DefaultCache.TTL.Duration = ttl
}
case "disable_coalescing":
cfg.DefaultCache.DisableCoalescing = true
default:
return h.Errf("unsupported root directive: %s", rootOption)
}
Expand Down
Loading

0 comments on commit 283ea9b

Please sign in to comment.