Caching assets with HTTP cache headers
You can also manage caching with the Gin framework. To illustrate this, write a simple web application to serve an image. The code is as follows:
func IllustrationHandler(c *gin.Context) { Â Â Â c.File("illustration.png") } func main() { Â Â Â router := gin.Default() Â Â Â router.GET("/illustration", IllustrationHandler) Â Â Â router.Run(":3000") }
The application should serve an image when the user hits the /illustration
resource URL:
Because the same image is always delivered, we need to make sure that we're caching the image. That way, we can avoid having unnecessary traffic and have better web performance. Let's see how that is done.
Setting HTTP caching headers
To cache this HTTP request, you can attach an entity tag (ETag) to the HTTP response header. When a user sends...