🐛 Fix cache issue
This commit is contained in:
parent
b041ce3e06
commit
e89f149336
@ -1,11 +1,18 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
"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/datatypes"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -70,6 +77,19 @@ type Attachment struct {
|
|||||||
IsMature bool `json:"is_mature" gorm:"-"`
|
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
|
// Data model for in progress multipart attachments
|
||||||
type AttachmentFragment struct {
|
type AttachmentFragment struct {
|
||||||
cruda.BaseModel
|
cruda.BaseModel
|
||||||
|
@ -86,7 +86,10 @@ func listAttachment(c *fiber.Ctx) error {
|
|||||||
var out []models.Attachment
|
var out []models.Attachment
|
||||||
if err := tx.
|
if err := tx.
|
||||||
Offset(offset).Limit(take).
|
Offset(offset).Limit(take).
|
||||||
Preload("Thumbnail").Preload("Compressed").
|
Preload("Pool").
|
||||||
|
Preload("Thumbnail").
|
||||||
|
Preload("Compressed").
|
||||||
|
Preload("Boosts").
|
||||||
Find(&out).Error; err != nil {
|
Find(&out).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
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 {
|
if err := database.C.Save(&attachment).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
services.CacheAttachment(attachment)
|
|
||||||
|
|
||||||
if !c.QueryBool("analyzeNow", false) {
|
if !c.QueryBool("analyzeNow", false) {
|
||||||
services.AnalyzeAttachment(attachment)
|
services.AnalyzeAttachment(attachment)
|
||||||
|
@ -226,7 +226,6 @@ func AnalyzeAttachment(file models.Attachment) error {
|
|||||||
if linked && err != nil {
|
if linked && err != nil {
|
||||||
return fmt.Errorf("unable to link file record: %v", err)
|
return fmt.Errorf("unable to link file record: %v", err)
|
||||||
} else if !linked {
|
} else if !linked {
|
||||||
CacheAttachment(file)
|
|
||||||
if err := tx.Save(&file).Error; err != nil {
|
if err := tx.Save(&file).Error; err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return fmt.Errorf("unable to save file record: %v", err)
|
return fmt.Errorf("unable to save file record: %v", err)
|
||||||
|
@ -25,6 +25,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetAttachmentCacheKey(rid string) any {
|
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)
|
return fmt.Sprintf("attachment#%s", rid)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,10 +105,10 @@ func GetAttachmentCache(rid string) (models.Attachment, bool) {
|
|||||||
func CacheAttachment(item models.Attachment) {
|
func CacheAttachment(item models.Attachment) {
|
||||||
cacheManager := cache.New[any](localCache.S)
|
cacheManager := cache.New[any](localCache.S)
|
||||||
marshal := marshaler.New(cacheManager)
|
marshal := marshaler.New(cacheManager)
|
||||||
contx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
_ = marshal.Set(
|
_ = marshal.Set(
|
||||||
contx,
|
ctx,
|
||||||
GetAttachmentCacheKey(item.Rid),
|
GetAttachmentCacheKey(item.Rid),
|
||||||
item,
|
item,
|
||||||
store.WithExpiration(60*time.Minute),
|
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 {
|
if err := tx.Save(&attachment).Error; err != nil {
|
||||||
return attachment, fmt.Errorf("failed to save attachment record: %v", err)
|
return attachment, fmt.Errorf("failed to save attachment record: %v", err)
|
||||||
} else {
|
|
||||||
CacheAttachment(attachment)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return attachment, nil
|
return attachment, nil
|
||||||
@ -183,17 +183,12 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er
|
|||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
CacheAttachment(prev)
|
|
||||||
CacheAttachment(og)
|
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
|
func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
|
||||||
if err := database.C.Save(&item).Error; err != nil {
|
if err := database.C.Save(&item).Error; err != nil {
|
||||||
return item, err
|
return item, err
|
||||||
} else {
|
|
||||||
CacheAttachment(item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return item, nil
|
return item, nil
|
||||||
|
@ -86,7 +86,6 @@ func ReUploadFile(meta models.Attachment, dst int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
database.C.Save(&meta)
|
database.C.Save(&meta)
|
||||||
CacheAttachment(meta)
|
|
||||||
cleanupDst()
|
cleanupDst()
|
||||||
return nil
|
return nil
|
||||||
case models.DestinationTypeS3:
|
case models.DestinationTypeS3:
|
||||||
@ -111,7 +110,6 @@ func ReUploadFile(meta models.Attachment, dst int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
database.C.Save(&meta)
|
database.C.Save(&meta)
|
||||||
CacheAttachment(meta)
|
|
||||||
cleanupDst()
|
cleanupDst()
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user