Skip to content

Commit 370586a

Browse files
redis cache working
1 parent 884d5f5 commit 370586a

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

cmd/api/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func main() {
2020
logger.InitLogger()
2121
utils.LoadConfig()
2222
utils.InitCache()
23+
utils.InitTokenCache()
2324
utils.InitDB()
2425
validator.InitValidator()
2526

pkg/controllers/submission.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func SubmitCode(c echo.Context) error {
4949
fmt.Println("CreateBatchSubmission error:", err)
5050
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create batch submission"})
5151
}
52+
for _, token := range tokens {
53+
if err := utils.TokenCache.Set(utils.Ctx, token, submissionID, 0).Err(); err != nil {
54+
fmt.Printf("Failed to cache token %s: %v\n", token, err)
55+
}
56+
}
5257

5358
sub := utils.SubmissionInput{
5459
ID: submissionID,

pkg/utils/tokenCache.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/redis/go-redis/v9"
9+
)
10+
11+
var (
12+
TokenCache *redis.Client
13+
Ctx = context.Background()
14+
)
15+
16+
// InitTokenCache initializes a separate Redis client for token cache
17+
func InitTokenCache() {
18+
redisHost := os.Getenv("REDIS_HOST")
19+
if redisHost == "" {
20+
redisHost = "localhost"
21+
}
22+
23+
redisPort := os.Getenv("REDIS_PORT")
24+
if redisPort == "" {
25+
redisPort = "6379"
26+
}
27+
28+
TokenCache = redis.NewClient(&redis.Options{
29+
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
30+
DB: 1, // Use DB 1 to keep it separate from queue DB 0
31+
})
32+
33+
// Test connection
34+
if err := TokenCache.Ping(Ctx).Err(); err != nil {
35+
panic(fmt.Sprintf("failed to connect to Redis token cache: %v", err))
36+
}
37+
}

0 commit comments

Comments
 (0)