forked from kjk/notionapi
/
cache.go
106 lines (90 loc) · 2.4 KB
/
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
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
package caching_downloader
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/Nikolo/notionapi"
)
// Cache describes a caching interface
type Cache interface {
// ReadFile reads a file with a given name from cache
ReadFile(string) ([]byte, error)
// WriteFile writes a file with a given name to cache
WriteFile(string, []byte) error
// GetPageIDs returns ids of pages in the cache
GetPageIDs() ([]string, error)
// Remove removes a file with a given name from cache
Remove(string)
}
var _ Cache = &DirectoryCache{}
// DirectoryCache implements disk-based Cache interface
type DirectoryCache struct {
Dir string
mu sync.Mutex
}
// ReadFile reads a file with a given name from cache
func (c *DirectoryCache) ReadFile(name string) ([]byte, error) {
c.mu.Lock()
defer c.mu.Unlock()
path := filepath.Join(c.Dir, name)
return ioutil.ReadFile(path)
}
// WriteFile writes a file with a given name to cache
func (c *DirectoryCache) WriteFile(name string, data []byte) error {
c.mu.Lock()
defer c.mu.Unlock()
path := filepath.Join(c.Dir, name)
// make sure directory for a file exits
// ok to ignore error as WriteFile will fail too
_ = os.MkdirAll(filepath.Dir(path), 0755)
return ioutil.WriteFile(path, data, 0644)
}
// Remove removes a file with a given name from cache
func (c *DirectoryCache) Remove(name string) {
c.mu.Lock()
defer c.mu.Unlock()
path := filepath.Join(c.Dir, name)
os.Remove(path)
}
// GetPageIDs returns ids of pages in the cache
func (c *DirectoryCache) GetPageIDs() ([]string, error) {
c.mu.Lock()
defer c.mu.Unlock()
files, err := ioutil.ReadDir(c.Dir)
if err != nil {
return nil, err
}
var ids []string
for _, fi := range files {
// skip non-files
if !fi.Mode().IsRegular() {
continue
}
// valid cache files are in the format:
// ${pageID}.txt
parts := strings.Split(fi.Name(), ".")
if len(parts) != 2 || parts[1] != "txt" {
continue
}
id := notionapi.ToNoDashID(parts[0])
if !notionapi.IsValidNoDashID(id) {
//d.logf("checkVersionsOfCachedPages: unexpected file '%s' in CacheDir '%s'\n", fi.Name(), d.CacheDir)
continue
}
ids = append(ids, id)
}
return ids, nil
}
// NewDirectoryCache returns a new DirectoryCache which caches files
// in a directory
func NewDirectoryCache(dir string) (*DirectoryCache, error) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return nil, err
}
return &DirectoryCache{
Dir: dir,
}, nil
}