forked from mtusi2026/mtusi2026lab2
fix: address lab2 review feedback
This commit is contained in:
@@ -4,11 +4,13 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"mtusi2026lab2/internal/model"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -48,7 +50,7 @@ func cacheKey(token string) string {
|
||||
func (c *SessionCache) Set(
|
||||
ctx context.Context,
|
||||
token string,
|
||||
userID uuid.UUID,
|
||||
user model.User,
|
||||
expiresAt time.Time,
|
||||
) error {
|
||||
ttl := time.Until(expiresAt)
|
||||
@@ -56,33 +58,31 @@ func (c *SessionCache) Set(
|
||||
return nil
|
||||
}
|
||||
|
||||
return c.client.Set(
|
||||
ctx,
|
||||
cacheKey(token),
|
||||
userID.String(),
|
||||
ttl,
|
||||
).Err()
|
||||
payload, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.client.Set(ctx, cacheKey(token), payload, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *SessionCache) Get(
|
||||
ctx context.Context,
|
||||
token string,
|
||||
) (uuid.UUID, bool, error) {
|
||||
) (model.User, bool, error) {
|
||||
value, err := c.client.Get(ctx, cacheKey(token)).Result()
|
||||
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return uuid.Nil, false, nil
|
||||
return model.User{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, false, err
|
||||
return model.User{}, false, err
|
||||
}
|
||||
|
||||
userID, err := uuid.Parse(value)
|
||||
if err != nil {
|
||||
return uuid.Nil, false, err
|
||||
var user model.User
|
||||
if err := json.Unmarshal([]byte(value), &user); err != nil {
|
||||
return model.User{}, false, err
|
||||
}
|
||||
|
||||
return userID, true, nil
|
||||
return user, true, nil
|
||||
}
|
||||
|
||||
func (c *SessionCache) Delete(
|
||||
|
||||
Reference in New Issue
Block a user