contrib icon indicating copy to clipboard operation
contrib copied to clipboard

bug fix in cache middleware

Open jiangjin opened this issue 9 years ago • 1 comments

fix several problems during cache when I use cache middleware:

  • fix bug in cachedWriter.Write(data []byte); current version just assigns the slice of data []byte into responseCache.data, slicing doesn't copy the slice data []byte data, it creates a new slice value that points to the original array. That means the points/references stored in the cache. That's dangerous, the original array is modified, the value in cache also being changed. This will not bring troubles for c.String(), but it brings bad effect for c.JSON, which use encoder in encoder pool.
  • in CachePage(), when the cache is not hit, http responseWriter in the original context would be replaced by cachedWriter, it's better to use cachedWriter in the copied context for temp usage. so the original context would not be affected.
  • in CachePage(), when the cache is hit, the http header would be fetched from the cache. Use set not add method to avoid the duplicated keys in the http header.

jiangjin avatar Mar 12 '15 16:03 jiangjin

Nice

se77en avatar Aug 17 '15 08:08 se77en