Add preview card storing to the database

This commit is contained in:
kaimanhub 2025-03-30 20:24:15 +03:00
commit 6169351d22
12 changed files with 228 additions and 55 deletions

View file

@ -70,6 +70,7 @@ func (c *Caches) Init() {
c.initBlock()
c.initBlockIDs()
c.initBoostOfIDs()
c.initCard()
c.initConversation()
c.initConversationLastStatusIDs()
c.initDomainAllow()

29
internal/cache/db.go vendored
View file

@ -52,6 +52,9 @@ type DBCaches struct {
// BoostOfIDs provides access to the boost of IDs list database cache.
BoostOfIDs SliceCache[string]
// Card provides access to the gtsmodel Card database cache.
Card StructCache[*gtsmodel.Card]
// Conversation provides access to the gtsmodel Conversation database cache.
Conversation StructCache[*gtsmodel.Conversation]
@ -637,6 +640,32 @@ func (c *Caches) initEmoji() {
})
}
func (c *Caches) initCard() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(
sizeofCard(), // model in-mem size.
config.GetCacheEmojiMemRatio(), // TODO: this need to be replaced with GetCacheCardMemRatio
)
log.Infof(nil, "cache size = %d", cap)
copyF := func(e1 *gtsmodel.Card) *gtsmodel.Card {
e2 := new(gtsmodel.Card)
*e2 = *e1
return e2
}
c.DB.Card.Init(structr.CacheConfig[*gtsmodel.Card]{
Indices: []structr.IndexConfig{
{Fields: "ID"},
},
MaxSize: cap,
IgnoreErr: ignoreErrors,
Copy: copyF,
})
}
func (c *Caches) initEmojiCategory() {
// Calculate maximum cache size.
cap := calculateResultCacheMax(

View file

@ -395,6 +395,13 @@ func sizeofEmoji() uintptr {
}))
}
func sizeofCard() uintptr {
// TODO: this implementation need to be extended to contain other fields also.
return uintptr(size.Of(&gtsmodel.Card{
ID: exampleID,
}))
}
func sizeofEmojiCategory() uintptr {
return uintptr(size.Of(&gtsmodel.EmojiCategory{
ID: exampleID,