-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
133 lines (112 loc) · 2.55 KB
/
Copy pathcache.go
File metadata and controls
133 lines (112 loc) · 2.55 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/gob"
"log"
"os"
"sync"
"time"
)
type CommitStat struct {
Additions int
Deletions int
}
type RepoMetadata struct {
Branches []string
Tags []string
ReadmeName string
LicenseName string
TotalCommits int
Version int
}
const CurrentMetadataVersion = 1
type LangCacheKey struct {
RepoName string
CommitHash string
}
var (
commitStatsCache sync.Map // string -> CommitStat
repoMetadataCache sync.Map // string -> RepoMetadata
langCache sync.Map // LangCacheKey -> []LanguageStat
saveChan = make(chan struct{}, 1)
)
const cacheFile = "bbgit.cache"
func NotifySave() {
select {
case saveChan <- struct{}{}:
default:
}
}
func StartSaver() {
go func() {
for range saveChan {
// Throttle saves to once per second
time.Sleep(1 * time.Second)
// Drain any additional signals during sleep
for len(saveChan) > 0 {
<-saveChan
}
if err := SaveCache(); err != nil {
log.Printf("Error saving cache: %v", err)
}
}
}()
}
type cacheContainer struct {
CommitStats map[string]CommitStat
RepoMetadata map[string]RepoMetadata
LangStats map[LangCacheKey][]LanguageStat
}
func SaveCache() error {
container := cacheContainer{
CommitStats: make(map[string]CommitStat),
RepoMetadata: make(map[string]RepoMetadata),
LangStats: make(map[LangCacheKey][]LanguageStat),
}
commitStatsCache.Range(func(k, v interface{}) bool {
container.CommitStats[k.(string)] = v.(CommitStat)
return true
})
repoMetadataCache.Range(func(k, v interface{}) bool {
container.RepoMetadata[k.(string)] = v.(RepoMetadata)
return true
})
langCache.Range(func(k, v interface{}) bool {
container.LangStats[k.(LangCacheKey)] = v.([]LanguageStat)
return true
})
f, err := os.Create(cacheFile)
if err != nil {
return err
}
defer f.Close()
if err := gob.NewEncoder(f).Encode(container); err != nil {
return err
}
log.Printf("OPS Cache saved to %s", cacheFile)
return nil
}
func LoadCache() {
f, err := os.Open(cacheFile)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("Error opening cache file: %v", err)
}
return
}
defer f.Close()
var container cacheContainer
if err := gob.NewDecoder(f).Decode(&container); err != nil {
log.Printf("Error decoding cache file: %v", err)
return
}
for k, v := range container.CommitStats {
commitStatsCache.Store(k, v)
}
for k, v := range container.RepoMetadata {
repoMetadataCache.Store(k, v)
}
for k, v := range container.LangStats {
langCache.Store(k, v)
}
log.Printf("Loaded cache from %s", cacheFile)
}