update: resource cache

This commit is contained in:
boojack 2021-12-18 23:04:30 +08:00
parent 1710cc526f
commit 8eb4273e4a

View File

@ -2,11 +2,11 @@ package api
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"memos/api/e" "memos/api/e"
"memos/store" "memos/store"
"net/http" "net/http"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -55,7 +55,7 @@ func handleUploadResource(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
e.ErrorHandler(w, "UPLOAD_FILE_ERROR", "Read file error") e.ErrorHandler(w, "UPLOAD_FILE_ERROR", "Read file error")
fmt.Println(err) return
} }
resource, err := store.CreateResource(userId, filename, fileBytes, filetype, size) resource, err := store.CreateResource(userId, filename, fileBytes, filetype, size)
@ -95,6 +95,17 @@ func handleGetResource(w http.ResponseWriter, r *http.Request) {
resourceId := vars["id"] resourceId := vars["id"]
filename := vars["filename"] filename := vars["filename"]
etag := `"` + resourceId + "/" + filename + `"`
w.Header().Set("Etag", etag)
w.Header().Set("Cache-Control", "max-age=2592000")
if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified)
return
}
}
resource, err := store.GetResourceByIdAndFilename(resourceId, filename) resource, err := store.GetResourceByIdAndFilename(resourceId, filename)
if err != nil { if err != nil {