Documentation
¶
Overview ¶
Package redis provides Redis caching integration for Bifrost plugin. This plugin caches request body hashes using xxhash and returns cached responses for identical requests. It supports configurable caching behavior including success-only caching and custom cache key generation.
Index ¶
- Constants
- func NewRedisPlugin(config RedisPluginConfig, logger schemas.Logger) (schemas.Plugin, error)
- type ContextKey
- type Plugin
- func (plugin *Plugin) Cleanup() error
- func (plugin *Plugin) ClearCacheForKey(key string) error
- func (p *Plugin) GetName() string
- func (plugin *Plugin) PostHook(ctx *context.Context, res *schemas.BifrostResponse, ...) (*schemas.BifrostResponse, *schemas.BifrostError, error)
- func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.PluginShortCircuit, error)
- type RedisPluginConfig
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func NewRedisPlugin ¶
NewRedisPlugin creates a new Redis plugin instance with the provided configuration. It establishes a connection to Redis, tests connectivity, and returns a configured plugin.
All Redis client options are passed directly to the Redis client, which handles its own defaults. The plugin only sets defaults for its own behavior (TTL, CacheOnlySuccessful, etc.).
Parameters:
- config: Redis and plugin configuration (only Addr is required)
- logger: Logger instance for the plugin
Returns:
- schemas.Plugin: A configured Redis plugin instance
- error: Any error that occurred during plugin initialization or Redis connection
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey is a custom type for context keys to prevent key collisions
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin implements the schemas.Plugin interface for Redis caching. It caches responses based on xxhash of normalized requests and returns cached responses for identical requests. The plugin supports configurable caching behavior including success-only caching and custom cache key generation.
Fields:
- client: Redis client instance for cache operations
- config: Plugin configuration including Redis and caching settings
- logger: Logger instance for plugin operations
func (*Plugin) Cleanup ¶
Cleanup performs cleanup operations for the Redis plugin. It removes all cached entries with the configured prefix and closes the Redis connection.
The function performs the following operations: 1. Retrieves all cache keys matching the configured prefix pattern 2. Deletes all matching cache entries from Redis 3. Closes the Redis client connection
This method should be called when shutting down the application to ensure proper resource cleanup and prevent connection leaks.
Returns:
- error: Any error that occurred during cleanup operations
func (*Plugin) ClearCacheForKey ¶
ClearCacheForKey deletes a specific cache key from Redis. It is used to clear a specific cache key when needed.
Parameters:
- key: The cache key to delete
Returns:
- error: Any error that occurred during cache key deletion
func (*Plugin) GetName ¶
GetName returns the canonical name of the Redis plugin. This name is used for plugin identification and logging purposes.
Returns:
- string: The plugin name "bifrost-redis"
func (*Plugin) PostHook ¶
func (plugin *Plugin) PostHook(ctx *context.Context, res *schemas.BifrostResponse, bifrostErr *schemas.BifrostError) (*schemas.BifrostResponse, *schemas.BifrostError, error)
PostHook is called after a response is received from a provider. It caches the response using the request hash as the key, with optional filtering based on the CacheOnlySuccessful configuration.
The function performs the following operations: 1. Checks if CacheOnlySuccessful is enabled and skips caching for unsuccessful responses 2. Retrieves the request hash from the context (set during PreHook) 3. Marshals the response for storage 4. Stores the response in Redis asynchronously (non-blocking)
The Redis SET operation runs in a separate goroutine to avoid blocking the response. The function gracefully handles errors and continues without caching if any step fails, ensuring that response processing is never interrupted by caching issues.
Parameters:
- ctx: Pointer to the context.Context containing the request hash
- res: The response from the provider to be cached
- bifrostErr: The error from the provider, if any (used for success determination)
Returns:
- *schemas.BifrostResponse: The original response, unmodified
- *schemas.BifrostError: The original error, unmodified
- error: Any error that occurred during caching preparation (always nil as errors are handled gracefully)
func (*Plugin) PreHook ¶
func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.PluginShortCircuit, error)
PreHook is called before a request is processed by Bifrost. It checks if a cached response exists for the request hash and returns it if found.
Parameters:
- ctx: Pointer to the context.Context
- req: The incoming Bifrost request
Returns:
- *schemas.BifrostRequest: The original request
- *schemas.BifrostResponse: Cached response if found, nil otherwise
- error: Any error that occurred during cache lookup
type RedisPluginConfig ¶
type RedisPluginConfig struct { // Connection settings Addr string `json:"addr"` // Redis server address (host:port) - REQUIRED Username string `json:"username,omitempty"` // Username for Redis AUTH (optional) Password string `json:"password,omitempty"` // Password for Redis AUTH (optional) DB int `json:"db,omitempty"` // Redis database number (default: 0) CacheKey string `json:"cache_key"` // Cache key for context lookup - REQUIRED CacheTTLKey string `json:"cache_ttl_key"` // Cache TTL key for context lookup (optional) // Connection pool and timeout settings (passed directly to Redis client) PoolSize int `json:"pool_size,omitempty"` // Maximum number of socket connections (optional) MinIdleConns int `json:"min_idle_conns,omitempty"` // Minimum number of idle connections (optional) MaxIdleConns int `json:"max_idle_conns,omitempty"` // Maximum number of idle connections (optional) ConnMaxLifetime time.Duration `json:"conn_max_lifetime,omitempty"` // Connection maximum lifetime (optional) ConnMaxIdleTime time.Duration `json:"conn_max_idle_time,omitempty"` // Connection maximum idle time (optional) DialTimeout time.Duration `json:"dial_timeout,omitempty"` // Timeout for socket connection (optional) ReadTimeout time.Duration `json:"read_timeout,omitempty"` // Timeout for socket reads (optional) WriteTimeout time.Duration `json:"write_timeout,omitempty"` // Timeout for socket writes (optional) ContextTimeout time.Duration `json:"context_timeout,omitempty"` // Timeout for Redis operations (optional) // Plugin behavior settings TTL time.Duration `json:"ttl,omitempty"` // Time-to-live for cached responses (default: 5min) Prefix string `json:"prefix,omitempty"` // Prefix for cache keys (optional) // Advanced caching behavior CacheByModel *bool `json:"cache_by_model,omitempty"` // Include model in cache key (default: true) CacheByProvider *bool `json:"cache_by_provider,omitempty"` // Include provider in cache key (default: true) }
RedisPluginConfig contains configuration for the Redis plugin. All Redis client options are passed directly to the Redis client, which handles its own defaults. Only specify values you want to override from Redis client defaults.