feat: simplest room expiration

This commit is contained in:
efim 2023-12-01 04:14:23 +00:00
parent a502ee72a0
commit 5cab5d88d9
1 changed files with 4 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
"time"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
@ -57,6 +58,7 @@ type RoomManager interface {
} }
const roomRedisPrefix = "room" const roomRedisPrefix = "room"
const roomTtl = 24 * time.Hour
func roomNameToRedisId(roomName string) string { func roomNameToRedisId(roomName string) string {
return fmt.Sprintf("%s:%s", roomRedisPrefix, roomName) return fmt.Sprintf("%s:%s", roomRedisPrefix, roomName)
@ -155,7 +157,7 @@ func (redisRM RedisRM) Update(ctx context.Context, roomName string, f func(fromR
_, err = tx.Pipelined(ctx, func(pipe redis.Pipeliner) error { _, err = tx.Pipelined(ctx, func(pipe redis.Pipeliner) error {
log.Printf(">> about to Set %s to %v", roomName, room) log.Printf(">> about to Set %s to %v", roomName, room)
pipe.Set(ctx, roomKey, &room, 0) pipe.Set(ctx, roomKey, &room, roomTtl)
return nil return nil
}) })
@ -178,6 +180,6 @@ func (redisRM RedisRM) Update(ctx context.Context, roomName string, f func(fromR
} }
func (redisRM RedisRM) Save(ctx context.Context, room Room) error { func (redisRM RedisRM) Save(ctx context.Context, room Room) error {
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), &room, 0).Err() // maybe even set expiration? err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), &room, roomTtl).Err()
return err return err
} }