github.com/ernestokarim/closurer@v0.0.0-20130119214741-f245d086c750/cache/cache.go (about) 1 package cache 2 3 import ( 4 "encoding/gob" 5 "log" 6 "os" 7 "path/filepath" 8 9 "github.com/ernestokarim/closurer/app" 10 "github.com/ernestokarim/closurer/config" 11 ) 12 13 const CACHE_FILENAME = "cache" 14 15 // Load the caches from a file. 16 func Load() error { 17 conf := config.Current() 18 filename := filepath.Join(conf.Build, CACHE_FILENAME) 19 20 if config.NoCache { 21 return nil 22 } 23 24 f, err := os.Open(filename) 25 if err != nil && os.IsNotExist(err) { 26 return nil 27 } else if err != nil { 28 return app.Error(err) 29 } 30 defer f.Close() 31 32 log.Println("Reading cache:", filename) 33 34 d := gob.NewDecoder(f) 35 if err := d.Decode(&modificationCache); err != nil { 36 return app.Error(err) 37 } 38 if err := d.Decode(&dataCache); err != nil { 39 return app.Error(err) 40 } 41 42 log.Println("Read", len(modificationCache), "modifications and", len(dataCache), "datas!") 43 44 return nil 45 } 46 47 // Save the caches to a file. 48 func Dump() error { 49 conf := config.Current() 50 51 f, err := os.Create(filepath.Join(conf.Build, CACHE_FILENAME)) 52 if err != nil { 53 return app.Error(err) 54 } 55 defer f.Close() 56 57 log.Println("Write", len(modificationCache), "modifications and", len(dataCache), "datas!") 58 59 e := gob.NewEncoder(f) 60 if err := e.Encode(&modificationCache); err != nil { 61 return app.Error(err) 62 } 63 if err := e.Encode(&dataCache); err != nil { 64 return app.Error(err) 65 } 66 67 return nil 68 }