Documentation
¶
Index ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) CompareAndSet(ctx context.Context, key string, expectedValue, newValue []byte, ...) (bool, error)
- func (c *Client) Delete(ctx context.Context, key string) error
- func (c *Client) Get(ctx context.Context, key string) ([]byte, error)
- func (c *Client) GetOrSet(ctx context.Context, key string, value []byte, ttl time.Duration) (storedValue []byte, wasSet bool, err error)
- func (c *Client) Health(ctx context.Context) error
- func (c *Client) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *Client) Stats() (map[string]any, error)
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements the cache.Cache interface using Redis as the backend.
func NewClient ¶
NewClient creates a new Redis cache client. Validates configuration and establishes connection.
func (*Client) Close ¶
Close closes the Redis client and releases resources. After calling Close, the client should not be used. Close is idempotent - calling it multiple times is safe.
func (*Client) CompareAndSet ¶
func (c *Client) CompareAndSet(ctx context.Context, key string, expectedValue, newValue []byte, ttl time.Duration) (bool, error)
CompareAndSet atomically compares and swaps a value. Returns (success, error):
- success=true: Value was updated (comparison matched)
- success=false: Value was NOT updated (comparison failed)
Special case: expectedValue=nil means "set only if key doesn't exist" (acquire lock). Uses Lua script for atomicity.
func (*Client) Delete ¶
Delete removes a key from the cache. Does not return error if key doesn't exist.
func (*Client) Get ¶
Get retrieves a value from the cache. Returns cache.ErrNotFound if the key doesn't exist.
func (*Client) GetOrSet ¶
func (c *Client) GetOrSet(ctx context.Context, key string, value []byte, ttl time.Duration) (storedValue []byte, wasSet bool, err error)
GetOrSet atomically gets an existing value or sets a new one. Returns (storedValue, wasSet, error):
- wasSet=true: Value was newly set (first-time processing)
- wasSet=false: Value already existed (duplicate detected)
- storedValue: Always returns the value in cache (current or newly set)
Uses Redis SET NX GET for atomicity.
func (*Client) Health ¶
Health checks if the Redis connection is healthy. Uses PING command to verify connectivity.
type Config ¶
type Config struct {
// Host is the Redis server hostname or IP address.
Host string `config:"host" required:"true"`
// Port is the Redis server port (default: 6379).
Port int `config:"port" default:"6379"`
// Password for Redis authentication (optional).
// Should be provided via environment variable: CACHE_REDIS_PASSWORD
Password string `config:"password"`
// Database number to use (default: 0).
// Redis supports databases 0-15 by default.
Database int `config:"database" default:"0"`
// PoolSize is the maximum number of socket connections (default: 10).
// Higher values allow more concurrent operations but consume more resources.
PoolSize int `config:"pool_size" default:"10"`
// DialTimeout is the timeout for establishing new connections (default: 5s).
DialTimeout time.Duration `config:"dial_timeout" default:"5s"`
// ReadTimeout is the timeout for socket reads (default: 3s).
// -1 disables timeout.
ReadTimeout time.Duration `config:"read_timeout" default:"3s"`
// WriteTimeout is the timeout for socket writes (default: 3s).
// -1 disables timeout.
WriteTimeout time.Duration `config:"write_timeout" default:"3s"`
// MaxRetries is the maximum number of retries before giving up (default: 3).
// -1 disables retries.
MaxRetries int `config:"max_retries" default:"3"`
// MinRetryBackoff is the minimum backoff between retries (default: 8ms).
MinRetryBackoff time.Duration `config:"min_retry_backoff" default:"8ms"`
// MaxRetryBackoff is the maximum backoff between retries (default: 512ms).
MaxRetryBackoff time.Duration `config:"max_retry_backoff" default:"512ms"`
}
Config holds Redis-specific configuration options.