redis

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2025 License: MIT Imports: 7 Imported by: 0

README

Redis Module

Contents

Enable

liquor app enable redis
# or
go get github.com/go-liquor/liquor-sdk/modules/redis

In cmd/app/main.go add module:

package main

import (
    "github.com/go-liquor/framework/internal/adapters/server/http"
    "github.com/go-liquor/framework/internal/app/services"
    "github.com/go-liquor/liquor-sdk/app"
    "github.com/go-liquor/liquor-sdk/modules/redis"
)

func main() {
    app.NewApp(
        redis.RedisModule,
        http.Server,
        app.RegisterService(
            services.NewInitialService,
        ),
    )
}

Add this in your config.yaml:

redis:
  addr: "<REDIS_ADDR>" #eg localhost:6379
  password: ""  # optional

Available Operations

Key-Value Operations
type RedisClient interface {
    // Store a key with optional expiration
    Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
    
    // Retrieve a key's value
    Get(ctx context.Context, key string) (string, error)
    
    // Delete one or more keys
    Delete(ctx context.Context, keys ...string) error
    
    // Check if keys exist
    Exists(ctx context.Context, keys ...string) (bool, error)
    
    // Set key expiration
    Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
    
    // Increment a counter
    Incr(ctx context.Context, key string) (int64, error)
}
Hash Operations
type RedisClient interface {
    // Set hash fields
    HSet(ctx context.Context, key string, values ...interface{}) error
    
    // Get a hash field
    HGet(ctx context.Context, key, field string) (string, error)
    
    // Get all hash fields
    HGetAll(ctx context.Context, key string) (map[string]string, error)
    
    // Delete hash fields
    HDel(ctx context.Context, key string, fields ...string) error
}
List Operations
type RedisClient interface {
    // Add to list head
    LPush(ctx context.Context, key string, values ...interface{}) error
    
    // Remove from list head
    LPop(ctx context.Context, key string) (string, error)
    
    // Add to list tail
    RPush(ctx context.Context, key string, values ...interface{}) error
    
    // Remove from list tail
    RPop(ctx context.Context, key string) (string, error)
}

Usage Example

type Service struct {
    redis RedisClient
}

func NewService(redis RedisClient) *Service {
    return &Service{
        redis: redis,
    }
}

func (s *Service) CacheData(ctx context.Context) error {
    // Store data with 1 hour expiration
    err := s.redis.Set(ctx, "my-key", "my-value", time.Hour)
    if err != nil {
        return err
    }

    // Store hash data
    err = s.redis.HSet(ctx, "user:123", "name", "John", "age", "30")
    if err != nil {
        return err
    }

    // Add to list
    err = s.redis.LPush(ctx, "notifications", "new-message")
    if err != nil {
        return err
    }

    return nil
}

In-Memory Implementation

For testing or development purposes, you can use the in-memory implementation:

func main() {
    // Use in-memory implementation
    redis := redis.NewInMemoryRedis()
    
    service := NewService(redis)
    // ... use service
}

Testing

func TestYourService(t *testing.T) {
    // Using in-memory implementation
    redis := redis.NewInMemoryRedis()
    service := NewService(redis)

    // Test your service
    err := service.CacheData(context.Background())
    require.NoError(t, err)

    // Verify data
    value, err := redis.Get(context.Background(), "my-key")
    require.NoError(t, err)
    assert.Equal(t, "my-value", value)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RedisModule = fx.Module("liquor-redis-module", fx.Provide(NewRedisClient))

Functions

This section is empty.

Types

type RedisClient

type RedisClient interface {
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Get(ctx context.Context, key string) (string, error)
	Delete(ctx context.Context, keys ...string) error
	Exists(ctx context.Context, keys ...string) (bool, error)
	Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
	Incr(ctx context.Context, key string) (int64, error)
	HSet(ctx context.Context, key string, values ...interface{}) error
	HGet(ctx context.Context, key, field string) (string, error)
	HGetAll(ctx context.Context, key string) (map[string]string, error)
	HDel(ctx context.Context, key string, fields ...string) error
	LPush(ctx context.Context, key string, values ...interface{}) error
	LPop(ctx context.Context, key string) (string, error)
	RPush(ctx context.Context, key string, values ...interface{}) error
	RPop(ctx context.Context, key string) (string, error)
}

RedisClient defines the interface for Redis operations

func NewInMemoryRedis

func NewInMemoryRedis() RedisClient

NewInMemoryRedis creates a new in-memory implementation of RedisClient

func NewRedisClient

func NewRedisClient(cfg *config.Config) RedisClient

NewRedisClient creates a new Redis client using the provided configuration.

Parameters:

  • cfg: Configuration object containing Redis connection details

Returns:

  • RedisClient: Interface for Redis operations

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL