Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simple query caching #394

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/go-gorm/caches/v4 v4.0.5 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/go-gorm/caches/v4 v4.0.5 h1:Sdj9vxbEM0sCmv5+s5o6GzoVMuraWF0bjJJvUU+7c1U=
github.com/go-gorm/caches/v4 v4.0.5/go.mod h1:Ms8LnWVoW4GkTofpDzFH8OfDGNTjLxQDyxBmRN67Ujw=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
Expand Down
4 changes: 4 additions & 0 deletions pkg/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func Connect(driver, dsn string, debug bool, logger *slog.Logger) (*gorm.DB, err
return nil, err
}

if err := db.Use(NewMemoryCache()); err != nil {
return nil, err
}

if err := preMigrationActions(db); err != nil {
return nil, err
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/database/gorm_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package database

import (
"context"
"errors"
"fmt"
"sync"

"github.com/go-gorm/caches/v4"
)

var ErrInvalidDataInCache = errors.New("invalid cache data")

type memoryCacher struct {
store *sync.Map
}

func (c *memoryCacher) init() {
if c.store == nil {
c.store = &sync.Map{}
}
}

func (c *memoryCacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) {
val, ok := c.store.Load(key)
if !ok {
// Not stored in cache
return nil, nil //nolint:nilnil
}

b, ok := val.([]byte)
if !ok {
return nil, ErrInvalidDataInCache // invalid data stored in cache
}

if err := q.Unmarshal(b); err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidDataInCache, err) // invalid data stored in cache
}

return q, nil
}

func (c *memoryCacher) Store(ctx context.Context, key string, val *caches.Query[any]) error {
res, err := val.Marshal()
if err != nil {
return err
}

c.store.Store(key, res)

return nil
}

func (c *memoryCacher) Invalidate(ctx context.Context) error {
c.store = &sync.Map{}
return nil
}

func NewMemoryCache() *caches.Caches {
c := &memoryCacher{}
c.init()

cachesPlugin := &caches.Caches{Conf: &caches.Config{
Cacher: c,
Easer: true,
}}

return cachesPlugin
}
1 change: 1 addition & 0 deletions vendor/github.com/go-gorm/caches/v4/.gitignore

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

21 changes: 21 additions & 0 deletions vendor/github.com/go-gorm/caches/v4/LICENSE

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

Loading
Loading