-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader_decorator_filecache_loader.go
More file actions
88 lines (74 loc) · 2.91 KB
/
loader_decorator_filecache_loader.go
File metadata and controls
88 lines (74 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xconf/blob/main/LICENSE.
package xconf
import (
"os"
"sync"
"time"
)
// FileCacheLoader decorates another "file" loader to load configuration only
// if the file was modified. If the file was not modified since the previous load,
// the file won't be read and parsed again. You can improve performance this way,
// if you plan to load configuration multiple times (like using it in DefaultConfig with reload enabled).
type FileCacheLoader struct {
loader Loader // a "file" loader, like JSONFileLoader, YAMLFileLoader, etc...
filePath string // file's path.
cache *fileCache // cache storage.
}
// NewFileCacheLoader instantiates a new FileCacheLoader object that loads
// and caches the configuration from the original "file" loader.
// The second parameter should be the same file as the original loader's one.
func NewFileCacheLoader(loader Loader, filePath string) FileCacheLoader {
return FileCacheLoader{
loader: loader,
filePath: filePath,
cache: new(fileCache),
}
}
// Load returns decorated loader's key-value configuration map.
// If the file was modified since last load, that file will be read and parsed again,
// if not, the previous, already processed, configuration map will be returned.
func (decorator FileCacheLoader) Load() (map[string]any, error) {
fInfo, err := os.Stat(decorator.filePath)
if err != nil {
return nil, err
}
fModifiedAt := fInfo.ModTime()
if configMap := decorator.cache.load(fModifiedAt); configMap != nil {
return configMap, nil
}
configMap, err := decorator.loader.Load()
if err != nil {
return configMap, err
}
decorator.cache.save(configMap, fModifiedAt)
return configMap, nil
}
// fileCache holds caching info.
type fileCache struct {
configMap map[string]any // cached config map.
lastModified time.Time // file's last modified time.
mu sync.RWMutex // concurrency semaphore
}
// save stores configuration key-value map and file's last modified time.
func (cache *fileCache) save(configMap map[string]any, lastModified time.Time) {
cache.mu.Lock()
cache.configMap = DeepCopyConfigMap(configMap)
cache.lastModified = lastModified
cache.mu.Unlock()
}
// load retrieves configuration key-value map comparing file's modified time.
func (cache *fileCache) load(currentLastModified time.Time) map[string]any {
cache.mu.RLock()
defer cache.mu.RUnlock()
if !currentLastModified.After(cache.lastModified) {
// return a copy not to modify this state from outside (for example from a decorator,
// which usually modifies directly the original returned configuration map reference
// - for performance reasons, so we ensure from this stateful loader that we return a
// new configuration map each time)
return DeepCopyConfigMap(cache.configMap)
}
return nil
}