Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit f585482

Browse files
committed
add redis cache
1 parent 13db989 commit f585482

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

cache.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/go-redis/redis/v8"
7+
)
8+
9+
var client *redis.Client
10+
11+
func SetupCache() {
12+
client = redis.NewClient(&redis.Options{
13+
Addr: fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port),
14+
Password: conf.Redis.Password,
15+
DB: conf.Redis.DB,
16+
})
17+
pong, err := client.Ping(context.Background()).Result()
18+
if err != nil {
19+
logger.Fatal("Failed to connect to Redis server: ", err)
20+
} else {
21+
logger.Debug("Connected to Redis server successfully.")
22+
}
23+
}

conf.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ type Config struct {
1515
Port int
1616
}
1717
Redis struct {
18-
Host string
19-
Port int
20-
DB int
18+
Host string
19+
Port int
20+
Password string
21+
DB int
22+
Expiration int
2123
}
2224
}
2325

config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ server:
44
redis:
55
host: localhost
66
port: 6379
7-
db: 0
7+
password: ""
8+
db: 0

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func main() {
1515
SetupLogger()
1616
ReadConfig()
1717
ReadToken()
18+
SetupCache()
1819

1920
app.Listen(fmt.Sprintf(":%d", conf.Server.Port))
2021
}

0 commit comments

Comments
 (0)