diff --git a/cmd/cache.go b/cmd/cache.go index cfbedce..4c67173 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -6,9 +6,11 @@ package cmd import ( "encoding/gob" + "fmt" "net/http" "os" "sync" + "time" "github.com/julienschmidt/httprouter" "github.com/klauspost/compress/zstd" @@ -79,6 +81,8 @@ func (cache *fileCache) isEmpty() bool { } func (cache *fileCache) Export(path string) error { + startTime := time.Now() + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err @@ -95,12 +99,23 @@ func (cache *fileCache) Export(path string) error { cache.mutex.RLock() enc.Encode(&cache.list) + length := len(cache.list) cache.mutex.RUnlock() + if Verbose { + fmt.Printf("%s | CACHE: Exported %d entries in %s\n", + time.Now().Format(logDate), + length, + time.Since(startTime), + ) + } + return nil } func (cache *fileCache) Import(path string) error { + startTime := time.Now() + file, err := os.OpenFile(path, os.O_RDONLY, 0600) if err != nil { return err @@ -116,15 +131,22 @@ func (cache *fileCache) Import(path string) error { dec := gob.NewDecoder(z) cache.mutex.Lock() - err = dec.Decode(&cache.list) - + length := len(cache.list) cache.mutex.Unlock() if err != nil { return err } + if Verbose { + fmt.Printf("%s | CACHE: Imported %d entries in %s\n", + time.Now().Format(logDate), + length, + time.Since(startTime), + ) + } + return nil } @@ -146,6 +168,12 @@ func serveCacheClear(args []string, cache *fileCache, formats *types.Types, erro } func registerCacheHandlers(mux *httprouter.Router, args []string, cache *fileCache, formats *types.Types, errorChannel chan<- error) error { + register(mux, Prefix+"/clear_cache", serveCacheClear(args, cache, formats, errorChannel)) + + return nil +} + +func importCache(args []string, cache *fileCache, formats *types.Types) error { skipIndex := false if CacheFile != "" { @@ -156,15 +184,11 @@ func registerCacheHandlers(mux *httprouter.Router, args []string, cache *fileCac } if !skipIndex { - list, err := fileList(args, &filters{}, "", &fileCache{}, formats) + _, err := fileList(args, &filters{}, "", cache, formats) if err != nil { return err } - - cache.set(list) } - register(mux, Prefix+"/clear_cache", serveCacheClear(args, cache, formats, errorChannel)) - return nil } diff --git a/cmd/favicons.go b/cmd/favicons.go index f04af23..5350529 100644 --- a/cmd/favicons.go +++ b/cmd/favicons.go @@ -27,14 +27,12 @@ const ( ` ) -func serveFavicons(errorChannel chan<- error) httprouter.Handle { +func serveFavicons() httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { fname := strings.TrimPrefix(r.URL.Path, "/") data, err := favicons.ReadFile(fname) if err != nil { - errorChannel <- err - return } diff --git a/cmd/root.go b/cmd/root.go index 44582a0..7c7a4ed 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "0.94.0" + ReleaseVersion string = "0.96.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 71e634d..58e43be 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -445,6 +445,11 @@ func ServePage(args []string) error { list: []string{}, } + err = importCache(paths, cache, formats) + if err != nil { + return err + } + regexes := ®exes{ filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`), alphanumeric: regexp.MustCompile(`^[A-z0-9]*$`), @@ -485,9 +490,9 @@ func ServePage(args []string) error { register(mux, "/", redirectRoot()) } - register(mux, Prefix+"/favicons/*favicon", serveFavicons(errorChannel)) + register(mux, Prefix+"/favicons/*favicon", serveFavicons()) - register(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel)) + register(mux, Prefix+"/favicon.ico", serveFavicons()) register(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, cache, formats, errorChannel))