| 1 | // Package readcache is een optionele, wegwerpbare cache voor afgeleide |
| 2 | // paginadata (README, trees, laatste commits, ref-lijsten, tellingen). |
| 3 | // De bron blijft altijd git: een fout in de cache betekent een miss. |
| 4 | // |
| 5 | // Indeling: <root>/cache/<repoID>/{sha,refs}/<hash>. Alleen cache/ is van |
| 6 | // deze package; Clear en LRU raken niets anders onder root aan. |
| 7 | package readcache |
| 8 | |
| 9 | import ( |
| 10 | "container/list" |
| 11 | "crypto/sha256" |
| 12 | "encoding/hex" |
| 13 | "encoding/json" |
| 14 | "errors" |
| 15 | "io/fs" |
| 16 | "os" |
| 17 | "path/filepath" |
| 18 | "strconv" |
| 19 | "strings" |
| 20 | "sync" |
| 21 | "sync/atomic" |
| 22 | ) |
| 23 | |
| 24 | // DefaultMax is de standaardgrootte: 512 MiB. |
| 25 | const DefaultMax = 512 << 20 |
| 26 | |
| 27 | // Cache is veilig voor gelijktijdig gebruik. Een nil *Cache is uit. |
| 28 | type Cache struct { |
| 29 | dir string |
| 30 | max int64 |
| 31 | |
| 32 | mu sync.Mutex |
| 33 | lru *list.List // voorkant = recent |
| 34 | items map[string]*list.Element |
| 35 | size int64 |
| 36 | |
| 37 | hits, misses atomic.Int64 |
| 38 | gens map[int64]uint64 |
| 39 | allGen uint64 |
| 40 | } |
| 41 | |
| 42 | type entry struct { |
| 43 | rel string |
| 44 | size int64 |
| 45 | } |
| 46 | |
| 47 | // Open maakt <root>/cache aan en leest de bestaande inhoud in. |
| 48 | func Open(root string, max int64) (*Cache, error) { |
| 49 | root = strings.TrimSpace(root) |
| 50 | if root == "" { |
| 51 | return nil, errors.New("readcache: geen pad") |
| 52 | } |
| 53 | if max <= 0 { |
| 54 | max = DefaultMax |
| 55 | } |
| 56 | dir := filepath.Join(root, "cache") |
| 57 | if err := os.MkdirAll(dir, 0o750); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | c := &Cache{dir: dir, max: max, lru: list.New(), items: map[string]*list.Element{}, gens: map[int64]uint64{}} |
| 61 | type found struct { |
| 62 | rel string |
| 63 | size int64 |
| 64 | mod int64 |
| 65 | } |
| 66 | var all []found |
| 67 | _ = filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error { |
| 68 | if err != nil || d.IsDir() { |
| 69 | return nil |
| 70 | } |
| 71 | if strings.HasPrefix(d.Name(), ".tmp-") { |
| 72 | _ = os.Remove(p) |
| 73 | return nil |
| 74 | } |
| 75 | info, err := d.Info() |
| 76 | if err != nil { |
| 77 | return nil |
| 78 | } |
| 79 | rel, _ := filepath.Rel(dir, p) |
| 80 | all = append(all, found{rel, info.Size(), info.ModTime().UnixNano()}) |
| 81 | return nil |
| 82 | }) |
| 83 | // oudste achteraan |
| 84 | for i := 1; i < len(all); i++ { |
| 85 | for j := i; j > 0 && all[j].mod > all[j-1].mod; j-- { |
| 86 | all[j], all[j-1] = all[j-1], all[j] |
| 87 | } |
| 88 | } |
| 89 | for _, f := range all { |
| 90 | c.items[f.rel] = c.lru.PushBack(&entry{f.rel, f.size}) |
| 91 | c.size += f.size |
| 92 | } |
| 93 | c.mu.Lock() |
| 94 | c.evictLocked() |
| 95 | c.mu.Unlock() |
| 96 | return c, nil |
| 97 | } |
| 98 | |
| 99 | // Dir is de cachemap zelf (<root>/cache). |
| 100 | func (c *Cache) Dir() string { |
| 101 | if c == nil { |
| 102 | return "" |
| 103 | } |
| 104 | return c.dir |
| 105 | } |
| 106 | |
| 107 | func repoDir(repoID int64) string { return strconv.FormatInt(repoID, 10) } |
| 108 | |
| 109 | // rel geeft het pad voor een sleutel. refs=true hoort bij een ref-naam |
| 110 | // en verdwijnt bij elke ref-wijziging; anders is de sleutel onveranderlijk. |
| 111 | func rel(repoID int64, refs bool, key string) string { |
| 112 | sum := sha256.Sum256([]byte(key)) |
| 113 | ns := "sha" |
| 114 | if refs { |
| 115 | ns = "refs" |
| 116 | } |
| 117 | return filepath.Join(repoDir(repoID), ns, hex.EncodeToString(sum[:16])) |
| 118 | } |
| 119 | |
| 120 | // GetJSON vult v uit de cache. false bij een miss of corrupte entry. |
| 121 | func (c *Cache) GetJSON(repoID int64, refs bool, key string, v any) bool { |
| 122 | if c == nil { |
| 123 | return false |
| 124 | } |
| 125 | r := rel(repoID, refs, key) |
| 126 | b, err := os.ReadFile(filepath.Join(c.dir, r)) |
| 127 | if err != nil { |
| 128 | c.misses.Add(1) |
| 129 | return false |
| 130 | } |
| 131 | // eerste regel is de volledige sleutel, tegen hash-botsingen |
| 132 | nl := strings.IndexByte(string(b), '\n') |
| 133 | if nl < 0 || string(b[:nl]) != key || json.Unmarshal(b[nl+1:], v) != nil { |
| 134 | c.misses.Add(1) |
| 135 | c.remove(r) |
| 136 | return false |
| 137 | } |
| 138 | c.hits.Add(1) |
| 139 | c.mu.Lock() |
| 140 | if el, ok := c.items[r]; ok { |
| 141 | c.lru.MoveToFront(el) |
| 142 | } |
| 143 | c.mu.Unlock() |
| 144 | return true |
| 145 | } |
| 146 | |
| 147 | // Gen is de generatie van een repo. Lees die vóór git en geef hem aan |
| 148 | // PutJSONAt: een ref-wijziging tussendoor maakt de schrijfactie ongeldig. |
| 149 | func (c *Cache) Gen(repoID int64) uint64 { |
| 150 | if c == nil { |
| 151 | return 0 |
| 152 | } |
| 153 | c.mu.Lock() |
| 154 | defer c.mu.Unlock() |
| 155 | return c.gens[repoID] + c.allGen<<32 |
| 156 | } |
| 157 | |
| 158 | // PutJSON schrijft atomisch. Fouten worden genegeerd. |
| 159 | func (c *Cache) PutJSON(repoID int64, refs bool, key string, v any) { |
| 160 | c.PutJSONAt(repoID, refs, key, v, c.Gen(repoID)) |
| 161 | } |
| 162 | |
| 163 | // PutJSONAt schrijft alleen als de generatie nog gen is. |
| 164 | func (c *Cache) PutJSONAt(repoID int64, refs bool, key string, v any, gen uint64) { |
| 165 | if c == nil || strings.ContainsRune(key, '\n') { |
| 166 | return |
| 167 | } |
| 168 | body, err := json.Marshal(v) |
| 169 | if err != nil { |
| 170 | return |
| 171 | } |
| 172 | data := append([]byte(key+"\n"), body...) |
| 173 | if int64(len(data)) > c.max/4 { |
| 174 | return |
| 175 | } |
| 176 | r := rel(repoID, refs, key) |
| 177 | full := filepath.Join(c.dir, r) |
| 178 | if err := os.MkdirAll(filepath.Dir(full), 0o750); err != nil { |
| 179 | return |
| 180 | } |
| 181 | tmp, err := os.CreateTemp(filepath.Dir(full), ".tmp-*") |
| 182 | if err != nil { |
| 183 | return |
| 184 | } |
| 185 | _, werr := tmp.Write(data) |
| 186 | cerr := tmp.Close() |
| 187 | if werr != nil || cerr != nil || os.Rename(tmp.Name(), full) != nil { |
| 188 | _ = os.Remove(tmp.Name()) |
| 189 | return |
| 190 | } |
| 191 | c.mu.Lock() |
| 192 | if c.gens[repoID]+c.allGen<<32 != gen { |
| 193 | c.mu.Unlock() |
| 194 | _ = os.Remove(full) |
| 195 | return |
| 196 | } |
| 197 | if el, ok := c.items[r]; ok { |
| 198 | c.size -= el.Value.(*entry).size |
| 199 | c.lru.Remove(el) |
| 200 | } |
| 201 | c.items[r] = c.lru.PushFront(&entry{r, int64(len(data))}) |
| 202 | c.size += int64(len(data)) |
| 203 | c.evictLocked() |
| 204 | c.mu.Unlock() |
| 205 | } |
| 206 | |
| 207 | func (c *Cache) evictLocked() { |
| 208 | for c.size > c.max { |
| 209 | el := c.lru.Back() |
| 210 | if el == nil { |
| 211 | return |
| 212 | } |
| 213 | e := el.Value.(*entry) |
| 214 | _ = os.Remove(filepath.Join(c.dir, e.rel)) |
| 215 | c.lru.Remove(el) |
| 216 | delete(c.items, e.rel) |
| 217 | c.size -= e.size |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func (c *Cache) remove(r string) { |
| 222 | _ = os.Remove(filepath.Join(c.dir, r)) |
| 223 | c.mu.Lock() |
| 224 | if el, ok := c.items[r]; ok { |
| 225 | c.size -= el.Value.(*entry).size |
| 226 | c.lru.Remove(el) |
| 227 | delete(c.items, r) |
| 228 | } |
| 229 | c.mu.Unlock() |
| 230 | } |
| 231 | |
| 232 | func (c *Cache) dropPrefix(repoID int64, prefix string) { |
| 233 | c.mu.Lock() |
| 234 | defer c.mu.Unlock() |
| 235 | c.gens[repoID]++ |
| 236 | _ = os.RemoveAll(filepath.Join(c.dir, prefix)) |
| 237 | for r, el := range c.items { |
| 238 | if r == prefix || strings.HasPrefix(r, prefix+string(filepath.Separator)) { |
| 239 | c.size -= el.Value.(*entry).size |
| 240 | c.lru.Remove(el) |
| 241 | delete(c.items, r) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // DropRefs vergeet alles wat van een ref-naam afhangt. |
| 247 | func (c *Cache) DropRefs(repoID int64) { |
| 248 | if c != nil { |
| 249 | c.dropPrefix(repoID, filepath.Join(repoDir(repoID), "refs")) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // DropRepo vergeet alles van één repo (verwijderd, hernoemd, zichtbaarheid). |
| 254 | func (c *Cache) DropRepo(repoID int64) { |
| 255 | if c != nil { |
| 256 | c.dropPrefix(repoID, repoDir(repoID)) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Clear leegt de hele cache/-map, en niets daarbuiten. |
| 261 | func (c *Cache) Clear() { |
| 262 | if c == nil { |
| 263 | return |
| 264 | } |
| 265 | c.mu.Lock() |
| 266 | defer c.mu.Unlock() |
| 267 | ents, _ := os.ReadDir(c.dir) |
| 268 | for _, e := range ents { |
| 269 | _ = os.RemoveAll(filepath.Join(c.dir, e.Name())) |
| 270 | } |
| 271 | c.allGen++ |
| 272 | c.lru.Init() |
| 273 | c.items = map[string]*list.Element{} |
| 274 | c.size = 0 |
| 275 | } |
| 276 | |
| 277 | // Usage is de stand voor Beheer. |
| 278 | type Usage struct { |
| 279 | Dir string |
| 280 | Bytes int64 |
| 281 | Max int64 |
| 282 | Entries int |
| 283 | Hits int64 |
| 284 | Misses int64 |
| 285 | } |
| 286 | |
| 287 | // Usage geeft de huidige stand. |
| 288 | func (c *Cache) Usage() Usage { |
| 289 | if c == nil { |
| 290 | return Usage{} |
| 291 | } |
| 292 | c.mu.Lock() |
| 293 | defer c.mu.Unlock() |
| 294 | return Usage{Dir: c.dir, Bytes: c.size, Max: c.max, Entries: len(c.items), Hits: c.hits.Load(), Misses: c.misses.Load()} |
| 295 | } |