Skip to content

Commit

Permalink
test for CustomCacheStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui committed Dec 29, 2023
1 parent 83df4de commit e3df7ae
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 44 deletions.
21 changes: 20 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func TestCacheByRequestURIIgnoreOrder(t *testing.T) {
const prefixKey = "#prefix#"

func TestPrefixKey(t *testing.T) {

memoryStore := persist.NewMemoryStore(1 * time.Minute)
cachePathMiddleware := CacheByRequestPath(
memoryStore,
Expand Down Expand Up @@ -278,3 +277,23 @@ func TestWithDiscardHeaders(t *testing.T) {
assert.Equal(t, headers2.Get(headerKey), "")
}
}

func TestCustomCacheStrategy(t *testing.T) {
memoryStore := persist.NewMemoryStore(1 * time.Minute)
cacheMiddleware := Cache(
memoryStore,
24*time.Hour,
WithCacheStrategyByRequest(func(c *gin.Context) (bool, Strategy) {
return true, Strategy{
CacheKey: "custom_cache_key_" + c.Query("uid"),
}
}),
)

_ = mockHttpRequest(cacheMiddleware, "/cache?uid=1", false)

var val interface{}
err := memoryStore.Get("custom_cache_key_1", &val)
assert.Nil(t, err)
return
}
19 changes: 15 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ module github.com/chenyahui/gin-cache
go 1.13

require (
github.com/gin-gonic/gin v1.7.7
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-redis/redis/v8 v8.11.5
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/stretchr/testify v1.7.1
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/stretchr/testify v1.8.4
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0
google.golang.org/protobuf v1.32.0 // indirect
)
Loading

0 comments on commit e3df7ae

Please sign in to comment.