Skip to content

Commit f0dc1d5

Browse files
committed
Refactor versionCache to use boundedCache with concurrency support
1 parent 3b90e4b commit f0dc1d5

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

version.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,32 @@ var SemanticVersionRegex = regexp.MustCompile(`^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:
1515
var simpleNumericRegex = regexp.MustCompile(`^\d+$`)
1616

1717
// versionCache caches parsed versions to avoid re-parsing the same strings.
18-
var versionCache sync.Map
18+
var versionCache = &boundedCache{
19+
items: make(map[string]*VersionInfo),
20+
max: 10000,
21+
}
22+
23+
type boundedCache struct {
24+
mu sync.RWMutex
25+
items map[string]*VersionInfo
26+
max int
27+
}
28+
29+
func (c *boundedCache) Load(key string) (*VersionInfo, bool) {
30+
c.mu.RLock()
31+
v, ok := c.items[key]
32+
c.mu.RUnlock()
33+
return v, ok
34+
}
35+
36+
func (c *boundedCache) Store(key string, value *VersionInfo) {
37+
c.mu.Lock()
38+
if len(c.items) >= c.max {
39+
c.items = make(map[string]*VersionInfo)
40+
}
41+
c.items[key] = value
42+
c.mu.Unlock()
43+
}
1944

2045
// VersionInfo represents a parsed version with its components.
2146
type VersionInfo struct {
@@ -35,7 +60,7 @@ func ParseVersion(s string) (*VersionInfo, error) {
3560

3661
// Check cache first
3762
if cached, ok := versionCache.Load(s); ok {
38-
return cached.(*VersionInfo), nil
63+
return cached, nil
3964
}
4065

4166
v := &VersionInfo{Original: s}

0 commit comments

Comments
 (0)