From 5d01afe3a62e965e806a03f5289504148f11b3fc Mon Sep 17 00:00:00 2001 From: JessonChan Date: Mon, 16 Nov 2015 14:05:05 +0800 Subject: [PATCH] isOk to check whether the file is latest --- staticfile.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/staticfile.go b/staticfile.go index 80162e74..cb57b133 100644 --- a/staticfile.go +++ b/staticfile.go @@ -100,15 +100,13 @@ var ( func openFile(filePath string, fi os.FileInfo, acceptEncoding string) (bool, string, *serveContentHolder, error) { mapKey := acceptEncoding + ":" + filePath - mapFile, ok := staticFileMap[mapKey] - if ok { - if mapFile.modTime == fi.ModTime() && mapFile.size == fi.Size() { - return mapFile.encoding != "", mapFile.encoding, mapFile, nil - } + mapFile, _ := staticFileMap[mapKey] + if isOk(mapFile, fi) { + return mapFile.encoding != "", mapFile.encoding, mapFile, nil } mapLock.Lock() defer mapLock.Unlock() - if mapFile, ok = staticFileMap[mapKey]; !ok { + if mapFile, _ = staticFileMap[mapKey]; !isOk(mapFile, fi) { file, err := os.Open(filePath) if err != nil { return false, "", nil, err @@ -126,6 +124,13 @@ func openFile(filePath string, fi os.FileInfo, acceptEncoding string) (bool, str return mapFile.encoding != "", mapFile.encoding, mapFile, nil } +func isOk(s *serveContentHolder, fi os.FileInfo) bool { + if s == nil { + return false + } + return s.modTime == fi.ModTime() && s.size == fi.Size() +} + // isStaticCompress detect static files func isStaticCompress(filePath string) bool {