This commit is contained in:
2026-01-25 21:59:00 +08:00
parent 7fd537bef3
commit 4cad3f0250
118 changed files with 30473 additions and 0 deletions

69
internal/cache/redis.go vendored Normal file
View File

@@ -0,0 +1,69 @@
package cache
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
"accounting-app/internal/config"
)
// RedisClient wraps the Redis client with additional functionality
type RedisClient struct {
client *redis.Client
cfg *config.Config
}
// NewRedisClient creates a new Redis client from the configuration
func NewRedisClient(cfg *config.Config) (*RedisClient, error) {
client := redis.NewClient(&redis.Options{
Addr: cfg.RedisAddr,
Password: cfg.RedisPassword,
DB: cfg.RedisDB,
})
rc := &RedisClient{
client: client,
cfg: cfg,
}
// Test the connection
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := rc.Ping(ctx); err != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
}
return rc, nil
}
// Ping checks if the Redis connection is healthy
func (rc *RedisClient) Ping(ctx context.Context) error {
_, err := rc.client.Ping(ctx).Result()
return err
}
// HealthCheck performs a health check on the Redis connection
func (rc *RedisClient) HealthCheck() error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return rc.Ping(ctx)
}
// Close closes the Redis connection
func (rc *RedisClient) Close() error {
return rc.client.Close()
}
// Client returns the underlying Redis client for direct access
func (rc *RedisClient) Client() *redis.Client {
return rc.client
}
// GetConfig returns the configuration used by this client
func (rc *RedisClient) GetConfig() *config.Config {
return rc.cfg
}