Added Get() and Set() functions for file index
This commit is contained in:
parent
fcdb81a9b7
commit
340b637baf
30
cmd/files.go
30
cmd/files.go
|
@ -40,6 +40,20 @@ type Index struct {
|
||||||
List []string
|
List []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Index) Get() []string {
|
||||||
|
i.Mutex.RLock()
|
||||||
|
val := i.List
|
||||||
|
i.Mutex.RUnlock()
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Index) Set(val []string) {
|
||||||
|
i.Mutex.Lock()
|
||||||
|
i.List = val
|
||||||
|
i.Mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Index) GenerateCache(args []string) error {
|
func (i *Index) GenerateCache(args []string) error {
|
||||||
filters := &Filters{}
|
filters := &Filters{}
|
||||||
|
|
||||||
|
@ -53,6 +67,14 @@ func (i *Index) GenerateCache(args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Index) IsEmpty() bool {
|
||||||
|
i.Mutex.RLock()
|
||||||
|
length := len(i.List)
|
||||||
|
i.Mutex.RUnlock()
|
||||||
|
|
||||||
|
return length == 0
|
||||||
|
}
|
||||||
|
|
||||||
type Dimensions struct {
|
type Dimensions struct {
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
|
@ -570,8 +592,8 @@ func prepareDirectories(files *Files, sort string) []string {
|
||||||
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
func pickFile(args []string, filters *Filters, sort string, index *Index) (string, error) {
|
||||||
var fileList []string
|
var fileList []string
|
||||||
|
|
||||||
if Cache && filters.IsEmpty() && len(index.List) != 0 {
|
if Cache && filters.IsEmpty() && !index.IsEmpty() {
|
||||||
fileList = index.List
|
fileList = index.Get()
|
||||||
} else {
|
} else {
|
||||||
files := &Files{
|
files := &Files{
|
||||||
List: make(map[string][]string),
|
List: make(map[string][]string),
|
||||||
|
@ -605,9 +627,7 @@ func pickFile(args []string, filters *Filters, sort string, index *Index) (strin
|
||||||
fileList = prepareDirectories(files, sort)
|
fileList = prepareDirectories(files, sort)
|
||||||
|
|
||||||
if Cache {
|
if Cache {
|
||||||
index.Mutex.Lock()
|
index.Set(fileList)
|
||||||
index.List = append(index.List, fileList...)
|
|
||||||
index.Mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.32.0"
|
var Version = "0.32.1"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
|
|
@ -486,7 +486,7 @@ func ServePage(args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/clear_cache", serveCacheClearHandler(args, index))
|
http.Handle("/_/clear_cache", serveCacheClearHandler(args, index))
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := &ServeStats{
|
stats := &ServeStats{
|
||||||
|
@ -502,7 +502,7 @@ func ServePage(args []string) error {
|
||||||
http.HandleFunc("/favicon.ico", doNothing)
|
http.HandleFunc("/favicon.ico", doNothing)
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
http.Handle("/stats", serveStatsHandler(args, stats))
|
http.Handle("/_/stats", serveStatsHandler(args, stats))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)
|
err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)
|
||||||
|
|
Loading…
Reference in New Issue