2023-08-30 13:15:44 +09:00
|
|
|
package session
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"repositories.action2quare.com/ayo/gocommon"
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-30 13:38:13 +09:00
|
|
|
type Provider struct {
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient *redis.Client
|
|
|
|
|
updateChannel string
|
|
|
|
|
deleteChannel string
|
|
|
|
|
ttl time.Duration
|
|
|
|
|
ctx context.Context
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 13:38:13 +09:00
|
|
|
func NewProvider(ctx context.Context, redisUrl string, ttl time.Duration) (*Provider, error) {
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient, err := gocommon.NewRedisClient(redisUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 13:38:13 +09:00
|
|
|
return &Provider{
|
2023-08-30 13:15:44 +09:00
|
|
|
redisClient: redisClient,
|
|
|
|
|
updateChannel: communication_channel_name_prefix + "_u",
|
|
|
|
|
deleteChannel: communication_channel_name_prefix + "_d",
|
|
|
|
|
ttl: ttl,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 13:38:13 +09:00
|
|
|
func (p *Provider) Update(key string, input *Authorization) error {
|
2023-08-30 13:15:44 +09:00
|
|
|
bt, err := bson.Marshal(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = p.redisClient.SetEX(p.ctx, key, bt, p.ttl).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = p.redisClient.Publish(p.ctx, p.updateChannel, key).Result()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 13:38:13 +09:00
|
|
|
func (p *Provider) Delete(key string) error {
|
2023-08-30 13:15:44 +09:00
|
|
|
cnt, err := p.redisClient.Del(p.ctx, key).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cnt > 0 {
|
|
|
|
|
_, err = p.redisClient.Publish(p.ctx, p.deleteChannel, key).Result()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|