diff options
Diffstat (limited to 'internal/db/bundb/bundb.go')
-rw-r--r-- | internal/db/bundb/bundb.go | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 6328ca34f..d92318afd 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -31,7 +31,6 @@ import ( "strings" "time" - "github.com/ReneKroon/ttlcache" "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/stdlib" "github.com/sirupsen/logrus" @@ -46,6 +45,7 @@ import ( "github.com/uptrace/bun/dialect/sqlitedialect" "github.com/uptrace/bun/migrate" + grufcache "codeberg.org/gruf/go-cache/v2" "modernc.org/sqlite" ) @@ -136,11 +136,8 @@ func NewBunDBService(ctx context.Context) (db.DB, error) { return nil, fmt.Errorf("database type %s not supported for bundb", dbType) } - // add a hook to log queries and the time they take - // only do this for logging where performance isn't 1st concern - if logrus.GetLevel() >= logrus.DebugLevel && config.GetLogDbQueries() { - conn.DB.AddQueryHook(newDebugQueryHook()) - } + // Add database query hook + conn.DB.AddQueryHook(queryHook{}) // table registration is needed for many-to-many, see: // https://bun.uptrace.dev/orm/many-to-many-relation/ @@ -155,7 +152,27 @@ func NewBunDBService(ctx context.Context) (db.DB, error) { return nil, fmt.Errorf("db migration error: %s", err) } + // Create DB structs that require ptrs to each other accounts := &accountDB{conn: conn, cache: cache.NewAccountCache()} + status := &statusDB{conn: conn, cache: cache.NewStatusCache()} + timeline := &timelineDB{conn: conn} + + // Setup DB cross-referencing + accounts.status = status + status.accounts = accounts + timeline.status = status + + // Prepare mentions cache + // TODO: move into internal/cache + mentionCache := grufcache.New[string, *gtsmodel.Mention]() + mentionCache.SetTTL(time.Minute*5, false) + mentionCache.Start(time.Second * 10) + + // Prepare notifications cache + // TODO: move into internal/cache + notifCache := grufcache.New[string, *gtsmodel.Notification]() + notifCache.SetTTL(time.Minute*5, false) + notifCache.Start(time.Second * 10) ps := &bunDBService{ Account: accounts, @@ -179,11 +196,11 @@ func NewBunDBService(ctx context.Context) (db.DB, error) { }, Mention: &mentionDB{ conn: conn, - cache: ttlcache.NewCache(), + cache: mentionCache, }, Notification: ¬ificationDB{ conn: conn, - cache: ttlcache.NewCache(), + cache: notifCache, }, Relationship: &relationshipDB{ conn: conn, @@ -191,15 +208,9 @@ func NewBunDBService(ctx context.Context) (db.DB, error) { Session: &sessionDB{ conn: conn, }, - Status: &statusDB{ - conn: conn, - cache: cache.NewStatusCache(), - accounts: accounts, - }, - Timeline: &timelineDB{ - conn: conn, - }, - conn: conn, + Status: status, + Timeline: timeline, + conn: conn, } // we can confidently return this useable service now |