monorepo/cloud/maplepress-backend/pkg/storage/cache/redis.go

33 lines
824 B
Go

package cache
import (
"context"
"fmt"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
// ProvideRedisClient creates a new Redis client
func ProvideRedisClient(cfg *config.Config, logger *zap.Logger) (*redis.Client, error) {
logger.Info("connecting to Redis",
zap.String("host", cfg.Cache.Host),
zap.Int("port", cfg.Cache.Port))
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Cache.Host, cfg.Cache.Port),
Password: cfg.Cache.Password,
DB: cfg.Cache.DB,
})
// Test connection
ctx := context.Background()
if err := client.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
}
logger.Info("successfully connected to Redis")
return client, nil
}