mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
63007fc8c7
Result: {"status":"keep","total_issues":8,"eslint_errors":0,"eslint_problems":0,"eslint_warnings":0,"golint_canonicalheader":0,"golint_errname":0,"golint_errorlint":1,"golint_exhaustive":0,"golint_forcetypeassert":0,"golint_gosec":0,"golint_intrange":0,"golint_modernize":3,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":0,"golint_recvcheck":1,"golint_test_testifylint":0,"golint_test_thelper":0,"golint_test_total":0,"golint_test_usetesting":0,"golint_total":8,"golint_usestdlibvars":0,"golint_vetx_total":0,"golint_wastedassign":0,"measure_s":92,"tsc_errors":0,"vitest_failed":0,"vitest_total":126}
160 lines
4.2 KiB
Go
160 lines
4.2 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
|
|
db "github.com/Rain-kl/Wavelet/internal/infra/persistence"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/Rain-kl/Wavelet/internal/repository"
|
|
"github.com/Rain-kl/Wavelet/pkg/cache/ram"
|
|
)
|
|
|
|
const (
|
|
uploadMetaRedisCacheTTL = 30 * 60 // seconds
|
|
uploadMetaRAMMaximumSize = 4096
|
|
uploadMetaInvalidationChan = "upload:meta_invalidation"
|
|
)
|
|
|
|
type uploadMetaInvalidationMessage struct {
|
|
ID uint64 `json:"id"`
|
|
}
|
|
|
|
var (
|
|
uploadMetaRAM = ram.MustNew[uint64, model.Upload](ram.Options{MaximumSize: uploadMetaRAMMaximumSize})
|
|
uploadMetaListenerOnce sync.Once
|
|
uploadMetaListenerCtx context.Context
|
|
uploadMetaListenerCancel context.CancelFunc
|
|
uploadMetaListenerDone chan struct{}
|
|
)
|
|
|
|
func uploadMetaRedisKey(id uint64) string {
|
|
return fmt.Sprintf("upload:meta:%d", id)
|
|
}
|
|
|
|
func cloneUpload(upload model.Upload) model.Upload {
|
|
return upload
|
|
}
|
|
|
|
func ensureUploadMetaCacheListener() {
|
|
if db.Redis == nil {
|
|
return
|
|
}
|
|
uploadMetaListenerOnce.Do(startUploadMetaCacheInvalidationListener)
|
|
}
|
|
|
|
func startUploadMetaCacheInvalidationListener() {
|
|
uploadMetaListenerCtx, uploadMetaListenerCancel = context.WithCancel(context.Background())
|
|
uploadMetaListenerDone = make(chan struct{})
|
|
// 捕获当前客户端:goroutine 不再读可变全局 db.Redis,测试置空/替换全局时不会数据竞争
|
|
redisClient := db.Redis
|
|
|
|
go func() {
|
|
defer close(uploadMetaListenerDone)
|
|
pubsub := redisClient.Subscribe(uploadMetaListenerCtx, uploadMetaInvalidationChan)
|
|
defer func() {
|
|
_ = pubsub.Close()
|
|
}()
|
|
|
|
go func() {
|
|
<-uploadMetaListenerCtx.Done()
|
|
_ = pubsub.Close()
|
|
}()
|
|
|
|
for msg := range pubsub.Channel() {
|
|
var payload uploadMetaInvalidationMessage
|
|
if err := json.Unmarshal([]byte(msg.Payload), &payload); err != nil || payload.ID == 0 {
|
|
uploadMetaRAM.InvalidateAll()
|
|
continue
|
|
}
|
|
uploadMetaRAM.Invalidate(payload.ID)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func publishUploadMetaRAMInvalidation(ctx context.Context, id uint64) {
|
|
if db.Redis == nil {
|
|
return
|
|
}
|
|
payload, err := json.Marshal(uploadMetaInvalidationMessage{ID: id})
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = db.Redis.Publish(ctx, uploadMetaInvalidationChan, payload).Err()
|
|
}
|
|
|
|
// GetUploadByID loads upload metadata from RAM, Redis, or the database.
|
|
func GetUploadByID(ctx context.Context, id uint64) (model.Upload, error) {
|
|
ensureUploadMetaCacheListener()
|
|
|
|
if upload, ok := uploadMetaRAM.GetIfPresent(id); ok {
|
|
return cloneUpload(upload), nil
|
|
}
|
|
|
|
key := uploadMetaRedisKey(id)
|
|
if db.Redis != nil {
|
|
var upload model.Upload
|
|
if err := db.GetJSON(ctx, key, &upload); err == nil {
|
|
uploadMetaRAM.Set(id, cloneUpload(upload))
|
|
return upload, nil
|
|
}
|
|
}
|
|
|
|
upload, err := repository.GetCacheableUploadByID(ctx, id)
|
|
if err != nil {
|
|
return model.Upload{}, err
|
|
}
|
|
|
|
SetUploadMetaCache(ctx, &upload)
|
|
return upload, nil
|
|
}
|
|
|
|
// SetUploadMetaCache populates RAM and Redis upload metadata caches.
|
|
func SetUploadMetaCache(ctx context.Context, upload *model.Upload) {
|
|
ensureUploadMetaCacheListener()
|
|
|
|
if upload == nil {
|
|
return
|
|
}
|
|
|
|
cloned := cloneUpload(*upload)
|
|
uploadMetaRAM.Set(upload.ID, cloned)
|
|
if db.Redis != nil {
|
|
_ = db.SetJSON(ctx, uploadMetaRedisKey(upload.ID), cloned, uploadMetaRedisCacheTTL)
|
|
}
|
|
}
|
|
|
|
// InvalidateUploadMetaCache clears RAM and Redis upload metadata caches and notifies peer nodes.
|
|
func InvalidateUploadMetaCache(ctx context.Context, id uint64) {
|
|
ensureUploadMetaCacheListener()
|
|
|
|
uploadMetaRAM.Invalidate(id)
|
|
if db.Redis != nil {
|
|
_ = db.Redis.Del(ctx, db.PrefixedKey(uploadMetaRedisKey(id))).Err()
|
|
publishUploadMetaRAMInvalidation(ctx, id)
|
|
}
|
|
}
|
|
|
|
// ResetUploadMetaCacheForTest clears the in-process upload metadata RAM cache.
|
|
func ResetUploadMetaCacheForTest() {
|
|
uploadMetaRAM.InvalidateAll()
|
|
}
|
|
|
|
// StopUploadMetaCacheListener stops the Redis Pub/Sub subscription listener and resets the sync.Once guard.
|
|
func StopUploadMetaCacheListener() {
|
|
if uploadMetaListenerCancel != nil {
|
|
uploadMetaListenerCancel()
|
|
if uploadMetaListenerDone != nil {
|
|
<-uploadMetaListenerDone // 等待 goroutine 退出,保证之后置空 db.Redis 不再竞争
|
|
}
|
|
uploadMetaListenerCancel = nil
|
|
uploadMetaListenerDone = nil
|
|
}
|
|
uploadMetaListenerOnce = sync.Once{}
|
|
}
|