-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpersistent.go
More file actions
28 lines (25 loc) · 818 Bytes
/
persistent.go
File metadata and controls
28 lines (25 loc) · 818 Bytes
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
package httpcache
import (
"fmt"
"net/http"
"os"
"time"
)
// NewPersistent creates an http RoundTripper with a file cache.
// Cache files will be created under path.
func NewPersistent(transport http.RoundTripper, path string, TTL time.Duration) http.RoundTripper {
return &CachedRoundTrip{
Transport: transport,
Cache: &fileCache{Path: path},
TTL: TTL,
}
}
// NewPersistentClient creates an http client with a file cache.
func NewPersistentClient(path string, TTL time.Duration) (*http.Client, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil {
return nil, fmt.Errorf("httpcache: could not create dir %s: %v", path, err)
}
}
return &http.Client{Transport: NewPersistent(http.DefaultTransport, path, TTL)}, nil
}