From e89f1493369fd9a87ca33e5e357a6f346a1e3ea7 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 29 Dec 2024 01:03:22 +0800 Subject: [PATCH] :bug: Fix cache issue --- pkg/internal/models/attachments.go | 20 ++++++++++++++++++++ pkg/internal/server/api/index_api.go | 5 ++++- pkg/internal/server/api/up_multipart_api.go | 1 - pkg/internal/services/analyzer.go | 1 - pkg/internal/services/attachments.go | 13 ++++--------- pkg/internal/services/uploader.go | 2 -- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/pkg/internal/models/attachments.go b/pkg/internal/models/attachments.go index 194cf35..23fee8b 100644 --- a/pkg/internal/models/attachments.go +++ b/pkg/internal/models/attachments.go @@ -1,11 +1,18 @@ package models import ( + "context" + "fmt" "time" "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" + "github.com/eko/gocache/lib/v4/cache" + "github.com/eko/gocache/lib/v4/marshaler" + + localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache" "gorm.io/datatypes" + "gorm.io/gorm" ) const ( @@ -70,6 +77,19 @@ type Attachment struct { IsMature bool `json:"is_mature" gorm:"-"` } +func (v *Attachment) AfterUpdate(tx *gorm.DB) error { + cacheManager := cache.New[any](localCache.S) + marshal := marshaler.New(cacheManager) + ctx := context.Background() + + _ = marshal.Delete( + ctx, + fmt.Sprintf("attachment#%s", v.Rid), + ) + + return nil +} + // Data model for in progress multipart attachments type AttachmentFragment struct { cruda.BaseModel diff --git a/pkg/internal/server/api/index_api.go b/pkg/internal/server/api/index_api.go index 18655b3..7c62036 100644 --- a/pkg/internal/server/api/index_api.go +++ b/pkg/internal/server/api/index_api.go @@ -86,7 +86,10 @@ func listAttachment(c *fiber.Ctx) error { var out []models.Attachment if err := tx. Offset(offset).Limit(take). - Preload("Thumbnail").Preload("Compressed"). + Preload("Pool"). + Preload("Thumbnail"). + Preload("Compressed"). + Preload("Boosts"). Find(&out).Error; err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/internal/server/api/up_multipart_api.go b/pkg/internal/server/api/up_multipart_api.go index ce2b902..819e0a9 100644 --- a/pkg/internal/server/api/up_multipart_api.go +++ b/pkg/internal/server/api/up_multipart_api.go @@ -129,7 +129,6 @@ func uploadFragmentChunk(c *fiber.Ctx) error { if err := database.C.Save(&attachment).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - services.CacheAttachment(attachment) if !c.QueryBool("analyzeNow", false) { services.AnalyzeAttachment(attachment) diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index ef25360..de375c5 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -226,7 +226,6 @@ func AnalyzeAttachment(file models.Attachment) error { if linked && err != nil { return fmt.Errorf("unable to link file record: %v", err) } else if !linked { - CacheAttachment(file) if err := tx.Save(&file).Error; err != nil { tx.Rollback() return fmt.Errorf("unable to save file record: %v", err) diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index 1739a74..604c72f 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -25,6 +25,8 @@ import ( ) func GetAttachmentCacheKey(rid string) any { + // Reminder: when you update this, update it in models/attachments too + // It cannot be imported here due to cycle import return fmt.Sprintf("attachment#%s", rid) } @@ -103,10 +105,10 @@ func GetAttachmentCache(rid string) (models.Attachment, bool) { func CacheAttachment(item models.Attachment) { cacheManager := cache.New[any](localCache.S) marshal := marshaler.New(cacheManager) - contx := context.Background() + ctx := context.Background() _ = marshal.Set( - contx, + ctx, GetAttachmentCacheKey(item.Rid), item, store.WithExpiration(60*time.Minute), @@ -146,8 +148,6 @@ func NewAttachmentMetadata(tx *gorm.DB, user *sec.UserInfo, file *multipart.File if err := tx.Save(&attachment).Error; err != nil { return attachment, fmt.Errorf("failed to save attachment record: %v", err) - } else { - CacheAttachment(attachment) } return attachment, nil @@ -183,17 +183,12 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er return true, err } - CacheAttachment(prev) - CacheAttachment(og) - return true, nil } func UpdateAttachment(item models.Attachment) (models.Attachment, error) { if err := database.C.Save(&item).Error; err != nil { return item, err - } else { - CacheAttachment(item) } return item, nil diff --git a/pkg/internal/services/uploader.go b/pkg/internal/services/uploader.go index f3a27d7..aab5b1f 100644 --- a/pkg/internal/services/uploader.go +++ b/pkg/internal/services/uploader.go @@ -86,7 +86,6 @@ func ReUploadFile(meta models.Attachment, dst int) error { } database.C.Save(&meta) - CacheAttachment(meta) cleanupDst() return nil case models.DestinationTypeS3: @@ -111,7 +110,6 @@ func ReUploadFile(meta models.Attachment, dst int) error { } database.C.Save(&meta) - CacheAttachment(meta) cleanupDst() return nil default: