Player cache and project structure refactor.

This commit is contained in:
MaxJa4
2024-01-21 18:02:27 +01:00
parent 4aae0896aa
commit f2573f2273
10 changed files with 73 additions and 60 deletions

View File

@@ -1,18 +1,17 @@
package models
import (
"context"
"cache"
"github.com/glebarez/sqlite"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
var DB *gorm.DB
var ctx = context.Background()
var Cache *redis.Client
var PlayerCache *cache.PlayerCache
func ConnectDatabase() {
database, err := gorm.Open(sqlite.Open("isc_data.db"), &gorm.Config{
@@ -61,25 +60,15 @@ func ConnectDatabase() {
DB = database
}
func ConnectCache() {
func ConnectCache(playerCacheLifetime time.Duration) {
address := os.Getenv("REDIS_ADDRESS")
if address == "" {
address = "127.0.0.1"
}
port := os.Getenv("REDIS_PORT")
if port == "" {
port = "6379"
address = "127.0.0.1:6379"
}
Cache = redis.NewClient(&redis.Options{
Addr: address + ":" + port,
Password: "",
DB: 0,
})
_, err := Cache.Ping(ctx).Result()
if err != nil {
Cache = nil
PlayerCache = cache.NewPlayerCache(address, playerCacheLifetime)
if err := PlayerCache.Connect(); err != nil {
PlayerCache = nil
log.Fatal("Failed to connect to Redis! " + err.Error())
}
}