Skip to content

Commit 5af9227

Browse files
fix: add ttl to redis session records
Signed-off-by: Henry Whitaker <henrywhitaker3@outlook.com>
1 parent 55a3c43 commit 5af9227

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

internal/repository/redis_session_queries.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package repository
33
import (
44
"context"
55
"encoding/json"
6+
"time"
67

78
"github.com/redis/rueidis"
89
)
@@ -61,7 +62,9 @@ func (r *RedisSessionRepository) CreateSession(
6162
return out, err
6263
}
6364

64-
cmd := r.c.B().Set().Key(params.UUID).Value(string(raw)).Nx().Build()
65+
cmd := r.c.B().Set().Key(params.UUID).Value(string(raw)).
66+
Nx().ExSeconds(params.Expiry - time.Now().Unix()).
67+
Build()
6568
if err := r.c.Do(ctx, cmd).Error(); err != nil {
6669
return out, err
6770
}
@@ -94,7 +97,10 @@ func (r *RedisSessionRepository) UpdateSession(
9497
return session, err
9598
}
9699

97-
cmd := r.c.B().Set().Key(params.UUID).Value(string(raw)).Build()
100+
cmd := r.c.B().Set().Key(params.UUID).
101+
Value(string(raw)).
102+
ExSeconds(params.Expiry - time.Now().Unix()).
103+
Build()
98104
if err := r.c.Do(ctx, cmd).Error(); err != nil {
99105
return session, err
100106
}

0 commit comments

Comments
 (0)