@@ -61,6 +61,22 @@ func givingCacheOfHttpServer(timeout time.Duration, runFor RunFor, onHit ...func
61
61
},
62
62
))
63
63
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
+
64
80
r .POST ("/ping" , cache .Handler (
65
81
Caching {
66
82
Evict : []CacheEvict {
@@ -77,6 +93,48 @@ func givingCacheOfHttpServer(timeout time.Duration, runFor RunFor, onHit ...func
77
93
return r , cache
78
94
}
79
95
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
+
80
138
func Test_Cache_CanStore (t * testing.T ) {
81
139
82
140
for _ , runFor := range []RunFor {MemoryCache , RedisCache } {
0 commit comments