]> git.lizzy.rs Git - go-anidb.git/blob - cache.go
Modernize
[go-anidb.git] / cache.go
1 package anidb
2
3 import (
4         "github.com/EliasFleckenstein03/go-fscache"
5         "os"
6         "path"
7         "time"
8 )
9
10 func init() {
11         c, err := fscache.NewCacheDir(path.Join(os.TempDir(), "anidb", "cache"))
12         if err != nil {
13                 panic(err)
14         }
15         Cache = *c
16
17         RefreshTitles()
18 }
19
20 var Cache fscache.CacheDir
21
22 type cacheable interface {
23         setCachedTS(time.Time)
24 }
25
26 func CacheSet(v interface{}, key ...fscache.CacheKey) (err error) {
27         now := time.Now()
28         _, err = Cache.Set(v, key...)
29         if err != nil {
30                 return err
31         }
32         switch t := v.(type) {
33         case cacheable:
34                 t.setCachedTS(now)
35         }
36         return
37 }
38
39 func CacheGet(v interface{}, key ...fscache.CacheKey) (err error) {
40         ts, err := Cache.Get(v, key...)
41         if err != nil {
42                 return err
43         }
44         switch t := v.(type) {
45         case cacheable:
46                 t.setCachedTS(ts)
47         }
48         return
49 }