Skip to content

Commit

Permalink
feat(rfc-9211): Implement {name}-Cache-Control (#259)
Browse files Browse the repository at this point in the history
* feat(rfc-9211): Implement {name}-Cache-Control

* fix bad headervalue usage for cache-control checks

* update tests, fix roadrunner and træfik

* bump to v1.6.22

* bump echo

* fix caddy tests

* Add redis example

* downgrade target mod, bump caddy dep

* throw error without panic on storage access fail

* Try to patch the headers already sent

* update caddy documentation

* fix UpdateCacheEventually to handle properly the canceled request

* Prepare v1.6.22
  • Loading branch information
darkweak authored Oct 14, 2022
1 parent 1dc1ae0 commit 87ac241
Show file tree
Hide file tree
Showing 47 changed files with 495 additions and 232 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ experimental:
plugins:
souin:
moduleName: github.com/darkweak/souin
version: v1.6.21
version: v1.6.22
```
After that you can declare either the whole configuration at once in the middleware block or by service. See the examples below.
```yaml
Expand Down
5 changes: 2 additions & 3 deletions cache/providers/badgerProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package providers

import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"time"
Expand Down Expand Up @@ -150,15 +149,15 @@ func (provider *Badger) Set(key string, value []byte, url t.URL, duration time.D
})

if err != nil {
panic(fmt.Sprintf("Impossible to set value into Badger, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Badger, %v", err)
}

err = provider.DB.Update(func(txn *badger.Txn) error {
return txn.SetEntry(badger.NewEntry([]byte(stalePrefix+key), value).WithTTL(provider.stale + duration))
})

if err != nil {
panic(fmt.Sprintf("Impossible to set value into Badger, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Badger, %v", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions cache/providers/embeddedOlricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ func (provider *EmbeddedOlric) Set(key string, value []byte, url t.URL, duration
}

if err := provider.dm.PutEx(key, value, duration); err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to set value into EmbeddedOlric, %v", err)
}

if err := provider.dm.PutEx(stalePrefix+key, value, provider.stale+duration); err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to set value into EmbeddedOlric, %v", err)
}
}

Expand All @@ -194,7 +194,7 @@ func (provider *EmbeddedOlric) Delete(key string) {
go func() {
err := provider.dm.Delete(key)
if err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to delete a value into EmbeddedOlric, %v", err)
}
}()
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func (provider *EmbeddedOlric) DeleteMany(key string) {
})

if err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to delete values into EmbeddedOlric, %v", err)
}
}()
}
Expand Down
5 changes: 2 additions & 3 deletions cache/providers/etcdProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package providers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"time"
Expand Down Expand Up @@ -136,7 +135,7 @@ func (provider *Etcd) Set(key string, value []byte, url t.URL, duration time.Dur
go provider.Reconnect()
return
}
panic(fmt.Sprintf("Impossible to set value into Etcd, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Etcd, %v", err)
}

_, err = provider.Client.Put(provider.ctx, stalePrefix+key, string(value), clientv3.WithLease(rs.ID))
Expand All @@ -146,7 +145,7 @@ func (provider *Etcd) Set(key string, value []byte, url t.URL, duration time.Dur
go provider.Reconnect()
return
}
panic(fmt.Sprintf("Impossible to set value into Etcd, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Etcd, %v", err)
}
}

Expand Down
15 changes: 10 additions & 5 deletions cache/providers/nutsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package providers

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
Expand All @@ -11,12 +10,14 @@ import (
t "github.com/darkweak/souin/configurationtypes"
"github.com/imdario/mergo"
"github.com/xujiajun/nutsdb"
"go.uber.org/zap"
)

// Nuts provider type
type Nuts struct {
*nutsdb.DB
stale time.Duration
stale time.Duration
logger *zap.Logger
}

const (
Expand Down Expand Up @@ -96,7 +97,11 @@ func NutsConnectionFactory(c t.AbstractConfigurationInterface) (types.AbstractPr
c.GetLogger().Sugar().Error("Impossible to open the Nuts DB.", e)
}

return &Nuts{DB: db, stale: dc.GetStale()}, nil
return &Nuts{
DB: db,
stale: dc.GetStale(),
logger: c.GetLogger(),
}, nil
}

// ListKeys method returns the list of existing keys
Expand Down Expand Up @@ -165,15 +170,15 @@ func (provider *Nuts) Set(key string, value []byte, url t.URL, duration time.Dur
})

if err != nil {
panic(fmt.Sprintf("Impossible to set value into Nuts, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Nuts, %v", err)
}

err = provider.DB.Update(func(tx *nutsdb.Tx) error {
return tx.Put(bucket, []byte(stalePrefix+key), value, uint32((provider.stale + duration).Seconds()))
})

if err != nil {
panic(fmt.Sprintf("Impossible to set value into Nuts, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Nuts, %v", err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions cache/providers/olricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func OlricConnectionFactory(configuration t.AbstractConfigurationInterface) (typ
}
c, err := client.New(&config)
if err != nil {
panic(err)
configuration.GetLogger().Sugar().Errorf("Impossible to connect to Olric, %v", err)
}

return &Olric{
Expand Down Expand Up @@ -152,15 +152,15 @@ func (provider *Olric) Set(key string, value []byte, url t.URL, duration time.Du
go provider.Reconnect()
return
}
panic(err)
provider.logger.Sugar().Errorf("Impossible to set value into Olric, %v", err)
}

if err := provider.dm.PutEx(stalePrefix+key, value, provider.stale+duration); err != nil {
if !provider.reconnecting {
go provider.Reconnect()
return
}
panic(err)
provider.logger.Sugar().Errorf("Impossible to set value into Olric, %v", err)
}
}

Expand All @@ -173,7 +173,7 @@ func (provider *Olric) Delete(key string) {
go func() {
err := provider.dm.Delete(key)
if err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to delete value into Olric, %v", err)
}
}()
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (provider *Olric) DeleteMany(key string) {
})

if err != nil {
panic(err)
provider.logger.Sugar().Errorf("Impossible to delete values into Olric, %v", err)
}
}()
}
Expand Down
5 changes: 2 additions & 3 deletions cache/providers/redisProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package providers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"time"
Expand Down Expand Up @@ -149,15 +148,15 @@ func (provider *Redis) Set(key string, value []byte, url t.URL, duration time.Du
go provider.Reconnect()
return
}
panic(fmt.Sprintf("Impossible to set value into Redis, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Redis, %v", err)
}

if err := provider.Client.Set(provider.ctx, stalePrefix+key, value, duration+provider.stale).Err(); err != nil {
if !provider.reconnecting {
go provider.Reconnect()
return
}
panic(fmt.Sprintf("Impossible to set value into Redis, %s", err))
provider.logger.Sugar().Errorf("Impossible to set value into Redis, %v", err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type (
}
)

const CacheControlCtx ctxKey = "CACHE-CONTROL-CTX"

func GetContext() *Context {
return &Context{
CacheName: &cacheContext{},
Expand Down
15 changes: 10 additions & 5 deletions plugins/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ type souinWriterInterface interface {

// CustomWriter handles the response and provide the way to cache the value
type CustomWriter struct {
Response *http.Response
Buf *bytes.Buffer
Rw http.ResponseWriter
Req *http.Request
size int
Response *http.Response
Buf *bytes.Buffer
Rw http.ResponseWriter
Req *http.Request
size int
headersSent bool
}

// Header will write the response headers
func (r *CustomWriter) Header() http.Header {
if r.headersSent {
return http.Header{}
}
return r.Rw.Header()
}

Expand All @@ -59,6 +63,7 @@ func (r *CustomWriter) WriteHeader(code int) {
if code != 0 {
r.Response.StatusCode = code
}
r.headersSent = true
}

// Write will write the response body
Expand Down
4 changes: 2 additions & 2 deletions plugins/beego/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/beego/beego/v2 v2.0.4
github.com/darkweak/souin v1.6.21
github.com/darkweak/souin v1.6.22
)

require (
Expand Down Expand Up @@ -79,4 +79,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/darkweak/souin v1.6.21 => ../..
replace github.com/darkweak/souin v1.6.22 => ../..
39 changes: 39 additions & 0 deletions plugins/caddy/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,45 @@ route /nuts-configuration {
respond "Hello nuts"
}

route /redis-configuration {
cache {
ttl 15s
redis {
configuration {
Network my-network
Addr 127.0.0.1:6789
Username user
Password password
DB 1
MaxRetries 1
MinRetryBackoff 5s
MaxRetryBackoff 5s
DialTimeout 5s
ReadTimeout 5s
WriteTimeout 5s
PoolFIFO true
PoolSize 99999
PoolTimeout 10s
MinIdleConns 100
MaxIdleConns 100
ConnMaxIdleTime 5s
ConnMaxLifetime 5s
}
}
}
respond "Hello redis"
}

route /redis-url {
cache {
ttl 15s
redis {
url 127.0.0.1:6789
}
}
respond "Hello redis url"
}

route /vary {
cache {
ttl 15s
Expand Down
Loading

0 comments on commit 87ac241

Please sign in to comment.