From 021f18315726685bba4daabf27c6309554305e0a Mon Sep 17 00:00:00 2001 From: mountain Date: Wed, 30 Aug 2023 16:35:22 +0900 Subject: [PATCH] =?UTF-8?q?provider=EC=97=90=EB=8F=84=20Query=20func=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session/consumer_common.go | 4 ++-- session/provider.go | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) 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 +}