Skip to content

Commit

Permalink
simplify cache data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 15, 2024
1 parent 1bf698d commit e028eaa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 4 additions & 5 deletions internal/agent/sftp/filelister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/pkg/sftp"
"github.com/sonroyaalmerol/pbs-plus/internal/agent/cache"
Expand Down Expand Up @@ -44,8 +43,8 @@ func (f *CustomFileInfo) Size() int64 {

// Check size cache with read lock
if snapSizes, ok := cache.SizeCache.Load(f.snapshotId); ok {
if cachedSize, ok := snapSizes.(*sync.Map).Load(f.filePath); ok {
return cachedSize.(int64)
if cachedSize, ok := snapSizes.(map[string]int64)[f.filePath]; ok {
return cachedSize
}
}

Expand All @@ -61,8 +60,8 @@ func (f *CustomFileInfo) Size() int64 {
return 0
}

snapSizes, _ := cache.SizeCache.LoadOrStore(f.snapshotId, &sync.Map{})
snapSizes.(*sync.Map).Store(f.filePath, byteCount)
snapSizes, _ := cache.SizeCache.LoadOrStore(f.snapshotId, map[string]int64{})
snapSizes.(map[string]int64)[f.filePath] = byteCount

return byteCount
}
Expand Down
21 changes: 20 additions & 1 deletion internal/agent/snapshots/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ func (instance *WinVSSSnapshot) UpdateTimestamp() {
}

func (instance *WinVSSSnapshot) Close() {
_, _ = cache.SizeCache.LoadAndDelete(instance.Id)
if fileMap, ok := cache.SizeCache.Load(instance.Id); ok {
clear(fileMap.(map[string]int64))
cache.SizeCache.Delete(instance.Id)
}

_ = vss.Remove(instance.Id)
_ = os.Remove(instance.SnapshotPath)
Expand All @@ -144,3 +147,19 @@ func CloseAllSnapshots() {
}
}
}

func GarbageCollect() {
knownSnaps := &KnownSnapshots{
registry: "KnownSnaps",
}

if knownSnapshots, err := knownSnaps.GetAll(); err == nil {
for _, snapshot := range knownSnapshots {
if knownSnap, err := knownSnaps.Get(snapshot.Id); err == nil {
if time.Since(knownSnap.GetTimestamp()) >= 15*time.Minute {
knownSnap.Close()
}
}
}
}
}

0 comments on commit e028eaa

Please sign in to comment.