Skip to content

Commit

Permalink
added automatic cache cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pineapple217 committed May 6, 2024
1 parent d658912 commit 18e9d97
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"log/slog"
"os"
"os/signal"
"time"

"github.com/Pineapple217/mb/pkg/config"
"github.com/Pineapple217/mb/pkg/database"
"github.com/Pineapple217/mb/pkg/handler"
"github.com/Pineapple217/mb/pkg/media"
"github.com/Pineapple217/mb/pkg/scheduler"
"github.com/Pineapple217/mb/pkg/server"
)

Expand All @@ -19,7 +21,7 @@ const banner = `
·██ ▐███▪▐█ ▀█▪
▐█ ▌▐▌▐█·▐█▀▀█▄
██ ██▌▐█▌██▄▪▐█
▀▀ █▪▀▀▀·▀▀▀▀ v0.7.0
▀▀ █▪▀▀▀·▀▀▀▀ v0.7.1
Minimal blog with no JavaScript
https://github.com/Pineapple217/mb/pkg
-----------------------------------------------------------------------------`
Expand All @@ -42,11 +44,17 @@ func main() {

server.Start()

s := scheduler.NewScheduler()
s.Schedule(time.Hour*24, func() {
scheduler.CleanCache(q)
})

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
slog.Info("Received an interrupt signal, exiting...")

s.Stop()
server.Stop()
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/database/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ RETURNING *;
SELECT * FROM youtube_cache
WHERE yt_id = ? LIMIT 1;

-- name: RemoveUnusedYoutubeCache :execrows
DELETE FROM youtube_cache
WHERE id IN (
SELECT youtube_cache.id
FROM youtube_cache
LEFT JOIN posts ON instr(posts.content, youtube_cache.yt_id) > 0
WHERE instr(posts.content, youtube_cache.yt_id) IS NULL
);

-- name: RemoveUnusedSpotifyCache :execrows
DELETE FROM spotify_cache
WHERE id IN (
SELECT spotify_cache.id
FROM spotify_cache
LEFT JOIN posts ON instr(posts.content, spotify_cache.track_id) > 0
WHERE instr(posts.content, spotify_cache.track_id) IS NULL
);

-- name: GetAllTags :many
WITH split (
Expand Down
36 changes: 36 additions & 0 deletions pkg/database/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions pkg/scheduler/clean_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scheduler

import (
"context"
"log/slog"
"time"

"github.com/Pineapple217/mb/pkg/database"
)

func CleanCache(q *database.Queries) {
slog.Info("Starting clean databases chaches")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

c, err := q.RemoveUnusedYoutubeCache(ctx)
if err != nil {
slog.Warn("Failed to clean database cache", "type", "youtube", "error", err)
}
slog.Info("Cleaned cache", "type", "youtube", "count", c)

c, err = q.RemoveUnusedSpotifyCache(ctx)
if err != nil {
slog.Warn("Failed to clean database cache", "type", "spotify", "error", err)
}
slog.Info("Cleaned cache", "type", "spotify", "count", c)
}
43 changes: 43 additions & 0 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scheduler

import (
"sync"
"time"
)

type Scheduler struct {
wg *sync.WaitGroup
doneCh chan bool
}

func NewScheduler() Scheduler {
var wg sync.WaitGroup
done := make(chan bool)
return Scheduler{
wg: &wg,
doneCh: done,
}
}

func (s Scheduler) Stop() {
close(s.doneCh)
s.wg.Wait()
}

func (s Scheduler) Schedule(t time.Duration, f func()) {
s.wg.Add(1)
ticker := time.NewTicker(t)
go f()
go func() {
for {
select {
case <-ticker.C:
f()
case <-s.doneCh:
ticker.Stop()
s.wg.Done()
return
}
}
}()
}

0 comments on commit 18e9d97

Please sign in to comment.