wshandler도 session.Consumer로 교체
This commit is contained in:
@ -16,7 +16,7 @@ type sessionRedis struct {
|
||||
}
|
||||
|
||||
type consumer_redis struct {
|
||||
consumer_common[sessionRedis]
|
||||
consumer_common[*sessionRedis]
|
||||
redisClient *redis.Client
|
||||
}
|
||||
|
||||
@ -27,10 +27,10 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
}
|
||||
|
||||
consumer := &consumer_redis{
|
||||
consumer_common: consumer_common[sessionRedis]{
|
||||
consumer_common: consumer_common[*sessionRedis]{
|
||||
ttl: ttl,
|
||||
ctx: ctx,
|
||||
stages: [2]*cache_stage[sessionRedis]{make_cache_stage[sessionRedis](), make_cache_stage[sessionRedis]()},
|
||||
stages: [2]*cache_stage[*sessionRedis]{make_cache_stage[*sessionRedis](), make_cache_stage[*sessionRedis]()},
|
||||
startTime: time.Now(),
|
||||
},
|
||||
redisClient: redisClient,
|
||||
@ -73,7 +73,7 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
} else if len(raw) > 0 {
|
||||
var si Authorization
|
||||
if bson.Unmarshal([]byte(raw), &si) == nil {
|
||||
consumer.add(key, sessionRedis{
|
||||
consumer.add(key, &sessionRedis{
|
||||
Authorization: &si,
|
||||
expireAt: time.Now().Add(consumer.ttl),
|
||||
})
|
||||
@ -90,16 +90,13 @@ func NewConsumerWithRedis(ctx context.Context, redisUrl string, ttl time.Duratio
|
||||
return consumer, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Query(key string) *Authorization {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
func (c *consumer_redis) query_internal(key string) (*sessionRedis, bool, error) {
|
||||
if _, deleted := c.stages[0].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
if _, deleted := c.stages[1].deleted[key]; deleted {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
found, ok := c.stages[0].cache[key]
|
||||
@ -108,62 +105,92 @@ func (c *consumer_redis) Query(key string) *Authorization {
|
||||
}
|
||||
|
||||
if ok {
|
||||
if found.expireAt.After(time.Now()) {
|
||||
return found.Authorization
|
||||
}
|
||||
return found, false, nil
|
||||
}
|
||||
|
||||
payload, err := c.redisClient.Get(c.ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
return nil, false, nil
|
||||
} else if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(payload) > 0 {
|
||||
var si Authorization
|
||||
if bson.Unmarshal([]byte(payload), &si) == nil {
|
||||
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
c.add_internal(key, sessionRedis{
|
||||
Authorization: &si,
|
||||
expireAt: time.Now().Add(ttl),
|
||||
})
|
||||
return &si
|
||||
}
|
||||
if len(payload) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil
|
||||
|
||||
var auth Authorization
|
||||
if err := bson.Unmarshal([]byte(payload), &auth); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
|
||||
if err != nil {
|
||||
logger.Println("consumer Query :", err)
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
si := &sessionRedis{
|
||||
Authorization: &auth,
|
||||
expireAt: time.Now().Add(ttl),
|
||||
}
|
||||
c.add_internal(key, si)
|
||||
|
||||
return si, true, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Touch(key string) bool {
|
||||
func (c *consumer_redis) Query(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
si, _, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if time.Now().After(si.expireAt) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
func (c *consumer_redis) Touch(key string) (*Authorization, error) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
ok, err := c.redisClient.Expire(c.ctx, key, c.ttl).Result()
|
||||
if err == redis.Nil {
|
||||
return false
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
logger.Println("consumer Touch :", err)
|
||||
return false
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ok {
|
||||
newexpire := time.Now().Add(c.ttl)
|
||||
found, ok := c.stages[0].cache[key]
|
||||
if ok {
|
||||
found.expireAt = newexpire
|
||||
// redis에 살아있다.
|
||||
si, added, err := c.query_internal(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
found, ok = c.stages[1].cache[key]
|
||||
if ok {
|
||||
found.expireAt = newexpire
|
||||
if si == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if !added {
|
||||
si.expireAt = time.Now().Add(c.ttl)
|
||||
// stage 0으로 옮기기 위해 add_internal을 다시 부름
|
||||
c.add_internal(key, si)
|
||||
}
|
||||
|
||||
return si.Authorization, nil
|
||||
}
|
||||
|
||||
return ok
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user