Compare commits
No commits in common. "f113ae6cbaf741f9531f1440e432ffd9139fe2cf" and "2c4355257c9089cb1970441e5a1f59ca38289d39" have entirely different histories.
f113ae6cba
...
2c4355257c
@ -6,10 +6,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/internal/cache"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@ -22,19 +20,6 @@ func userinfoFetch(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "user principal data was not found")
|
||||
}
|
||||
|
||||
if val, err := cachekit.Get[sec.UserInfo](
|
||||
cache.Kcc,
|
||||
cachekit.FKey(cachekit.DAUserInfoPrefix, claims.Session),
|
||||
); err == nil {
|
||||
c.Locals("nex_user", &val)
|
||||
tk, err := IWriter.WriteUserInfoJwt(val)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to sign userinfo: %v", err))
|
||||
}
|
||||
c.Locals("nex_token", tk)
|
||||
return nil
|
||||
}
|
||||
|
||||
service := directory.GetServiceInstanceByType(nex.ServiceTypeAuth)
|
||||
if service != nil {
|
||||
conn, err := service.GetGrpcConn()
|
||||
|
16
pkg/internal/cache/redis.go
vendored
16
pkg/internal/cache/redis.go
vendored
@ -1,16 +1,8 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
import "github.com/redis/go-redis/v9"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
Rdb *redis.Client
|
||||
Kcc *cachekit.Conn
|
||||
)
|
||||
var Rdb *redis.Client
|
||||
|
||||
func ConnectRedis(addr, password string, db int) error {
|
||||
Rdb = redis.NewClient(&redis.Options{
|
||||
@ -18,9 +10,5 @@ func ConnectRedis(addr, password string, db int) error {
|
||||
Password: password,
|
||||
DB: db,
|
||||
})
|
||||
Kcc = &cachekit.Conn{
|
||||
Rd: Rdb,
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
package cachekit
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Those constants are used to directly get the cached data from redis
|
||||
// Formatted like {prefix}#{key}
|
||||
const (
|
||||
DAUserInfoPrefix = "userinfo"
|
||||
)
|
||||
|
||||
func FKey(prefix string, key any) string {
|
||||
return fmt.Sprintf("%s#%v", prefix, key)
|
||||
}
|
@ -3,41 +3,24 @@ package cachekit
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// The global variable below is used to keep there will only be one redis client exist in a single instance
|
||||
// Prevent if other DirectAccess™ SDK creating too many redis clients
|
||||
// And able to recreate the conn with different options
|
||||
var (
|
||||
rdc *redis.Client
|
||||
rdl *sync.Mutex
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
n *nex.Conn
|
||||
Rd *redis.Client
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func NewConn(conn *nex.Conn, timeout time.Duration) (*Conn, error) {
|
||||
rdl.Lock()
|
||||
defer rdl.Unlock()
|
||||
|
||||
func NewCaConn(conn *nex.Conn, timeout time.Duration) (*Conn, error) {
|
||||
c := &Conn{
|
||||
n: conn,
|
||||
Timeout: timeout,
|
||||
}
|
||||
|
||||
if rdc != nil {
|
||||
c.Rd = rdc
|
||||
return c, nil
|
||||
}
|
||||
|
||||
rdb := conn.AllocResource(nex.AllocatableResourceCache)
|
||||
if rdb == nil {
|
||||
return nil, fmt.Errorf("unable to allocate resource: cache")
|
||||
@ -45,7 +28,6 @@ func NewConn(conn *nex.Conn, timeout time.Duration) (*Conn, error) {
|
||||
return nil, fmt.Errorf("allocated cache resource is not a redis client")
|
||||
} else {
|
||||
c.Rd = client
|
||||
rdc = client
|
||||
}
|
||||
|
||||
return c, nil
|
||||
|
Reference in New Issue
Block a user