Skip to content

Commit

Permalink
Added methods to cache midelware
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jul 6, 2023
1 parent 38865d0 commit 460b93d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions internal/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ func (globs CacheGlobs) Clone() CacheGlobs {
type CacheConfig struct {
ExpirationTime time.Duration `mapstructure:"expiration-time"`
ClearTime time.Duration `mapstructure:"clear-time"`
Methods []string `mapstructure:"methods"`
}

func (config *CacheConfig) Clone() *CacheConfig {
var methods []string = nil
if config.Methods != nil {
methods = append(methods, config.Methods...)
}

return &CacheConfig{
ExpirationTime: config.ExpirationTime,
ClearTime: config.ClearTime,
Methods: methods,
}
}
6 changes: 6 additions & 0 deletions internal/config/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -31,6 +32,7 @@ func TestCacheConfigClone(t *testing.T) {
cacheConfig := &config.CacheConfig{
ExpirationTime: 5 * time.Minute,
ClearTime: 30 * time.Second,
Methods: []string{http.MethodGet, http.MethodPost},
}

clonedCacheConfig := cacheConfig.Clone()
Expand All @@ -42,4 +44,8 @@ func TestCacheConfigClone(t *testing.T) {
t.Run("equals values", func(t *testing.T) {
assert.EqualValues(t, cacheConfig, clonedCacheConfig)
})

t.Run("not same methods", func(t *testing.T) {
assert.NotSame(t, cacheConfig.Methods, clonedCacheConfig.Methods)
})
}
14 changes: 14 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -51,6 +52,9 @@ key-file: /etc/certificates/key-file.key
cache-config:
expiration-time: 1h
clear-time: 30m
methods:
- GET
- POST
`
)

Expand Down Expand Up @@ -96,6 +100,7 @@ func TestLoadConfiguration(t *testing.T) {
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
ClearTime: config.DefaultClearTime,
Methods: []string{http.MethodGet},
},
},
},
Expand All @@ -111,6 +116,7 @@ func TestLoadConfiguration(t *testing.T) {
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
ClearTime: config.DefaultClearTime,
Methods: []string{http.MethodGet},
},
},
},
Expand Down Expand Up @@ -154,6 +160,10 @@ func TestLoadConfiguration(t *testing.T) {
CacheConfig: config.CacheConfig{
ExpirationTime: time.Hour,
ClearTime: 30 * time.Minute,
Methods: []string{
http.MethodGet,
http.MethodPost,
},
},
},
},
Expand Down Expand Up @@ -205,6 +215,10 @@ func TestLoadConfiguration(t *testing.T) {
CacheConfig: config.CacheConfig{
ExpirationTime: time.Hour,
ClearTime: 30 * time.Minute,
Methods: []string{
http.MethodGet,
http.MethodPost,
},
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions internal/config/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"net/http"
"time"

"github.com/spf13/viper"
Expand All @@ -14,4 +15,5 @@ const (
func setDefaultValues(instance *viper.Viper) {
instance.SetDefault("cache-config.expiration-time", DefaultExpirationTime)
instance.SetDefault("cache-config.clear-time", DefaultClearTime)
instance.SetDefault("cache-config.methods", []string{http.MethodGet})
}
1 change: 1 addition & 0 deletions internal/config/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (u *Mapping) Clone() Mapping {
To: u.To,
Statics: u.Statics.Clone(),
Mocks: u.Mocks.Clone(),
Cache: u.Cache.Clone(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/config/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (mappings Mappings) String() string {
for _, static := range mapping.Statics {
builder.WriteString(sfmt.Sprintf(" static: %s => %s\n", static.Path, static.Dir))
}
for _, cacheGlob := range mapping.Cache {
builder.WriteString(sfmt.Sprintf(" cache: %s\n", cacheGlob))
}
}

builder.WriteString("\n")
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/cache/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewMiddleware(t *testing.T) {
middleware := cache.NewMiddleware(
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithMethods(http.MethodGet),
cache.WithMethods([]string{http.MethodGet}),
cache.WithGlobs(config.CacheGlobs{
"/translations",
"/api/**",
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/cache/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func WithPrefix(prefix string) MiddlewareOption {
}
}

func WithMethods(methods ...string) MiddlewareOption {
func WithMethods(methods []string) MiddlewareOption {
return func(m *Middleware) {
m.methods = methods
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func main() {
return cache.NewMiddleware(
cache.WithLogger(ui.CacheLogger),
cache.WithPrefix(key),
cache.WithMethods(cacheConfig.Methods),
cache.WithCacheStorage(cacheStorage),
cache.WithGlobs(globs),
)
Expand Down

0 comments on commit 460b93d

Please sign in to comment.