X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=titlecache.go;h=5901fd4e104d52c3af7179c2256ffc010bda04cf;hb=cecf0a395b431927ca917ffe2b3dddd953a5b6ad;hp=c4ff40450cd178c1eb6382a41a98920769f1900f;hpb=59218ebf4cf9c2353dfa2cb416ee7d2542a3b9b9;p=go-anidb.git diff --git a/titlecache.go b/titlecache.go index c4ff404..5901fd4 100644 --- a/titlecache.go +++ b/titlecache.go @@ -4,41 +4,30 @@ import ( "bytes" "github.com/Kovensky/go-anidb/titles" "io" + "log" "net/http" - "sync" "time" ) -var titlesFileData []byte -var titlesFileDataLock sync.Mutex var titlesDB = &titles.TitlesDatabase{} -// Loads the anime-titles database from the given io.Reader. -// -// Caches the io.Reader's contents on memory, which gets saved -// by DumpCaches. -func LoadTitles(src io.Reader) error { - buf := bytes.Buffer{} - _, err := io.Copy(&buf, src) - if err != nil && err != io.EOF { - return err +// Loads the database from anime-titles.dat.gz in the cache dir. +func RefreshTitles() error { + if flock := lockFile(cachePath("anime-titles.dat.gz")); flock != nil { + flock.Lock() + defer flock.Unlock() } - titlesFileDataLock.Lock() - defer titlesFileDataLock.Unlock() - - titlesFileData = buf.Bytes() - - titlesDB.LoadDB(bytes.NewReader(titlesFileData)) + fh, err := cache.Open("anime-titles.dat.gz") + if err != nil { + return err + } + defer fh.Close() + titlesDB.LoadDB(fh) return nil } -// Saves the currently cached anime-titles database to the given io.Writer. -func (adb *AniDB) SaveCurrentTitles(dst io.Writer) (int64, error) { - return io.Copy(dst, bytes.NewReader(titlesFileData)) -} - // Returns true if the titles database is up-to-date (newer than 24 hours). func TitlesUpToDate() (ok bool) { return time.Now().Sub(titlesDB.UpdateTime) < 24*time.Hour @@ -46,18 +35,50 @@ func TitlesUpToDate() (ok bool) { // Downloads a new anime-titles database if the database is outdated. // -// Caches the contents on memory, which gets saved by DumpCaches. +// Saves the database as anime-titles.dat.gz in the cache dir. func UpdateTitles() error { // too new, no need to update if TitlesUpToDate() { return nil } - resp, err := http.Get(titles.DataDumpURL) + if flock := lockFile(cachePath("anime-titles.dat.gz")); flock != nil { + flock.Lock() + defer flock.Unlock() + } + + c := &http.Client{Transport: &http.Transport{DisableCompression: true}} + + log.Printf("HTTP>>> %s", titles.DataDumpURL) + + resp, err := c.Get(titles.DataDumpURL) if err != nil { + log.Printf("HTTP<<< %s", resp.Status) return err } defer resp.Body.Close() - return LoadTitles(resp.Body) + buf := bytes.Buffer{} + log.Printf("HTTP--- %s", resp.Status) + + _, err = io.Copy(&buf, resp.Body) + if err != nil { + log.Printf("HTTP--- %v", err) + return err + } + + fh, err := cache.Create("anime-titles.dat.gz") + if err != nil { + return err + } + + _, err = io.Copy(fh, &buf) + if err != nil { + return err + } + + defer func() { + log.Printf("HTTP<<< Titles version %s", titlesDB.UpdateTime) + }() + return RefreshTitles() }