♻️ Rebuilt cache with nexus cache

This commit is contained in:
2025-03-29 15:48:04 +08:00
parent 1390f26afa
commit 073b32aa73
7 changed files with 99 additions and 574 deletions

View File

@ -1,7 +1,6 @@
package services
import (
"context"
"fmt"
"mime"
"mime/multipart"
@ -9,25 +8,20 @@ import (
"path/filepath"
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/fs"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"github.com/google/uuid"
"gorm.io/gorm"
)
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)
func KgAttachmentCache(rid string) string {
return cachekit.FKey(cachekit.DAAttachment, rid)
}
func GetAttachmentByID(id uint) (models.Attachment, error) {
@ -48,16 +42,11 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
}
func GetAttachmentByRID(rid string) (models.Attachment, error) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(
contx,
GetAttachmentCacheKey(rid),
new(models.Attachment),
if val, err := cachekit.Get[models.Attachment](
gap.Ca,
KgAttachmentCache(rid),
); err == nil {
return *val.(*models.Attachment), nil
return val, nil
}
var attachment models.Attachment
@ -88,31 +77,22 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
}
func GetAttachmentCache(rid string) (models.Attachment, bool) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(
contx,
GetAttachmentCacheKey(rid),
new(models.Attachment),
if val, err := cachekit.Get[models.Attachment](
gap.Ca,
KgAttachmentCache(rid),
); err == nil {
return *val.(*models.Attachment), true
return val, true
}
return models.Attachment{}, false
}
func CacheAttachment(item models.Attachment) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
ctx := context.Background()
_ = marshal.Set(
ctx,
GetAttachmentCacheKey(item.Rid),
cachekit.Set[models.Attachment](
gap.Ca,
KgAttachmentCache(item.Rid),
item,
store.WithExpiration(60*time.Minute),
store.WithTags([]string{"attachment", fmt.Sprintf("user#%d", item.AccountID)}),
60*time.Minute,
cachekit.FKey("attachment", item.Rid),
)
}
@ -224,10 +204,7 @@ func DeleteAttachment(item models.Attachment, txs ...*gorm.DB) error {
tx.Rollback()
return err
} else {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
_ = marshal.Delete(contx, GetAttachmentCacheKey(item.Rid))
cachekit.Delete(gap.Ca, KgAttachmentCache(item.Rid))
}
tx.Commit()
@ -298,11 +275,8 @@ func DeleteAttachmentInBatch(items []models.Attachment, txs ...*gorm.DB) error {
return err
}
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
for _, rid := range rids {
_ = marshal.Delete(contx, GetAttachmentCacheKey(rid))
cachekit.Delete(gap.Ca, KgAttachmentCache(rid))
}
tx.Commit()

View File

@ -1,7 +1,6 @@
package services
import (
"context"
"errors"
"fmt"
"math"
@ -11,13 +10,11 @@ import (
"path/filepath"
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
@ -26,8 +23,8 @@ import (
"gorm.io/gorm"
)
func GetAttachmentFragmentCacheKey(rid string) any {
return fmt.Sprintf("attachment-fragment#%s", rid)
func KgAttachmentFragmentCache(rid string) string {
return cachekit.FKey("attachment-fragment", rid)
}
func NewAttachmentFragment(tx *gorm.DB, user *sec.UserInfo, fragment models.AttachmentFragment) (models.AttachmentFragment, error) {
@ -69,16 +66,11 @@ func NewAttachmentFragment(tx *gorm.DB, user *sec.UserInfo, fragment models.Atta
}
func GetFragmentByRID(rid string) (models.AttachmentFragment, error) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(
contx,
GetAttachmentFragmentCacheKey(rid),
new(models.AttachmentFragment),
if val, err := cachekit.Get[models.AttachmentFragment](
gap.Ca,
KgAttachmentFragmentCache(rid),
); err == nil {
return *val.(*models.AttachmentFragment), nil
return val, nil
}
var attachment models.AttachmentFragment
@ -94,16 +86,12 @@ func GetFragmentByRID(rid string) (models.AttachmentFragment, error) {
}
func CacheAttachmentFragment(item models.AttachmentFragment) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
_ = marshal.Set(
contx,
GetAttachmentFragmentCacheKey(item.Rid),
cachekit.Set[models.AttachmentFragment](
gap.Ca,
KgAttachmentFragmentCache(item.Rid),
item,
store.WithExpiration(60*time.Minute),
store.WithTags([]string{"attachment-fragment", fmt.Sprintf("user#%d", item.AccountID)}),
60*time.Minute,
cachekit.FKey("attachment", item.Rid),
)
}

View File

@ -9,12 +9,10 @@ import (
"path/filepath"
"time"
localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
jsoniter "github.com/json-iterator/go"
"github.com/minio/minio-go/v7"
"github.com/samber/lo"
@ -25,22 +23,17 @@ type openAttachmentResult struct {
Boosts []models.AttachmentBoost `json:"boost"`
}
func GetAttachmentOpenCacheKey(rid string) any {
func KgAttachmentOpenCache(rid string) string {
return fmt.Sprintf("attachment-open#%s", rid)
}
func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype string, err error) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
var result *openAttachmentResult
if val, err := marshal.Get(
contx,
GetAttachmentOpenCacheKey(rid),
new(openAttachmentResult),
if val, err := cachekit.Get[openAttachmentResult](
gap.Ca,
KgAttachmentOpenCache(rid),
); err == nil {
result = val.(*openAttachmentResult)
result = &val
}
if result == nil {
@ -158,15 +151,11 @@ func CacheOpenAttachment(item *openAttachmentResult) {
return
}
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
_ = marshal.Set(
contx,
GetAttachmentCacheKey(item.Attachment.Rid),
cachekit.Set[openAttachmentResult](
gap.Ca,
KgAttachmentCache(item.Attachment.Rid),
*item,
store.WithExpiration(60*time.Minute),
store.WithTags([]string{"attachment-open", fmt.Sprintf("user#%s", item.Attachment.Rid)}),
60*time.Minute,
cachekit.FKey("attachment", item.Attachment.Rid),
)
}