fix fixedmap and parsed cache check time

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
master
kim (grufwub) 4 years ago
parent 0f662c6980
commit a6cbd74580

@ -7,8 +7,6 @@ import (
)
var (
FileMonitorSleepTime = time.Duration(*CacheCheckFreq) * time.Second
/* Global file caches */
GlobalFileCache *FileCache
)
@ -18,15 +16,21 @@ func startFileCaching() {
GlobalFileCache = new(FileCache)
GlobalFileCache.Init(*CacheSize)
/* Parse the supplied CacheCheckFreq */
sleepTime, err := time.ParseDuration(*CacheCheckFreq)
if err != nil {
logSystemFatal("Error parsing supplied cache check frequency %s: %s\n", *CacheCheckFreq, err)
}
/* Start file monitor in separate goroutine */
go startFileMonitor()
go startFileMonitor(sleepTime)
}
func startFileMonitor() {
func startFileMonitor(sleepTime time.Duration) {
go func() {
for {
/* Sleep so we don't take up all the precious CPU time :) */
time.Sleep(5 * time.Second)
time.Sleep(sleepTime)
/* Check global file cache freshness */
checkCacheFreshness()
@ -134,7 +138,7 @@ func (fc *FileCache) Fetch(path string, newFileContents func(string) FileContent
contents := newFileContents(path)
/* Create new file wrapper around contents */
file := NewFile(contents)
file = NewFile(contents)
/* NOTE: file isn't in cache yet so no need to lock file write mutex
* before loading from disk

@ -28,6 +28,7 @@ func NewFixedMap(size int) *FixedMap {
fm := new(FixedMap)
fm.Map = make(map[string]*MapElement)
fm.List = list.New()
fm.Size = size
return fm
}
@ -44,7 +45,7 @@ func (fm *FixedMap) Put(key string, value *File) {
element := fm.List.PushFront(key)
fm.Map[key] = &MapElement{ element, value }
if fm.List.Len() == fm.Size {
if fm.List.Len() > fm.Size {
/* We're at capacity! SIR! */
element = fm.List.Back()
@ -54,6 +55,8 @@ func (fm *FixedMap) Put(key string, value *File) {
/* Finally delete the map entry and list element! */
delete(fm.Map, key)
fm.List.Remove(element)
logSystem("Popped key: %s\n", key)
}
}

@ -30,7 +30,7 @@ var (
LoggingType = flag.Int("log-type", 0, "Change server log file handling -- 0:default 1:disable")
/* Cache settings */
CacheCheckFreq = flag.Int("cache-check", 30, "Change file cache freshness check frequency (in seconds).")
CacheCheckFreq = flag.String("cache-check", "30s", "Change file cache freshness check frequency.")
CacheSize = flag.Int("cache-size", 1000, "Change file cache size, measured in file count.")
CacheFileSizeMax = flag.Float64("cache-file-max", 5, "Change maximum file size to be cached (in megabytes).")
)

Loading…
Cancel
Save