-
Notifications
You must be signed in to change notification settings - Fork 26
/
httpsCache.go
49 lines (41 loc) · 1.02 KB
/
httpsCache.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
package main
import (
"context"
"errors"
"golang.org/x/crypto/acme/autocert"
)
// Make sure the httpsCache type implements the Cache interface
var _ autocert.Cache = (*httpsCache)(nil)
type httpsCache struct {
db *database
}
func (c *httpsCache) check() error {
if c.db == nil {
return errors.New("no database")
}
return nil
}
func (c *httpsCache) Get(ctx context.Context, key string) ([]byte, error) {
if err := c.check(); err != nil {
return nil, err
}
d, err := c.db.retrievePersistentCacheContext(ctx, "https_"+key)
if d == nil && err == nil {
return nil, autocert.ErrCacheMiss
} else if err != nil {
return nil, err
}
return d, nil
}
func (c *httpsCache) Put(ctx context.Context, key string, data []byte) error {
if err := c.check(); err != nil {
return err
}
return c.db.cachePersistentlyContext(ctx, "https_"+key, data)
}
func (c *httpsCache) Delete(ctx context.Context, key string) error {
if err := c.check(); err != nil {
return err
}
return c.db.clearPersistentCacheContext(ctx, "https_"+key)
}