1 // Package diskcache provides an implementation of httpcache.Cache that uses the diskv package
2 // to supplement an in-memory map with persistent storage
10 "github.com/peterbourgon/diskv"
14 // Cache is an implementation of httpcache.Cache that supplements the in-memory map with persistent storage
19 // Get returns the response corresponding to key if present
20 func (c *Cache) Get(key string) (resp []byte, ok bool) {
21 key = keyToFilename(key)
22 resp, err := c.d.Read(key)
24 return []byte{}, false
29 // Set saves a response to the cache as key
30 func (c *Cache) Set(key string, resp []byte) {
31 key = keyToFilename(key)
32 c.d.WriteStream(key, bytes.NewReader(resp), true)
35 // Delete removes the response with key from the cache
36 func (c *Cache) Delete(key string) {
37 key = keyToFilename(key)
41 func keyToFilename(key string) string {
43 io.WriteString(h, key)
44 return hex.EncodeToString(h.Sum(nil))
47 // New returns a new Cache that will store files in basePath
48 func New(basePath string) *Cache {
50 d: diskv.New(diskv.Options{
52 CacheSizeMax: 100 * 1024 * 1024, // 100MB
57 // NewWithDiskv returns a new Cache using the provided Diskv as underlying
59 func NewWithDiskv(d *diskv.Diskv) *Cache {