2016-05-01 06:00:43 +02:00
|
|
|
package cache
|
2016-04-24 09:32:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2016-04-26 22:12:46 +02:00
|
|
|
// Entry represents an individual item in the cache.
|
2016-04-24 09:32:32 +02:00
|
|
|
type Entry struct {
|
|
|
|
URL string `json:"url"`
|
2016-04-26 22:12:46 +02:00
|
|
|
Complete bool `json:"complete"`
|
|
|
|
ContentLength string `json:"content_length"`
|
2016-04-24 09:32:32 +02:00
|
|
|
ContentType string `json:"content_type"`
|
2016-04-26 22:12:46 +02:00
|
|
|
LastModified string `json:"last_modified"`
|
2016-04-24 09:32:32 +02:00
|
|
|
}
|
|
|
|
|
2016-04-26 22:12:46 +02:00
|
|
|
// Load reads the entry from disk.
|
|
|
|
func (e *Entry) Load(filename string) error {
|
2016-04-24 09:32:32 +02:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2016-04-25 10:01:01 +02:00
|
|
|
return err
|
2016-04-24 09:32:32 +02:00
|
|
|
}
|
2016-04-26 22:12:46 +02:00
|
|
|
defer f.Close()
|
2016-04-25 10:01:01 +02:00
|
|
|
return json.NewDecoder(f).Decode(e)
|
2016-04-24 09:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes the entry to disk.
|
|
|
|
func (e *Entry) Save(filename string) error {
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-26 22:12:46 +02:00
|
|
|
defer f.Close()
|
2016-04-24 09:32:32 +02:00
|
|
|
return json.NewEncoder(f).Encode(e)
|
|
|
|
}
|