consumer/provider에 Authorization타입으로 고정

This commit is contained in:
2023-08-30 13:38:13 +09:00
parent a5d66a0249
commit d8458662fd
4 changed files with 43 additions and 37 deletions

View File

@ -11,43 +11,43 @@ import (
"repositories.action2quare.com/ayo/gocommon/logger"
)
type innerSession[T any] struct {
inner *T
type innerSession struct {
inner *Authorization
expireAt time.Time
}
type cache_stage[T any] struct {
cache map[string]innerSession[T]
type cache_stage struct {
cache map[string]innerSession
deleted map[string]bool
}
func make_cache_stage[T any]() *cache_stage[T] {
return &cache_stage[T]{
cache: make(map[string]innerSession[T]),
func make_cache_stage() *cache_stage {
return &cache_stage{
cache: make(map[string]innerSession),
deleted: make(map[string]bool),
}
}
type Consumer[T any] struct {
type Consumer struct {
lock sync.Mutex
redisClient *redis.Client
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
stages [2]*cache_stage
startTime time.Time
}
func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration) (*Consumer[T], error) {
func NewConsumer(ctx context.Context, redisUrl string, ttl time.Duration) (*Consumer, error) {
redisClient, err := gocommon.NewRedisClient(redisUrl)
if err != nil {
return nil, err
}
consumer := &Consumer[T]{
consumer := &Consumer{
redisClient: redisClient,
ttl: ttl,
ctx: ctx,
stages: [2]*cache_stage[T]{make_cache_stage[T](), make_cache_stage[T]()},
stages: [2]*cache_stage{make_cache_stage(), make_cache_stage()},
startTime: time.Now(),
}
@ -86,7 +86,7 @@ func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration)
if err != nil {
logger.Println(err)
} else if len(raw) > 0 {
var si T
var si Authorization
if bson.Unmarshal([]byte(raw), &si) == nil {
consumer.add(key, &si)
}
@ -102,8 +102,8 @@ func NewConsumer[T any](ctx context.Context, redisUrl string, ttl time.Duration)
return consumer, nil
}
func (c *Consumer[T]) add_internal(key string, si *T, ttl time.Duration) {
inner := innerSession[T]{
func (c *Consumer) add_internal(key string, si *Authorization, ttl time.Duration) {
inner := innerSession{
inner: si,
expireAt: time.Now().Add(ttl),
}
@ -116,14 +116,14 @@ func (c *Consumer[T]) add_internal(key string, si *T, ttl time.Duration) {
logger.Printf("add : %v, %v\n", *c.stages[0], *c.stages[1])
}
func (c *Consumer[T]) add(key string, si *T) {
func (c *Consumer) add(key string, si *Authorization) {
c.lock.Lock()
defer c.lock.Unlock()
c.add_internal(key, si, c.ttl)
}
func (c *Consumer[T]) delete(key string) {
func (c *Consumer) delete(key string) {
c.lock.Lock()
defer c.lock.Unlock()
@ -135,19 +135,19 @@ func (c *Consumer[T]) delete(key string) {
logger.Printf("delete : %v, %v\n", *c.stages[0], *c.stages[1])
}
func (c *Consumer[T]) changeStage() {
func (c *Consumer) changeStage() {
c.lock.Lock()
defer c.lock.Unlock()
logger.Printf("changeStage : %v, %v\n", *c.stages[0], *c.stages[1])
c.stages[1] = c.stages[0]
c.stages[0] = make_cache_stage[T]()
c.stages[0] = make_cache_stage()
logger.Printf("---> : %v, %v\n", *c.stages[0], *c.stages[1])
}
func (c *Consumer[T]) Query(key string) *T {
func (c *Consumer) Query(key string) *Authorization {
c.lock.Lock()
defer c.lock.Unlock()
@ -179,7 +179,7 @@ func (c *Consumer[T]) Query(key string) *T {
}
if len(payload) > 0 {
var si T
var si Authorization
if bson.Unmarshal([]byte(payload), &si) == nil {
ttl, err := c.redisClient.TTL(c.ctx, key).Result()
if err != nil {
@ -194,7 +194,7 @@ func (c *Consumer[T]) Query(key string) *T {
return nil
}
func (c *Consumer[T]) Touch(key string) bool {
func (c *Consumer) Touch(key string) bool {
c.lock.Lock()
defer c.lock.Unlock()