Skip to content

Commit 70733dd

Browse files
session redis seperated from normal redis for session related functions
1 parent 3e7dc51 commit 70733dd

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

internal/session/sessredis.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package session
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"time"
8+
9+
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
10+
)
11+
12+
/* TODO: make the operations below thread safe with mutexes*/
13+
14+
/* store session into Redis database */
15+
func (m *Manager) saveSessionRedis(session *Session) error {
16+
ctx := context.Background()
17+
18+
/* session key for redis */
19+
key := fmt.Sprintf("session:%s", session.ID)
20+
21+
/* serialize the session with relevant information */
22+
sessionSerialized := session.serializeSessionForRedis()
23+
24+
/* hset the session to redis */
25+
if err := m.redis.HSet(ctx, key, sessionSerialized).Err(); err != nil {
26+
return fmt.Errorf("failed to save session to Redis: %w", err)
27+
}
28+
29+
return nil
30+
}
31+
32+
/* update expiry time in session */
33+
func (m *Manager) updateSessionExpiryRedis(session *Session) error {
34+
35+
/*
36+
function expects that new expiry time is already set in the session
37+
*/
38+
39+
ctx := context.Background()
40+
41+
/* create a key for Redis operation */
42+
key := fmt.Sprintf("session:%s", session.ID)
43+
44+
/* convert the expiry time to */
45+
formattedExpiry := session.Expiry.Format(time.RFC3339)
46+
47+
/* update just the expiry field */
48+
err := m.redis.HSet(ctx, key, "expiry", formattedExpiry).Err()
49+
if err != nil {
50+
return fmt.Errorf("failed to update session expiry in Redis: %w", err)
51+
}
52+
53+
return nil
54+
}
55+
56+
/* update status of the session - update and set expired operations will be done with this */
57+
func (m *Manager) updateSessionStatusRedis(session *Session, status Status) error {
58+
59+
ctx := context.Background()
60+
61+
/* create a key for Redis operation */
62+
key := fmt.Sprintf("session:%s", session.ID)
63+
64+
/* update the session status */
65+
err := m.redis.HSet(ctx, key, "status", status).Err()
66+
if err != nil {
67+
return fmt.Errorf("failed to mark session as expired in Redis: %w", err)
68+
}
69+
70+
return nil
71+
}
72+
73+
/* save transaction results to redis */
74+
func (m *Manager) SaveTransactionRedisList(session *Session, txResult *types.Transaction, list string) error {
75+
76+
ctx := context.Background()
77+
78+
/* get the session ID */
79+
sessionID := session.ID
80+
81+
/* create a key for Redis operation */
82+
key := fmt.Sprintf("session:%s:%s", sessionID, list)
83+
84+
/* marshal transaction result to JSON */
85+
resultBytes, err := json.Marshal(txResult)
86+
if err != nil {
87+
return fmt.Errorf("failed to marshal transaction result: %w", err)
88+
}
89+
90+
/* push the transaction result in the back of the list */
91+
return m.redis.RPush(ctx, key, resultBytes).Err()
92+
}

0 commit comments

Comments
 (0)