[performance] cached oauth database types (#2838)

* update token + client code to use struct caches

* add code comments

* slight tweak to default mem ratios

* fix envparsing

* add appropriate invalidate hooks

* update the tokenstore sweeping function to rely on caches

* update to use PutClient()

* add ClientID to list of token struct indices
This commit is contained in:
kim
2024-04-15 14:22:21 +01:00
committed by GitHub
parent 8b30709791
commit f79d50b9b2
18 changed files with 428 additions and 67 deletions

70
internal/cache/db.go vendored
View File

@@ -58,6 +58,9 @@ type GTSCaches struct {
// BoostOfIDs provides access to the boost of IDs list database cache.
BoostOfIDs SliceCache[string]
// Client provides access to the gtsmodel Client database cache.
Client StructCache[*gtsmodel.Client]
// DomainAllow provides access to the domain allow database cache.
DomainAllow *domain.Cache
@@ -150,6 +153,9 @@ type GTSCaches struct {
// Tag provides access to the gtsmodel Tag database cache.
Tag StructCache[*gtsmodel.Tag]
// Token provides access to the gtsmodel Token database cache.
Token StructCache[*gtsmodel.Token]
// Tombstone provides access to the gtsmodel Tombstone database cache.
Tombstone StructCache[*gtsmodel.Tombstone]
@@ -309,9 +315,10 @@ func (c *Caches) initApplication() {
{Fields: "ID"},
{Fields: "ClientID"},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
Invalidate: c.OnInvalidateApplication,
})
}
@@ -374,6 +381,32 @@ func (c *Caches) initBoostOfIDs() {
c.GTS.BoostOfIDs.Init(0, cap)
}
func (c *Caches) initClient() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofClient(), // model in-mem size.
config.GetCacheClientMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
copyF := func(c1 *gtsmodel.Client) *gtsmodel.Client {
c2 := new(gtsmodel.Client)
*c2 = *c1
return c2
}
c.GTS.Client.Init(structr.CacheConfig[*gtsmodel.Client]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
Invalidate: c.OnInvalidateClient,
})
}
func (c *Caches) initDomainAllow() {
c.GTS.DomainAllow = new(domain.Cache)
}
@@ -1135,7 +1168,7 @@ func (c *Caches) initTag() {
func (c *Caches) initThreadMute() {
cap := calculateResultCacheMax(
sizeOfThreadMute(), // model in-mem size.
sizeofThreadMute(), // model in-mem size.
config.GetCacheThreadMuteMemRatio(),
)
@@ -1160,6 +1193,35 @@ func (c *Caches) initThreadMute() {
})
}
func (c *Caches) initToken() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofToken(), // model in-mem size.
config.GetCacheTokenMemRatio(),
)
log.Infof(nil, "cache size = %d", cap)
copyF := func(t1 *gtsmodel.Token) *gtsmodel.Token {
t2 := new(gtsmodel.Token)
*t2 = *t1
return t2
}
c.GTS.Token.Init(structr.CacheConfig[*gtsmodel.Token]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
{Fields: "Code"},
{Fields: "Access"},
{Fields: "Refresh"},
{Fields: "ClientID", Multiple: true},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
}
func (c *Caches) initTombstone() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(