2025-08-15 15:21:01 +08:00

58 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rpccache
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/localcache"
"github.com/openimsdk/open-im-server/v3/pkg/rpcli"
"github.com/openimsdk/protocol/auth"
"github.com/openimsdk/tools/log"
"github.com/redis/go-redis/v9"
)
func NewAuthLocalCache(client *rpcli.AuthClient, localCache *config.LocalCache, cli redis.UniversalClient) *AuthLocalCache {
lc := localCache.Auth
log.ZDebug(context.Background(), "AuthLocalCache", "topic", lc.Topic, "slotNum", lc.SlotNum, "slotSize", lc.SlotSize, "enable", lc.Enable())
x := &AuthLocalCache{
client: client,
local: localcache.New[[]byte](
localcache.WithLocalSlotNum(lc.SlotNum),
localcache.WithLocalSlotSize(lc.SlotSize),
localcache.WithLinkSlotNum(lc.SlotNum),
localcache.WithLocalSuccessTTL(lc.Success()),
localcache.WithLocalFailedTTL(lc.Failed()),
),
}
if lc.Enable() {
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
}
return x
}
type AuthLocalCache struct {
client *rpcli.AuthClient
local localcache.Cache[[]byte]
}
// 感觉有点问题 是应该保存token map还是根据OperationID来保存一个bool
// 也不应该只绑定token 是不是还得绑定其他属性 确认说是这个用户在操作的
func (a *AuthLocalCache) ParseToken(ctx context.Context, token string) (val *auth.ParseTokenResp, err error) {
log.ZDebug(ctx, "AuthLocalCache ParseToken req", "token", token)
defer func() {
if err != nil {
log.ZError(ctx, "AuthLocalCache ParseToken error", err, "token", token, "err", err)
} else {
log.ZDebug(ctx, "AuthLocalCache ParseToken resp", "token", token, "val", val)
}
}()
var cache cacheProto[auth.ParseTokenResp]
return cache.Unmarshal(a.local.Get(ctx, token, func(ctx context.Context) ([]byte, error) {
log.ZDebug(ctx, "AuthLocalCache ParseToken call rpc", "token", token)
return cache.Marshal(a.client.ParseToken(ctx, token))
}))
}