diff --git a/session/consumer_common.go b/session/consumer_common.go index 5d6f5f3..b9db40a 100644 --- a/session/consumer_common.go +++ b/session/consumer_common.go @@ -21,8 +21,8 @@ func make_cache_stage[T any]() *cache_stage[T] { } type Consumer interface { - Query(key string) *Authorization - Touch(key string) bool + Query(string) *Authorization + Touch(string) bool } type consumer_common[T any] struct { diff --git a/session/provider.go b/session/provider.go index 7946153..0a387ad 100644 --- a/session/provider.go +++ b/session/provider.go @@ -11,8 +11,9 @@ import ( ) type Provider interface { - Update(key string, input *Authorization) error - Delete(key string) error + Update(string, *Authorization) error + Delete(string) error + Query(string) (*Authorization, error) } type provider_redis struct { @@ -91,6 +92,22 @@ func (p *provider_redis) Delete(key string) error { return err } +func (p *provider_redis) Query(key string) (*Authorization, error) { + payload, err := p.redisClient.Get(p.ctx, key).Result() + if err == redis.Nil { + return nil, nil + } else if err != nil { + return nil, err + } + + var auth Authorization + if err := bson.Unmarshal([]byte(payload), &auth); err != nil { + return nil, err + } + + return &auth, nil +} + func (p *provider_mongo) Update(key string, input *Authorization) error { _, _, err := p.mongoClient.Update(session_collection_name, bson.M{ "key": key, @@ -110,3 +127,12 @@ func (p *provider_mongo) Delete(key string) error { }) return err } + +func (p *provider_mongo) Query(key string) (*Authorization, error) { + var auth Authorization + err := p.mongoClient.FindOneAs(session_collection_name, bson.M{ + "key": key, + }, &auth) + + return &auth, err +}