forked from spencerkimball/stargazers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
107 lines (96 loc) · 3.09 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
107
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package fetch
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/kennygrant/sanitize"
)
// 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(c *Context, req *http.Request) (*http.Response, error) {
filename := cacheEntryFilename(c, req.URL.String())
if err := os.MkdirAll(path.Dir(filename), 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
}
log.Printf("found %q in response cache", req.URL.String())
return resp, err
}
func readCachedResponse(filename string, req *http.Request) (*http.Response, error) {
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return http.ReadResponse(bufio.NewReader(bytes.NewBuffer(body)), req)
}
// putCache puts the supplied http.Response into the cache.
func putCache(c *Context, req *http.Request, resp *http.Response) error {
defer resp.Body.Close()
filename := cacheEntryFilename(c, req.URL.String())
f, err := os.Create(filename)
if err != nil {
return err
}
if err := resp.Write(f); err != nil {
f.Close()
return err
}
f.Close()
log.Printf("wrote %q to response cache", req.URL.String())
// TODO(spencer): this sucks, but we must re-read the response as
// the body is closed during the call to resp.Write().
if readResp, err := readCachedResponse(filename, req); err != nil {
log.Printf("failed reading cached response: %s", err)
return err
} else {
resp.Body = readResp.Body
}
return nil
}
// cacheEntryFilename creates a filename-safe name in a subdirectory
// of the configured cache dir, with any access token stripped out.
func cacheEntryFilename(c *Context, url string) string {
newUrl := strings.Replace(url, fmt.Sprintf("access_token=%s", c.Token), "", 1)
return filepath.Join(c.CacheDir, c.Repo, sanitize.BaseName(newUrl))
}
// clearEntry clears a specified cache entry.
func clearEntry(c *Context, url string) error {
filename := cacheEntryFilename(c, url)
return os.Remove(filename)
}
// Clear clears all cache entries for the repository specified in the
// fetch context.
func Clear(c *Context) error {
filename := filepath.Join(c.CacheDir, c.Repo)
return os.RemoveAll(filename)
}