Files
tm_back/pkg/redis/README.md
T
n.nakhostin 98f581ec97 Enhance cursor rules and add user management functionality
- Updated cursor rules to emphasize the importance of dependency injection and logging practices.
- Introduced a new user management domain, including entities, services, handlers, and validation.
- Implemented user authentication features such as login, registration, and role-based access control.
- Added Redis integration for token management and caching.
- Enhanced API documentation with Swagger for user-related endpoints.
- Updated configuration to support new JWT settings and Redis connection parameters.
2025-08-10 14:09:17 +03:30

2.4 KiB

Redis Package

This package provides Redis client functionality for the Tender Management system, specifically designed for token blacklisting and caching operations.

Features

  • Connection Management: Handles Redis client connections with proper error handling
  • Interface-based Design: Uses interfaces for easy testing and mocking
  • Configuration Support: Supports Redis configuration from environment variables
  • Connection Testing: Validates connections on startup
  • Resource Cleanup: Proper connection closing and cleanup

Usage

Basic Setup

import "tm/pkg/redis"

// Create Redis configuration
config := &redis.Config{
    Host:     "localhost",
    Port:     6379,
    Password: "password",
    DB:       0,
    PoolSize: 10,
}

// Create Redis client
client, err := redis.NewClient(config, logger)
if err != nil {
    log.Fatal(err)
}
defer client.Close()

// Use the client
err = client.Set(ctx, "key", "value", time.Hour)

Token Blacklisting

The Redis client is primarily used for JWT token blacklisting:

// Add token to blacklist
err = client.Set(ctx, "blacklist:token_hash", "1", tokenExpiration)

// Check if token is blacklisted
exists, err := client.Exists(ctx, "blacklist:token_hash")
if err != nil {
    // Handle error
}
if exists > 0 {
    // Token is blacklisted
}

Configuration

The Redis configuration supports the following fields:

  • Host: Redis server hostname (default: localhost)
  • Port: Redis server port (default: 6379)
  • Password: Redis authentication password
  • DB: Redis database number (default: 0)
  • PoolSize: Connection pool size (default: 10)

Environment Variables

The configuration can be loaded from environment variables:

  • TM_CACHE_REDIS_HOST
  • TM_CACHE_REDIS_PORT
  • TM_CACHE_REDIS_PASSWORD
  • TM_CACHE_REDIS_DB
  • TM_CACHE_REDIS_POOL_SIZE

Error Handling

The package provides comprehensive error handling:

  • Connection failures are logged with context
  • All Redis operations return proper errors
  • Connection timeouts are handled gracefully
  • Resource cleanup is guaranteed with defer statements

Testing

The interface-based design makes it easy to mock Redis operations in tests:

type MockRedisClient struct {
    // Implement the Client interface
}

// Use in tests
client := &MockRedisClient{}

Dependencies

  • github.com/redis/go-redis/v9: Redis client library
  • tm/pkg/logger: Logging interface