From fcd6fd01cb2de8ebcf42c321e88fa146a0d1e95c Mon Sep 17 00:00:00 2001 From: ucwong Date: Fri, 17 Feb 2023 08:01:33 +0800 Subject: [PATCH] fix --- filecache.go | 13 +++++++------ handler.go | 46 ---------------------------------------------- 2 files changed, 7 insertions(+), 52 deletions(-) delete mode 100644 handler.go diff --git a/filecache.go b/filecache.go index f2c8680..18034d1 100644 --- a/filecache.go +++ b/filecache.go @@ -18,7 +18,7 @@ type FileCache struct { in chan *CacheInfo mutex sync.RWMutex shutdown chan any - wait sync.WaitGroup + wg sync.WaitGroup MaxItems int // Maximum number of files to cache MaxSize int64 // Maximum file size to store ExpireItem int // Seconds a file should be cached for @@ -89,14 +89,15 @@ func (cache *FileCache) addItem(name string, content []byte) (err error) { func (cache *FileCache) deleteItem(name string) { cache.mutex.Lock() + defer cache.mutex.Unlock() + delete(cache.items, name) - cache.mutex.Unlock() } // itemListener is a goroutine that listens for incoming files and caches // them. func (cache *FileCache) itemListener() { - defer cache.wait.Done() + defer cache.wg.Done() for { select { case c := <-cache.in: @@ -130,7 +131,7 @@ func (cache *FileCache) expireOldest(force bool) { // It runs periodically, every cache.Every seconds. If cache.Every is set // to 0, it will not run. func (cache *FileCache) vacuum() { - defer cache.wait.Done() + defer cache.wg.Done() if cache.Every < 1 { return } @@ -361,7 +362,7 @@ func (cache *FileCache) Start() error { cache.in = make(chan *CacheInfo, NewCachePipeSize) cache.shutdown = make(chan any) - cache.wait.Add(2) + cache.wg.Add(2) go cache.itemListener() go cache.vacuum() return nil @@ -375,7 +376,7 @@ func (cache *FileCache) Start() error { func (cache *FileCache) Stop() { close(cache.shutdown) - cache.wait.Wait() + cache.wg.Wait() if cache.items != nil { items := cache.StoredFiles() diff --git a/handler.go b/handler.go deleted file mode 100644 index 0becc72..0000000 --- a/handler.go +++ /dev/null @@ -1,46 +0,0 @@ -package filecache - -import ( - "fmt" - "mime" - "net/http" - "net/url" - "path/filepath" -) - -// HttpHandler returns a valid HTTP handler for the given cache. -func HttpHandler(cache *FileCache) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - cache.HttpWriteFile(w, r) - } -} - -func (cache *FileCache) HttpWriteFile(w http.ResponseWriter, r *http.Request) { - path, err := url.QueryUnescape(r.URL.String()) - if err != nil { - http.ServeFile(w, r, r.URL.Path) - } else if len(path) > 1 { - path = path[1:] - } else { - http.ServeFile(w, r, ".") - return - } - - if cache.InCache(path) { - itm := cache.items[path] - ctype := http.DetectContentType(itm.Access()) - mtype := mime.TypeByExtension(filepath.Ext(path)) - if mtype != "" && mtype != ctype { - ctype = mtype - } - header := w.Header() - header.Set("content-length", fmt.Sprintf("%d", itm.Size)) - header.Set("content-disposition", - fmt.Sprintf("filename=%s", filepath.Base(path))) - header.Set("content-type", ctype) - w.Write(itm.Access()) - return - } - cache.Cache(path, nil) - http.ServeFile(w, r, path) -}