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 phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

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

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