1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // ErrCacheMiss is returned when a certificate is not found in cache.
16 var ErrCacheMiss = errors.New("acme/autocert: certificate cache miss")
18 // Cache is used by Manager to store and retrieve previously obtained certificates
21 // The key argument of the methods refers to a domain name but need not be an FQDN.
22 // Cache implementations should not rely on the key naming pattern.
23 type Cache interface {
24 // Get returns a certificate data for the specified key.
25 // If there's no such key, Get returns ErrCacheMiss.
26 Get(ctx context.Context, key string) ([]byte, error)
28 // Put stores the data in the cache under the specified key.
29 // Underlying implementations may use any data storage format,
30 // as long as the reverse operation, Get, results in the original data.
31 Put(ctx context.Context, key string, data []byte) error
33 // Delete removes a certificate data from the cache under the specified key.
34 // If there's no such key in the cache, Delete returns nil.
35 Delete(ctx context.Context, key string) error
38 // DirCache implements Cache using a directory on the local filesystem.
39 // If the directory does not exist, it will be created with 0700 permissions.
42 // Get reads a certificate data from the specified file name.
43 func (d DirCache) Get(ctx context.Context, name string) ([]byte, error) {
44 name = filepath.Join(string(d), name)
48 done = make(chan struct{})
51 data, err = ioutil.ReadFile(name)
59 if os.IsNotExist(err) {
60 return nil, ErrCacheMiss
65 // Put writes the certificate data to the specified file name.
66 // The file will be created with 0600 permissions.
67 func (d DirCache) Put(ctx context.Context, name string, data []byte) error {
68 if err := os.MkdirAll(string(d), 0700); err != nil {
72 done := make(chan struct{})
77 if tmp, err = d.writeTempFile(name, data); err != nil {
82 // Don't overwrite the file if the context was canceled.
84 newName := filepath.Join(string(d), name)
85 err = os.Rename(tmp, newName)
96 // Delete removes the specified file name.
97 func (d DirCache) Delete(ctx context.Context, name string) error {
98 name = filepath.Join(string(d), name)
101 done = make(chan struct{})
104 err = os.Remove(name)
112 if err != nil && !os.IsNotExist(err) {
118 // writeTempFile writes b to a temporary file, closes the file and returns its path.
119 func (d DirCache) writeTempFile(prefix string, b []byte) (string, error) {
120 // TempFile uses 0600 permissions
121 f, err := ioutil.TempFile(string(d), prefix)
125 if _, err := f.Write(b); err != nil {
129 return f.Name(), f.Close()