🐛 Fix cache issue
This commit is contained in:
parent
b041ce3e06
commit
e89f149336
@ -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
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user