mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[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:
70
internal/cache/db.go
vendored
70
internal/cache/db.go
vendored
@@ -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(
|
||||
|
Reference in New Issue
Block a user