[chore] update database caching library (#1040)

* convert most of the caches to use result.Cache{}

* add caching of emojis

* fix issues causing failing tests

* update go-cache/v2 instances with v3

* fix getnotification

* add a note about the left-in StatusCreate comment

* update EmojiCategory db access to use new result.Cache{}

* fix possible panic in getstatusparents

* further proof that kim is not stinky
This commit is contained in:
kim 2022-11-15 18:45:15 +00:00 committed by GitHub
commit 8598dea98b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 725 additions and 2289 deletions

View file

@ -20,11 +20,11 @@ package bundb
import (
"context"
"database/sql"
"net/url"
"strings"
"time"
"github.com/superseriousbusiness/gotosocial/internal/cache"
"codeberg.org/gruf/go-cache/v3/result"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -34,7 +34,22 @@ import (
type domainDB struct {
conn *DBConn
cache *cache.DomainBlockCache
cache *result.Cache[*gtsmodel.DomainBlock]
}
func (d *domainDB) init() {
// Initialize domain block result cache
d.cache = result.NewSized([]result.Lookup{
{Name: "Domain"},
}, func(d1 *gtsmodel.DomainBlock) *gtsmodel.DomainBlock {
d2 := new(gtsmodel.DomainBlock)
*d2 = *d1
return d2
}, 1000)
// Set cache TTL and start sweep routine
d.cache.SetTTL(time.Minute*5, false)
d.cache.Start(time.Second * 10)
}
// normalizeDomain converts the given domain to lowercase
@ -49,76 +64,53 @@ func normalizeDomain(domain string) (out string, err error) {
}
func (d *domainDB) CreateDomainBlock(ctx context.Context, block *gtsmodel.DomainBlock) db.Error {
domain, err := normalizeDomain(block.Domain)
var err error
block.Domain, err = normalizeDomain(block.Domain)
if err != nil {
return err
}
block.Domain = domain
// Attempt to insert new domain block
if _, err := d.conn.NewInsert().
Model(block).
Exec(ctx); err != nil {
return d.cache.Store(block, func() error {
_, err := d.conn.NewInsert().
Model(block).
Exec(ctx)
return d.conn.ProcessError(err)
}
// Cache this domain block
d.cache.Put(block.Domain, block)
return nil
})
}
func (d *domainDB) GetDomainBlock(ctx context.Context, domain string) (*gtsmodel.DomainBlock, db.Error) {
var err error
domain, err = normalizeDomain(domain)
if err != nil {
return nil, err
}
// Check for easy case, domain referencing *us*
if domain == "" || domain == config.GetAccountDomain() {
return nil, db.ErrNoEntries
}
// Check for already cached rblock
if block, ok := d.cache.GetByDomain(domain); ok {
// A 'nil' return value is a sentinel value for no block
if block == nil {
return d.cache.Load("Domain", func() (*gtsmodel.DomainBlock, error) {
// Check for easy case, domain referencing *us*
if domain == "" || domain == config.GetAccountDomain() {
return nil, db.ErrNoEntries
}
// Else, this block exists
return block, nil
}
var block gtsmodel.DomainBlock
block := &gtsmodel.DomainBlock{}
q := d.conn.
NewSelect().
Model(&block).
Where("? = ?", bun.Ident("domain_block.domain"), domain).
Limit(1)
if err := q.Scan(ctx); err != nil {
return nil, d.conn.ProcessError(err)
}
q := d.conn.
NewSelect().
Model(block).
Where("? = ?", bun.Ident("domain_block.domain"), domain).
Limit(1)
// Query database for domain block
switch err := q.Scan(ctx); err {
// No error, block found
case nil:
d.cache.Put(domain, block)
return block, nil
// No error, simply not found
case sql.ErrNoRows:
d.cache.Put(domain, nil)
return nil, db.ErrNoEntries
// Any other db error
default:
return nil, d.conn.ProcessError(err)
}
return &block, nil
}, domain)
}
func (d *domainDB) DeleteDomainBlock(ctx context.Context, domain string) db.Error {
var err error
domain, err = normalizeDomain(domain)
if err != nil {
return err
@ -133,7 +125,7 @@ func (d *domainDB) DeleteDomainBlock(ctx context.Context, domain string) db.Erro
}
// Clear domain from cache
d.cache.InvalidateByDomain(domain)
d.cache.Invalidate(domain)
return nil
}