-
Notifications
You must be signed in to change notification settings - Fork 0
/
filecache.go
46 lines (38 loc) · 839 Bytes
/
filecache.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
package service
import (
"os"
"path/filepath"
"sort"
"github.com/zjyl1994/b2webp/common/utils"
"github.com/zjyl1994/b2webp/common/vars"
)
var FileCacheService fileCacheService
type fileCacheService struct{}
func (s fileCacheService) GetPath(fileName string) string {
return filepath.Join(vars.CacheDir, fileName)
}
func (s fileCacheService) Clean() error {
list, err := utils.ScanFileInDir(vars.CacheDir)
if err != nil {
return err
}
sort.Slice(list, func(i, j int) bool { return list[i].Mtime < list[j].Mtime })
var totalCount int64
for _, v := range list {
totalCount += v.Size
}
if vars.MaxCacheSize > totalCount {
return nil
}
for _, v := range list {
totalCount -= v.Size
err = os.Remove(v.Path)
if err != nil {
return err
}
if vars.MaxCacheSize > totalCount {
break
}
}
return nil
}