GolangNote

Golang笔记

go 实现图片服务器缓存

Permalink

go 实现图片服务器缓存

Go: 图片缓存 handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func blackHandler(w http.ResponseWriter, r *http.Request) {

    key := "black"
    e := `"` + key + `"`
    w.Header().Set("Etag", e)
    w.Header().Set("Cache-Control", "max-age=2592000") // 30 days

    if match := r.Header.Get("If-None-Match"); match != "" {
        if strings.Contains(match, e) {
            w.WriteHeader(http.StatusNotModified)
            return
        }
    }

    m := image.NewRGBA(image.Rect(0, 0, 240, 240))
    black := color.RGBA{0, 0, 0, 255}
    draw.Draw(m, m.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)

    var img image.Image = m
    writeImage(w, &img)
}

本文网址: https://golangnote.com/topic/113.html 转摘请注明来源

Related articles

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Write a Comment to "go 实现图片服务器缓存"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.18 Processed in 0ms