Log import/export of cache, and hopefully fix cache rebuild functionality

This commit is contained in:
Seednode 2023-09-26 11:15:08 -05:00
parent 926962048e
commit f782846f02
4 changed files with 50 additions and 27 deletions

View File

@ -6,9 +6,11 @@ package cmd
import (
"encoding/gob"
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/julienschmidt/httprouter"
"github.com/klauspost/compress/zstd"
@ -83,6 +85,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
@ -99,12 +103,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
@ -123,26 +138,38 @@ func (cache *fileCache) Import(path string) error {
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
}
func serveCacheClear(args []string, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
list, err := fileList(args, &filters{}, "")
cache.mutex.Lock()
cache.list = nil
cache.mutex.Unlock()
_, err := fileList(args, &filters{}, "")
if err != nil {
errorChannel <- err
return
}
cache.set(list)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Ok"))
@ -150,24 +177,6 @@ func serveCacheClear(args []string, errorChannel chan<- error) httprouter.Handle
}
func registerCacheHandlers(mux *httprouter.Router, args []string, errorChannel chan<- error) error {
skipIndex := false
if CacheFile != "" {
err := cache.Import(CacheFile)
if err == nil {
skipIndex = true
}
}
if !skipIndex {
list, err := fileList(args, &filters{}, "")
if err != nil {
return err
}
cache.set(list)
}
register(mux, Prefix+"/clear_cache", serveCacheClear(args, errorChannel))
return nil

View File

@ -27,14 +27,12 @@ const (
<meta name="theme-color" content="#ffffff">`
)
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
}

View File

@ -12,7 +12,7 @@ import (
)
const (
ReleaseVersion string = "0.95.0"
ReleaseVersion string = "0.95.1"
)
var (

View File

@ -436,6 +436,22 @@ func ServePage(args []string) error {
return ErrNoMediaFound
}
skipIndex := false
if CacheFile != "" {
err := cache.Import(CacheFile)
if err == nil {
skipIndex = true
}
}
if !skipIndex {
_, err := fileList(args, &filters{}, "")
if err != nil {
return err
}
}
mux := httprouter.New()
listenHost := net.JoinHostPort(Bind, strconv.Itoa(Port))
@ -471,9 +487,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, errorChannel))