Skip to content

Commit b86fa51

Browse files
AnnatarHeclaude
andcommitted
feat(model): add cache support to ConfigService with skip option
Add caching mechanism to ReadConfigFile using functional options pattern. Cache is stored in-memory with RWMutex for thread safety. Use WithSkipCache() option to bypass cache and read from disk. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a65e336 commit b86fa51

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

model/config.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,36 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
"sync"
910

1011
"github.com/pelletier/go-toml/v2"
1112
)
1213

1314
var UserShellTimeConfig ShellTimeConfig
1415

1516
type ConfigService interface {
16-
ReadConfigFile(ctx context.Context) (ShellTimeConfig, error)
17+
ReadConfigFile(ctx context.Context, opts ...ReadConfigOption) (ShellTimeConfig, error)
18+
}
19+
20+
// readConfigOptions holds configuration for ReadConfigFile behavior
21+
type readConfigOptions struct {
22+
skipCache bool
23+
}
24+
25+
// ReadConfigOption is a functional option for ReadConfigFile
26+
type ReadConfigOption func(*readConfigOptions)
27+
28+
// WithSkipCache returns an option that skips the cache and reads from disk
29+
func WithSkipCache() ReadConfigOption {
30+
return func(o *readConfigOptions) {
31+
o.skipCache = true
32+
}
1733
}
1834

1935
type configService struct {
2036
configFilePath string
37+
cachedConfig *ShellTimeConfig
38+
mu sync.RWMutex
2139
}
2240

2341
func NewConfigService(configFilePath string) ConfigService {
@@ -73,10 +91,27 @@ func mergeConfig(base, local *ShellTimeConfig) {
7391
}
7492
}
7593

76-
func (cs *configService) ReadConfigFile(ctx context.Context) (config ShellTimeConfig, err error) {
94+
func (cs *configService) ReadConfigFile(ctx context.Context, opts ...ReadConfigOption) (config ShellTimeConfig, err error) {
7795
ctx, span := modelTracer.Start(ctx, "config.read")
7896
defer span.End()
7997

98+
// Apply options
99+
options := &readConfigOptions{}
100+
for _, opt := range opts {
101+
opt(options)
102+
}
103+
104+
// Check cache first (unless skipCache is set)
105+
if !options.skipCache {
106+
cs.mu.RLock()
107+
if cs.cachedConfig != nil {
108+
config = *cs.cachedConfig
109+
cs.mu.RUnlock()
110+
return config, nil
111+
}
112+
cs.mu.RUnlock()
113+
}
114+
80115
configFile := cs.configFilePath
81116
existingConfig, err := os.ReadFile(configFile)
82117
if err != nil {
@@ -142,6 +177,11 @@ func (cs *configService) ReadConfigFile(ctx context.Context) (config ShellTimeCo
142177
config.SocketPath = DefaultSocketPath
143178
}
144179

180+
// Save to cache
181+
cs.mu.Lock()
182+
cs.cachedConfig = &config
183+
cs.mu.Unlock()
184+
145185
UserShellTimeConfig = config
146186
return
147187
}

0 commit comments

Comments
 (0)