0

gin-cache中的CachePage方法:

if err := store.Get(key, &cache); err != nil {
// replace writer
    writer := newCachedWriter(store, expire, c.Writer, key)
    c.Writer = writer
    handle(c)
} else ...

这里把context的ResponseWriter给替换成了cachedWriter,那么在handle(c)这里调用的应该是cachedWriter重写过的Write(data []byte) (int, error)吧?

但实际上被调用的仍然是gin.Context.ResponseWriter.Write

go version go1.7 windows/amd64

希望有大虾能指点一二。谢谢。

1 个回答

0
type cachedWriter struct {
    gin.ResponseWriter
    status  int
    written bool
    store   persistence.CacheStore
    expire  time.Duration
    key     string
}

如上,gin.ResponseWritercachedWriter中的一个匿名字段,如果cachedWriter没有定义与 gin.ResponseWriter所拥有函数名相同的函数的话,那么cachedWriter调用这些函数时就默认调用的是gin.ResponseWriter的函数。