From 5dc893e0beb9a390b9c14316e19ecaa0dad36725 Mon Sep 17 00:00:00 2001 From: kenshin579 Date: Sat, 8 Jul 2023 22:23:37 +0900 Subject: [PATCH] [#8] export config field --- cache.go | 24 ++++++++++++------------ cache_test.go | 18 +++++++++--------- redis_test.go | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cache.go b/cache.go index 1a49a3d..d980e69 100644 --- a/cache.go +++ b/cache.go @@ -54,7 +54,7 @@ type ( // returns true or false, whether it exists or not. Get(key uint64) ([]byte, bool) - // Set caches a response for a given key until an expiration date. + // Set caches a response for a given key until an Expiration date. Set(key uint64, response []byte, expiration time.Time) // Release frees cache for a given key. @@ -70,9 +70,9 @@ type ( Store CacheStore - expiration time.Duration - includePaths []string - excludePaths []string + Expiration time.Duration + IncludePaths []string + ExcludePaths []string } // CacheResponse is the cached response data structure. @@ -83,8 +83,8 @@ type ( // Header is the cached response header. Header http.Header `json:"header"` - // Expiration is the cached response expiration date. - Expiration time.Time `json:"expiration"` + // Expiration is the cached response Expiration date. + Expiration time.Time `json:"Expiration"` // LastAccess is the last date a cached response was accessed. // Used by LRU and MRU algorithms. @@ -100,7 +100,7 @@ var ( // DefaultCacheConfig defines default values for CacheConfig DefaultCacheConfig = CacheConfig{ Skipper: middleware.DefaultSkipper, - expiration: 3 * time.Minute, + Expiration: 3 * time.Minute, } ) @@ -128,8 +128,8 @@ func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc { if config.Store == nil { panic("Store configuration must be provided") } - if config.expiration < 1 { - panic("Cache expiration must be provided") + if config.Expiration < 1 { + panic("Cache Expiration must be provided") } return func(next echo.HandlerFunc) echo.HandlerFunc { @@ -194,7 +194,7 @@ func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc { response := CacheResponse{ Value: value, Header: writer.Header(), - Expiration: now.Add(config.expiration), + Expiration: now.Add(config.Expiration), LastAccess: now, Frequency: 1, } @@ -211,7 +211,7 @@ func CacheWithConfig(config CacheConfig) echo.MiddlewareFunc { } func (c *CacheConfig) isIncludePaths(URL string) bool { - for _, p := range c.includePaths { + for _, p := range c.IncludePaths { if strings.Contains(URL, p) { return true } @@ -220,7 +220,7 @@ func (c *CacheConfig) isIncludePaths(URL string) bool { } func (c *CacheConfig) isExcludePaths(URL string) bool { - for _, p := range c.excludePaths { + for _, p := range c.ExcludePaths { if strings.Contains(URL, p) { return true } diff --git a/cache_test.go b/cache_test.go index 7d82f5a..04c1ac4 100644 --- a/cache_test.go +++ b/cache_test.go @@ -57,7 +57,7 @@ func Test_CacheWithConfig(t *testing.T) { wants wants }{ { - name: "test includePaths", + name: "test IncludePaths", args: args{ method: http.MethodGet, url: "http://foo.bar/test-1", @@ -66,8 +66,8 @@ func Test_CacheWithConfig(t *testing.T) { Capacity: 5, Algorithm: LFU, }), - expiration: 5 * time.Second, - includePaths: []string{"foo.bar"}, + Expiration: 5 * time.Second, + IncludePaths: []string{"foo.bar"}, }, }, wants: wants{ @@ -76,7 +76,7 @@ func Test_CacheWithConfig(t *testing.T) { }, }, { - name: "test excludePaths", + name: "test ExcludePaths", args: args{ method: http.MethodGet, url: "http://foo.bar/test-1", @@ -85,8 +85,8 @@ func Test_CacheWithConfig(t *testing.T) { Capacity: 5, Algorithm: LFU, }), - expiration: 5 * time.Second, - excludePaths: []string{"foo.bar"}, + Expiration: 5 * time.Second, + ExcludePaths: []string{"foo.bar"}, }, }, wants: wants{ @@ -104,8 +104,8 @@ func Test_CacheWithConfig(t *testing.T) { Capacity: 5, Algorithm: LFU, }), - expiration: 5 * time.Second, - includePaths: []string{"foo.bar"}, + Expiration: 5 * time.Second, + IncludePaths: []string{"foo.bar"}, }, }, wants: wants{ @@ -192,7 +192,7 @@ func Test_nytes(t *testing.T) { } bytes := r.bytes() - assert.Equal(t, `{"value":"dGVzdA==","header":null,"expiration":"0001-01-01T00:00:00Z","lastAccess":"0001-01-01T00:00:00Z","frequency":1}`, string(bytes)) + assert.Equal(t, `{"value":"dGVzdA==","header":null,"Expiration":"0001-01-01T00:00:00Z","lastAccess":"0001-01-01T00:00:00Z","frequency":1}`, string(bytes)) } func Test_keyAsString(t *testing.T) { diff --git a/redis_test.go b/redis_test.go index ec101f6..da23529 100644 --- a/redis_test.go +++ b/redis_test.go @@ -40,8 +40,8 @@ func (suite *cacheRedisStoreTestSuite) SetupSuite() { suite.echo = echo.New() suite.echo.Use(CacheWithConfig(CacheConfig{ Store: store, - expiration: 5 * time.Second, - includePaths: []string{"/test"}, + Expiration: 5 * time.Second, + IncludePaths: []string{"/test"}, })) }