gin-contrib / gin-contrib/cache
SiteCache cannot work as middleware ?
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 456
- Forks
- 100
- Avg merge
- 2h 16m
- Merged PRs (30d)
- 2
Description
I thought SiteCache was a middleware, and when I used it, it didn't work.
I saw the source code as following, I noted:
func SiteCache(store persistence.CacheStore, expire time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
var cache responseCache
url := c.Request.URL
key := CreateKey(url.RequestURI())
if err := store.Get(key, &cache); err != nil {
c.Next() // just call the next handler, why not saving the response ? like other Decorator
} else {
fmt.Println("cache hited: ", string(cache.Data))
c.Writer.WriteHeader(cache.Status)
for k, vals := range cache.Header {
for _, v := range vals {
c.Writer.Header().Set(k, v)
}
}
c.Writer.Write(cache.Data)
}
}
}
then I changed code as following, and it worked as what i want:
func SiteCache(store persistence.CacheStore, expire time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
var cache responseCache
url := c.Request.URL
key := CreateKey(url.RequestURI())
if err := store.Get(key, &cache); err != nil {
if err != persistence.ErrCacheMiss {
log.Println(err.Error())
}
// replace writer
writer := newCachedWriter(store, expire, c.Writer, key)
c.Writer = writer
c.Next()
// Drop caches of aborted contexts
if c.IsAborted() {
store.Delete(key)
}
} else {
c.Writer.WriteHeader(cache.Status)
for k, vals := range cache.Header {
for _, v := range vals {
c.Writer.Header().Set(k, v)
}
}
c.Writer.Write(cache.Data)
c.Abort()
}
}
}
so, what is the SiteCache designd for ? A middleware or not ? I'm confused ~
waiting for your answer
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at SiteCache and trace both the cache-hit and cache-miss paths, including c.Next, responseCache, newCachedWriter, and persistence.CacheStore. No file or test is named, so first determine the intended middleware contract and whether a cache miss should capture the downstream response. Done means the behavior is clarified and verified for both paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100