6f3a759b by astaxie

gmfim add lock. fix #445

1 parent 338124e3
...@@ -5,16 +5,17 @@ import ( ...@@ -5,16 +5,17 @@ import (
5 "compress/flate" 5 "compress/flate"
6 "compress/gzip" 6 "compress/gzip"
7 "errors" 7 "errors"
8 //"fmt"
9 "io" 8 "io"
10 "io/ioutil" 9 "io/ioutil"
11 "net/http" 10 "net/http"
12 "os" 11 "os"
13 "strings" 12 "strings"
13 "sync"
14 "time" 14 "time"
15 ) 15 )
16 16
17 var gmfim map[string]*MemFileInfo = make(map[string]*MemFileInfo) 17 var gmfim map[string]*MemFileInfo = make(map[string]*MemFileInfo)
18 var lock sync.RWMutex
18 19
19 // OpenMemZipFile returns MemFile object with a compressed static file. 20 // OpenMemZipFile returns MemFile object with a compressed static file.
20 // it's used for serve static file if gzip enable. 21 // it's used for serve static file if gzip enable.
...@@ -32,12 +33,12 @@ func OpenMemZipFile(path string, zip string) (*MemFile, error) { ...@@ -32,12 +33,12 @@ func OpenMemZipFile(path string, zip string) (*MemFile, error) {
32 33
33 modtime := osfileinfo.ModTime() 34 modtime := osfileinfo.ModTime()
34 fileSize := osfileinfo.Size() 35 fileSize := osfileinfo.Size()
35 36 lock.RLock()
36 cfi, ok := gmfim[zip+":"+path] 37 cfi, ok := gmfim[zip+":"+path]
38 lock.RUnlock()
37 if ok && cfi.ModTime() == modtime && cfi.fileSize == fileSize { 39 if ok && cfi.ModTime() == modtime && cfi.fileSize == fileSize {
38 //fmt.Printf("read %s file %s from cache\n", zip, path) 40
39 } else { 41 } else {
40 //fmt.Printf("NOT read %s file %s from cache\n", zip, path)
41 var content []byte 42 var content []byte
42 if zip == "gzip" { 43 if zip == "gzip" {
43 //将文件内容压缩到zipbuf中 44 //将文件内容压缩到zipbuf中
...@@ -81,8 +82,9 @@ func OpenMemZipFile(path string, zip string) (*MemFile, error) { ...@@ -81,8 +82,9 @@ func OpenMemZipFile(path string, zip string) (*MemFile, error) {
81 } 82 }
82 83
83 cfi = &MemFileInfo{osfileinfo, modtime, content, int64(len(content)), fileSize} 84 cfi = &MemFileInfo{osfileinfo, modtime, content, int64(len(content)), fileSize}
85 lock.Lock()
86 defer lock.Unlock()
84 gmfim[zip+":"+path] = cfi 87 gmfim[zip+":"+path] = cfi
85 //fmt.Printf("%s file %s to %d, cache it\n", zip, path, len(content))
86 } 88 }
87 return &MemFile{fi: cfi, offset: 0}, nil 89 return &MemFile{fi: cfi, offset: 0}, nil
88 } 90 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!