43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package valkey
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
|
|
)
|
|
|
|
const profileTTL = 5 * time.Minute
|
|
|
|
// GetProfile reads an immutable profile; corrupt values and outages are cache misses.
|
|
func (c *Client) GetProfile(ctx context.Context, id uuid.UUID) (dto.UserDTO, bool) {
|
|
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
|
defer cancel()
|
|
key := "profile:" + id.String()
|
|
value, err := c.nodes[c.ring.Owner(key)].Get(ctx, key).Bytes()
|
|
if err != nil {
|
|
return dto.UserDTO{}, false
|
|
}
|
|
var user dto.UserDTO
|
|
if json.Unmarshal(value, &user) != nil || user.ID != id {
|
|
return dto.UserDTO{}, false
|
|
}
|
|
return user, true
|
|
}
|
|
|
|
// PutProfile populates the immutable profile cache with a bounded TTL.
|
|
func (c *Client) PutProfile(ctx context.Context, user dto.UserDTO) {
|
|
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
|
defer cancel()
|
|
value, err := json.Marshal(user)
|
|
if err != nil {
|
|
return
|
|
}
|
|
key := "profile:" + user.ID.String()
|
|
// Cache writes are best effort; PostgreSQL remains the source of truth.
|
|
_ = c.nodes[c.ring.Owner(key)].Set(ctx, key, value, profileTTL).Err()
|
|
}
|