Skip to content

Commit 91dbc5a

Browse files
committed
support path variable
1 parent 4e5aba8 commit 91dbc5a

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

cache_core.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ func (cache *Cache) getCacheKey(cacheable Cacheable, c *gin.Context) string {
127127
s := item[1]
128128
if s != "" {
129129
if c.Request.Method == http.MethodGet {
130-
if query, b := c.GetQuery(s); b {
130+
if query, ok := c.GetQuery(s); ok {
131131
result[i] = query
132+
} else if param := c.Param(s); param != "" {
133+
result[i] = param
132134
}
133135
}
134136
if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut {

cache_integration_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ func givingCacheOfHttpServer(timeout time.Duration, runFor RunFor, onHit ...func
6161
},
6262
))
6363

64+
r.GET("/ping/:id/:hash", cache.Handler(
65+
Caching{
66+
Cacheable: []Cacheable{
67+
{CacheName: "anson", Key: `userId:#id# hash:#hash#`},
68+
},
69+
},
70+
func(c *gin.Context) {
71+
id := c.Param("id")
72+
hash := c.Param("hash")
73+
c.JSON(200, gin.H{
74+
"id": id,
75+
"hash": hash,
76+
})
77+
},
78+
))
79+
6480
r.POST("/ping", cache.Handler(
6581
Caching{
6682
Evict: []CacheEvict{
@@ -77,6 +93,48 @@ func givingCacheOfHttpServer(timeout time.Duration, runFor RunFor, onHit ...func
7793
return r, cache
7894
}
7995

96+
func Test_Path_Variable_Cache_CanStore(t *testing.T) {
97+
98+
for _, runFor := range []RunFor{MemoryCache, RedisCache} {
99+
100+
for _, item := range map[string]struct {
101+
Id string
102+
Hash string
103+
}{
104+
"key1": {
105+
Id: "1", Hash: "anson",
106+
},
107+
"key2": {
108+
Id: "2", Hash: "anson",
109+
},
110+
} {
111+
t.Run(fmt.Sprintf(`key: %s %s`, item.Id, item.Hash), func(t *testing.T) {
112+
r, cache := givingCacheOfHttpServer(time.Hour, runFor, func(c *gin.Context, cacheValue string) {
113+
assert.True(t, len(cacheValue) > 0)
114+
})
115+
116+
w := httptest.NewRecorder()
117+
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/ping/%s/%s", item.Id, item.Hash), nil)
118+
r.ServeHTTP(w, req)
119+
120+
cacheKey := fmt.Sprintf("anson:userid:%s hash:%s", item.Id, item.Hash)
121+
loadCache := cache.loadCache(context.Background(), cacheKey)
122+
assert.Equal(t, 200, w.Code)
123+
124+
sprintf := fmt.Sprintf(`{"id": "%s", "hash": "%s"}`, item.Id, item.Hash)
125+
equalJSON, err := AreEqualJSON(sprintf, loadCache)
126+
assert.Equal(t, equalJSON && err == nil, true)
127+
128+
//test for cache hit hook
129+
w = httptest.NewRecorder()
130+
req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/ping/%s/%s", item.Id, item.Hash), nil)
131+
r.ServeHTTP(w, req)
132+
})
133+
}
134+
}
135+
136+
}
137+
80138
func Test_Cache_CanStore(t *testing.T) {
81139

82140
for _, runFor := range []RunFor{MemoryCache, RedisCache} {

go.mod

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/pygzfei/gin-cache
22

3-
go 1.17
3+
go 1.13
44

55
require (
66
github.com/gin-gonic/gin v1.7.7
@@ -9,26 +9,13 @@ require (
99
)
1010

1111
require (
12-
github.com/cespare/xxhash/v2 v2.1.2 // indirect
13-
github.com/davecgh/go-spew v1.1.1 // indirect
14-
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
15-
github.com/gin-contrib/sse v0.1.0 // indirect
16-
github.com/go-playground/locales v0.14.0 // indirect
17-
github.com/go-playground/universal-translator v0.18.0 // indirect
1812
github.com/go-playground/validator/v10 v10.10.0 // indirect
19-
github.com/golang/protobuf v1.5.2 // indirect
2013
github.com/json-iterator/go v1.1.12 // indirect
21-
github.com/leodido/go-urn v1.2.1 // indirect
2214
github.com/mattn/go-isatty v0.0.14 // indirect
2315
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
24-
github.com/modern-go/reflect2 v1.0.2 // indirect
25-
github.com/pmezard/go-difflib v1.0.0 // indirect
26-
github.com/stretchr/objx v0.3.0 // indirect
27-
github.com/ugorji/go/codec v1.2.6 // indirect
16+
github.com/ugorji/go v1.2.6 // indirect
2817
golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 // indirect
2918
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
3019
golang.org/x/text v0.3.7 // indirect
3120
google.golang.org/protobuf v1.27.1 // indirect
32-
gopkg.in/yaml.v2 v2.4.0 // indirect
33-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
3421
)

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
8787
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
8888
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
8989
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
90-
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
91-
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
9290
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
9391
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
9492
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=

0 commit comments

Comments
 (0)