-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
122 lines (111 loc) · 3.29 KB
/
cache.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2020 Allen Li
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package anidb
import (
"encoding/gob"
"fmt"
"os"
"path/filepath"
)
// A TitlesCache is a cache for AniDB titles data.
type TitlesCache struct {
// Path is the path to the cache file.
Path string
// Titles is the titles loaded from the cache.
Titles []AnimeT
// Updated indicates if the cached titles were updated.
// This is set to true when any method updates the cache.
Updated bool
}
// DefaultTitlesCache opens a TitlesCache at a default location,
// using XDG_CACHE_DIR.
func DefaultTitlesCache() (*TitlesCache, error) {
return OpenTitlesCache(defaultTitlesCacheFile())
}
// OpenTitlesCache opens a TitlesCache.
func OpenTitlesCache(path string) (*TitlesCache, error) {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return &TitlesCache{Path: path}, nil
}
return nil, fmt.Errorf("open titles cache: %s", err)
}
defer f.Close()
c := &TitlesCache{
Path: path,
}
if err := gob.NewDecoder(f).Decode(&c.Titles); err != nil {
return nil, fmt.Errorf("open titles cache %s: %s", path, err)
}
return c, nil
}
// GetTitles gets titles from the cache.
// If the cache has not been populated yet, downloads titles from AniDB.
func (c *TitlesCache) GetTitles() ([]AnimeT, error) {
if len(c.Titles) > 0 {
return c.Titles, nil
}
return c.GetFreshTitles()
}
// GetFreshTitles downloads titles from AniDB and stores it in the cache.
// See AniDB API documentation about rate limits.
func (c *TitlesCache) GetFreshTitles() ([]AnimeT, error) {
t, err := RequestTitles()
if err != nil {
return nil, err
}
c.Titles = t
c.Updated = true
return t, nil
}
// Save saves the cached titles to the cache file.
// This method sets Updated to false if successful.
// See also the SaveIfUpdated method, which is probably more useful.
func (c *TitlesCache) Save() error {
if err := os.MkdirAll(filepath.Dir(c.Path), 0777); err != nil {
return fmt.Errorf("save titles cache: %s", err)
}
f, err := os.Create(c.Path)
if err != nil {
return fmt.Errorf("save titles cache: %s", err)
}
defer f.Close()
if err := gob.NewEncoder(f).Encode(c.Titles); err != nil {
return fmt.Errorf("save titles cache %s: %s", c.Path, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("save titles cache: %s", err)
}
c.Updated = false
return nil
}
// SaveIfUpdated saves the cached titles to the cache file if they
// have been updated.
// This method sets Updated to false if successful.
func (c *TitlesCache) SaveIfUpdated() error {
if !c.Updated {
return nil
}
return c.Save()
}
func defaultTitlesCacheFile() string {
return filepath.Join(cacheDir(), xdgName, "titles.gob")
}
func cacheDir() string {
if p := os.Getenv("XDG_CACHE_HOME"); p != "" {
return p
}
return filepath.Join(os.Getenv("HOME"), ".cache")
}