feat: use sqlite for jikan api cache with hourly cleanup
This commit is contained in:
@@ -29,6 +29,9 @@ func (w *Worker) Start(ctx context.Context) {
|
||||
|
||||
// Run once immediately
|
||||
w.syncRelations(ctx)
|
||||
w.cleanupCache(ctx)
|
||||
|
||||
cleanupCounter := 0
|
||||
|
||||
for {
|
||||
select {
|
||||
@@ -36,10 +39,24 @@ func (w *Worker) Start(ctx context.Context) {
|
||||
return
|
||||
case <-ticker.C:
|
||||
w.syncRelations(ctx)
|
||||
|
||||
// Clean up cache every 60 runs (approx 1 hour)
|
||||
cleanupCounter++
|
||||
if cleanupCounter >= 60 {
|
||||
w.cleanupCache(ctx)
|
||||
cleanupCounter = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) cleanupCache(ctx context.Context) {
|
||||
err := w.db.DeleteExpiredJikanCache(ctx)
|
||||
if err != nil {
|
||||
log.Printf("worker: failed to clean up expired jikan cache: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) syncRelations(ctx context.Context) {
|
||||
// Find up to 20 anime that need their relations synced
|
||||
animes, err := w.db.GetAnimeNeedingRelationSync(ctx)
|
||||
|
||||
Reference in New Issue
Block a user