-
Notifications
You must be signed in to change notification settings - Fork 797
/
file_cache.go
46 lines (35 loc) · 889 Bytes
/
file_cache.go
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
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package main
import (
"os"
)
type FileCache struct {
files map[string]string
}
func NewFileCache() *FileCache {
return &FileCache{files: map[string]string{}}
}
func (fc *FileCache) LoadFile(fileName string) (string, error) {
if fc.files[fileName] == "" {
buff, err := os.ReadFile(fileName)
if err != nil {
return "", err
}
fc.files[fileName] = string(buff)
}
return fc.files[fileName], nil
}
func (fc *FileCache) UpdateFile(fileName string, text string) {
fc.files[fileName] = text
}
func (fc *FileCache) WriteAll() error {
for name, contents := range fc.files {
if err := os.WriteFile(name, []byte(contents), 0500); err != nil {
return err
}
}
return nil
}