Skip to content

Commit f87014e

Browse files
committed
Implement cache cleanout
1 parent eaedbda commit f87014e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

data/data.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ package data
99
import (
1010
"fmt"
1111
"net/url"
12+
"os"
1213
"time"
1314
)
1415

1516
const (
1617
// QueueReloadInterval is how often the queue will be reloaded
1718
QueueReloadInterval = time.Duration(15) * time.Second
19+
// EpisodeCacheTime is how long an episode is allowed to stay in cache in seconds
20+
// Default value is three days (3 * 24 * 60 * 60)
21+
EpisodeCacheTime = 259200
1822
)
1923

2024
// Dependent data structures
@@ -55,6 +59,7 @@ func InitData() error {
5559
// First ensures we have hot-reloaded any required data
5660
func SaveData() {
5761
ReloadData()
62+
CleanData()
5863

5964
Q.Save()
6065
DB.Save()
@@ -69,6 +74,34 @@ func ReloadData() {
6974
Q.Reload()
7075
}
7176

77+
// CleanData cleans out the cache based on items which are both finished/played
78+
// and with a last listen time of more than EpisodeCacheTime seconds ago
79+
// (defaults to three days). Removed episodes are set to "pending" status (to
80+
// be downloaded) and have their cache file removed
81+
func CleanData() {
82+
now := time.Now().Unix()
83+
count := 0
84+
85+
Q.Range(func(i int, item *QueueItem) bool {
86+
if item.Date == -1 || (item.State != StatePlayed && item.State != StateFinished) {
87+
return true
88+
}
89+
90+
diff := now - item.Date
91+
if diff >= EpisodeCacheTime {
92+
item.Date = -1
93+
item.State = StatePending
94+
95+
os.Remove(item.Path)
96+
count++
97+
}
98+
99+
return true
100+
})
101+
102+
fmt.Printf("Cache cleanup...done (removed %d items)\n", count)
103+
}
104+
72105
// ReloadLoop is an infinite loop to continually reload the
73106
// file on disk into memory.
74107
//

0 commit comments

Comments
 (0)