|
1 | 1 | package base |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "github.com/Xhofe/alist/conf" |
6 | 5 | "github.com/Xhofe/alist/model" |
7 | 6 | "github.com/Xhofe/alist/utils" |
8 | 7 | log "github.com/sirupsen/logrus" |
9 | 8 | ) |
10 | 9 |
|
11 | 10 | func KeyCache(path string, account *model.Account) string { |
12 | | - path = utils.ParsePath(path) |
13 | | - return fmt.Sprintf("%s%s", account.Name, path) |
| 11 | + //path = utils.ParsePath(path) |
| 12 | + key := utils.ParsePath(utils.Join(account.Name, path)) |
| 13 | + log.Debugln("cache key: ", key) |
| 14 | + return key |
14 | 15 | } |
15 | 16 |
|
16 | | -func SetCache(path string, obj interface{}, account *model.Account) error { |
17 | | - return conf.Cache.Set(conf.Ctx, KeyCache(path, account), obj, nil) |
| 17 | +func SaveSearchFiles[T model.ISearchFile](key string, obj []T) { |
| 18 | + err := model.DeleteSearchFilesByPath(key) |
| 19 | + if err != nil { |
| 20 | + log.Errorln("failed create search files", err) |
| 21 | + return |
| 22 | + } |
| 23 | + files := make([]model.SearchFile, len(obj)) |
| 24 | + for i := 0; i < len(obj); i++ { |
| 25 | + files[i] = model.SearchFile{ |
| 26 | + Path: key, |
| 27 | + Name: obj[i].GetName(), |
| 28 | + Size: obj[i].GetSize(), |
| 29 | + Type: obj[i].GetType(), |
| 30 | + } |
| 31 | + } |
| 32 | + err = model.CreateSearchFiles(files) |
| 33 | + if err != nil { |
| 34 | + log.Errorln("failed create search files", err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func SetCache[T model.ISearchFile](path string, obj []T, account *model.Account) error { |
| 39 | + key := KeyCache(path, account) |
| 40 | + go SaveSearchFiles(key, obj) |
| 41 | + return conf.Cache.Set(conf.Ctx, key, obj, nil) |
18 | 42 | } |
19 | 43 |
|
20 | 44 | func GetCache(path string, account *model.Account) (interface{}, error) { |
|
0 commit comments