Documentation
¶
Index ¶
- Variables
- func Clear(ctx context.Context) error
- func Delete(ctx context.Context, key string) error
- func Exists(ctx context.Context, key string) (bool, error)
- func Get(ctx context.Context, key string) ([]byte, error)
- func Health(ctx context.Context) error
- func Init(configs ...Config) error
- func InitFromEnv() error
- func IsHealthy() bool
- func MustInit(configs ...Config)
- func Ping(ctx context.Context) error
- func Reset()
- func Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func Shutdown(ctx context.Context) error
- type Builder
- type Cache
- type Config
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotInitialized = errors.New("cache not initialized") ErrInvalidDriver = errors.New("invalid cache driver") ErrInvalidConfig = errors.New("invalid cache configuration") ErrKeyNotFound = errors.New("key not found") ErrInvalidTTL = errors.New("invalid TTL value") )
Common errors
Functions ¶
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a way to create cache instances with custom prefixes
func WithPrefix ¶
WithPrefix creates a new Builder with the specified prefix
type Cache ¶
type Cache interface {
// Get retrieves a value by key
Get(ctx context.Context, key string) ([]byte, error)
// Set stores a value with optional TTL
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete removes a key
Delete(ctx context.Context, key string) error
// Exists checks if a key exists
Exists(ctx context.Context, key string) (bool, error)
// Clear removes all keys
Clear(ctx context.Context) error
// Close closes the cache connection
Close() error
// Ping checks if cache is reachable
Ping(ctx context.Context) error
}
Cache defines the interface for cache implementations
func NewFromEnv ¶
NewFromEnv creates cache instance from environment variables
type Config ¶
type Config struct {
// Driver specifies cache backend: "memory" or "redis"
Driver string `env:"CACHE_DRIVER,default:memory"`
// Redis specific settings
Host string `env:"CACHE_HOST,default:localhost"`
Port string `env:"CACHE_PORT,default:6379"`
Password string `env:"CACHE_PASSWORD"`
Database int `env:"CACHE_DATABASE,default:0"`
// Connection URL (overrides host/port/password)
URL string `env:"CACHE_URL"`
// Connection pool settings
MaxRetries int `env:"CACHE_MAX_RETRIES,default:3"`
PoolSize int `env:"CACHE_POOL_SIZE,default:10"`
MinIdleConns int `env:"CACHE_MIN_IDLE_CONNS,default:2"`
MaxIdleConns int `env:"CACHE_MAX_IDLE_CONNS,default:5"`
ConnMaxLifetime int `env:"CACHE_CONN_MAX_LIFETIME,default:0"` // seconds
ConnMaxIdleTime int `env:"CACHE_CONN_MAX_IDLE_TIME,default:0"` // seconds
// Memory cache specific
MaxSize int64 `env:"CACHE_MAX_SIZE,default:0"` // max memory in bytes
MaxKeys int `env:"CACHE_MAX_KEYS,default:0"` // max number of keys
DefaultTTL string `env:"CACHE_DEFAULT_TTL,default:0"` // default TTL as duration string
CleanupInterval string `env:"CACHE_CLEANUP_INTERVAL,default:1m"` // cleanup interval as duration string
// TLS settings for Redis
UseTLS bool `env:"CACHE_USE_TLS,default:false"`
CertFile string `env:"CACHE_CERT_FILE"`
KeyFile string `env:"CACHE_KEY_FILE"`
CAFile string `env:"CACHE_CA_FILE"`
// Common settings
KeyPrefix string `env:"CACHE_KEY_PREFIX"` // prefix for all keys
Namespace string `env:"CACHE_NAMESPACE"` // namespace for isolation
}
Config holds cache configuration
func (Config) ParsedCleanupInterval ¶
ParsedCleanupInterval returns the cleanup interval as a time.Duration
func (Config) ParsedDefaultTTL ¶
ParsedDefaultTTL returns the default TTL as a time.Duration
Click to show internal directories.
Click to hide internal directories.