This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
cache.go
98 lines (81 loc) · 2.56 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
package gql
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/kennygrant/sanitize"
"github.com/Ullaakut/astronomer/pkg/context"
)
// getCache searches the cache directory for a file matching the
// supplied request's URL. If found, the file contains a cached copy
// of the HTTP response. The contents are read into an http.Response
// object and returned.
func getCache(ctx *context.Context, req *http.Request, pagination string) (*http.Response, error) {
filename := cacheEntryFilename(ctx, req.URL.String()+pagination)
pathToCreate := path.Dir(filename)
if err := os.MkdirAll(pathToCreate, os.ModeDir|0755); err != nil {
return nil, err
}
resp, err := readCachedResponse(filename, req)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return resp, nil
}
func readCachedResponse(filename string, req *http.Request) (*http.Response, error) {
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &http.Response{
Body: ioutil.NopCloser(bytes.NewReader(body)),
}, nil
}
// putCache puts the supplied http.Response into the cache.
func putCache(ctx *context.Context, req *http.Request, pagination string, body []byte) error {
filename := cacheEntryFilename(ctx, req.URL.String()+pagination)
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("unable to create cache file: %v", err)
}
defer f.Close()
_, err = f.Write(body)
if err != nil {
return fmt.Errorf("unable to write response in cache file: %v", err)
}
_, err = readCachedResponse(filename, req)
if err != nil {
return err
}
return nil
}
// cacheEntryFilename creates a filename-safe name in a subdirectory
// of the configured cache dir, with any access token stripped out.
func cacheEntryFilename(ctx *context.Context, url string) string {
newURL := strings.Replace(url, fmt.Sprintf("access_token=%s", ctx.GithubToken), "", 1)
return filepath.Join(ctx.CacheDirectoryPath, ctx.RepoOwner, ctx.RepoName, sanitize.BaseName(newURL))
}
// listilePagination generates the pagination to append to the cache file names
// for stargazer lists.
func listFilePagination(cursor string) string {
if cursor == "" {
return fmt.Sprintf("-list-firstpage")
}
return fmt.Sprintf("-list-%s", cursor)
}
// contribFilePagination generates the pagination to append to the cache file names
// for user contribution data.
func contribFilePagination(cursor string, year int) string {
if cursor == "" {
return fmt.Sprintf("-firstpage-%d", year)
}
return fmt.Sprintf("-%s-%d", cursor, year)
}