♻️ Rebuilt cache system with nexus cache

This commit is contained in:
2025-03-29 13:49:52 +08:00
parent 60d8c0b766
commit c8abf6647a
6 changed files with 55 additions and 523 deletions

View File

@ -1,24 +0,0 @@
package cache
import (
"github.com/dgraph-io/ristretto"
"github.com/eko/gocache/lib/v4/store"
ristrettoCache "github.com/eko/gocache/store/ristretto/v4"
)
var S store.StoreInterface
func NewStore() error {
ris, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: 1 << 27,
BufferItems: 64,
})
if err != nil {
return err
}
S = ristrettoCache.NewRistretto(ris)
return nil
}

View File

@ -2,10 +2,13 @@ package gap
import (
"fmt"
"strings"
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit/pushcon"
"github.com/samber/lo"
"strings"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"github.com/rs/zerolog/log"
@ -13,8 +16,11 @@ import (
"github.com/spf13/viper"
)
var Nx *nex.Conn
var Px *pushcon.Conn
var (
Nx *nex.Conn
Px *pushcon.Conn
Ca *cachekit.CaConn
)
func InitializeToNexus() error {
grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2)
@ -47,6 +53,10 @@ func InitializeToNexus() error {
return fmt.Errorf("error during initialize pushcon: %v", err)
}
return err
Ca, err = cachekit.NewCaConn(Nx, 3*time.Second)
if err != nil {
return fmt.Errorf("error during initialize cachekit: %v", err)
}
return err
}

View File

@ -11,9 +11,9 @@ import (
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"github.com/gofiber/fiber/v2"
localCache "git.solsynth.dev/hypernet/interactive/pkg/internal/cache"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit"
@ -21,9 +21,6 @@ import (
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
aproto "git.solsynth.dev/hypernet/passport/pkg/proto"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
"gorm.io/datatypes"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
@ -53,16 +50,11 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) *
RealmList []uint `json:"realm"`
}
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
ctx := context.Background()
var self, allowlist, invisibleList, followList, realmList []uint
statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID)
statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState))
statusCacheKey := fmt.Sprintf("post-user-filter#%d", user.ID)
state, err := cachekit.Get[userContextState](gap.Ca, statusCacheKey)
if err == nil {
state := statusCache.(*userContextState)
allowlist = state.Allowlist
invisibleList = state.InvisibleList
followList = state.FollowList
@ -157,8 +149,8 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) *
return item.ID
})
_ = marshal.Set(
ctx,
cachekit.Set(
gap.Ca,
statusCacheKey,
userContextState{
Allowlist: allowlist,
@ -167,8 +159,8 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) *
FollowList: followList,
Self: self,
},
store.WithExpiration(2*time.Minute),
store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}),
5*time.Minute,
fmt.Sprintf("user#%d", user.ID),
)
}

View File

@ -7,7 +7,6 @@ import (
"syscall"
pkg "git.solsynth.dev/hypernet/interactive/pkg/internal"
"git.solsynth.dev/hypernet/interactive/pkg/internal/cache"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/fatih/color"
@ -72,11 +71,6 @@ func main() {
quartz.AddFunc("@every 5m", services.FetchFediverseTimedTask)
quartz.Start()
// Initialize cache
if err := cache.NewStore(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when initializing cache.")
}
// App
go http.NewServer().Listen()