Skip to content

Commit 29c49be

Browse files
authored
Merge pull request #37 from elecbug/elecbug/master
feat: update cache clear part
2 parents e68a834 + 14e25cf commit 29c49be

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

graph/algorithm/cache.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,32 @@ var cachedAllShortestPaths = make(map[string]graph.Paths)
1111
var cachedAllShortestPathLengths = make(map[string]graph.PathLength)
1212
var cacheMu sync.RWMutex
1313

14-
// CacheClear clears the cached shortest paths and their lengths.
15-
func CacheClear() {
14+
// ClearCache clears the cached shortest paths and their lengths.
15+
func ClearCache() {
1616
cacheMu.Lock()
1717
defer cacheMu.Unlock()
1818
cachedAllShortestPaths = make(map[string]graph.Paths)
1919
cachedAllShortestPathLengths = make(map[string]graph.PathLength)
2020
}
2121

22-
// AutoCacheClear starts a goroutine that clears the cache at regular intervals defined by tick.
23-
func AutoCacheClear(tick time.Duration) {
22+
// ClearCacheForGraph clears the cache for a specific graph.
23+
func ClearCacheForGraph(g *graph.Graph) {
24+
cacheMu.Lock()
25+
defer cacheMu.Unlock()
26+
27+
gh := g.Hash()
28+
29+
delete(cachedAllShortestPaths, gh)
30+
delete(cachedAllShortestPathLengths, gh)
31+
}
32+
33+
// AutoClearCache starts a goroutine that clears the cache at regular intervals defined by tick.
34+
func AutoClearCache(tick time.Duration) {
2435
go func() {
2536
for {
2637
time.Sleep(tick)
2738

28-
CacheClear()
39+
ClearCache()
2940
}
3041
}()
3142
}

0 commit comments

Comments
 (0)