Documentation
¶
Overview ¶
Example (ContainerUsage) ¶
Example_containerUsage demonstrates how to use Redis plugin with containers
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Configure test Redis with container
cfg := TestConfig{
Config: Config{
Database: 0,
},
RunContainer: true,
ContainerImage: "redis:7-alpine",
ContainerName: "redis-example-container",
}
// Create test plugin (starts container automatically)
plugin, err := NewTestPlugin(ctx, cfg)
if err != nil {
fmt.Printf("Failed to create test plugin: %v\n", err)
return
}
// Use Redis client for operations
client := plugin.DB()
// Set a value
err = client.Set(ctx, "example-key", "example-value", time.Hour).Err()
if err != nil {
fmt.Printf("Failed to set key: %v\n", err)
return
}
// Get the value
val, err := client.Get(ctx, "example-key").Result()
if err != nil {
fmt.Printf("Failed to get key: %v\n", err)
return
}
fmt.Printf("Retrieved value: %s\n", val)
// Test Client() method for compatibility
cmdableClient := plugin.Client()
// Use through Cmdable interface
err = cmdableClient.Set(ctx, "cmdable-key", "cmdable-value", time.Hour).Err()
if err != nil {
fmt.Printf("Failed to set key through Cmdable: %v\n", err)
return
}
val2, err := cmdableClient.Get(ctx, "cmdable-key").Result()
if err != nil {
fmt.Printf("Failed to get key through Cmdable: %v\n", err)
return
}
fmt.Printf("Retrieved through Cmdable: %s\n", val2)
Output: Retrieved value: example-value Retrieved through Cmdable: cmdable-value
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Endpoint string `env:"ENDPOINT" envDefault:":6379" comment:"Endpoint = host:port,host:port addresses of ring shards."`
Cluster bool `env:"CLUSTER" comment:"Cluster = enable cluster mode"`
Database int `env:"DATABASE" required:"true" comment:"Database to be selected after connecting to the server."`
Username string `` /* 231-byte string literal not displayed */
Password string `` /* 285-byte string literal not displayed */
HeartbeatFrequency time.Duration `` /* 155-byte string literal not displayed */
MaxRetries int `env:"MAX_RETRIES" comment:"Maximum number of retries before giving up. Default is 3 retries; -1 (not 0) disables retries."`
MinRetryBackoff time.Duration `env:"MIN_RETRY_BACKOFF" comment:"Minimum backoff between each retry. Default is 8 milliseconds; -1 disables backoff."`
MaxRetryBackoff time.Duration `env:"MAX_RETRY_BACKOFF" comment:"Maximum backoff between each retry. Default is 512 milliseconds; -1 disables backoff."`
DialTimeout time.Duration `env:"DIAL_TIMEOUT" comment:"Dial timeout for establishing new connections. Default is 5 seconds."`
ReadTimeout time.Duration `` /* 190-byte string literal not displayed */
WriteTimeout time.Duration `` /* 148-byte string literal not displayed */
PoolSize int `` /* 134-byte string literal not displayed */
MinIdleConns int `env:"MIN_IDLE_CONNS" comment:"Minimum number of idle connections which is useful when establishing new connection is slow."`
PoolTimeout time.Duration `` /* 161-byte string literal not displayed */
// TODO: need to implement the ability to install tls
// TLS Config to use. When set TLS will be negotiated.
TLSConfig *tls.Config
}
type Plugin ¶
type Plugin interface {
DB() *redis.Client
ClusterDB() *redis.ClusterClient
Client() redis.Cmdable // Add this method for compatibility with caching system
Print()
}
func NewTestPlugin ¶
func NewTestPlugin(ctx context.Context, cfg TestConfig) (Plugin, error)
NewTestPlugin creates a plugin instance configured for testing
type RedisContainer ¶
type RedisContainer interface {
GetConnectionString(ctx context.Context) (string, error)
Close(ctx context.Context) error
}
RedisContainer defines interface for Redis container management
type TestConfig ¶
type TestConfig struct {
Config
// RunContainer indicates whether to start a test container
RunContainer bool
// ContainerImage specifies custom Redis image for test container
ContainerImage string
// ContainerName sets custom name for test container
ContainerName string
// ConnAttempts sets number of connection attempts
ConnAttempts int
// ConnTimeout sets connection timeout
ConnTimeout time.Duration
}
TestConfig extends Config with additional testing-specific options
Click to show internal directories.
Click to hide internal directories.