Documentation
¶
Index ¶
Constants ¶
const ( DriverFieldName = "cache.driver" ExpirationFieldName = "cache.expiration" ExpirationDefault = 5 * time.Minute )
Variables ¶
var Component = &component.Component{ Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewCache, ) }), BindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error { return container.Invoke(func(config *Config) { allDrivers := drivers.Keys() if len(allDrivers) == 1 { DriverDefault = allDrivers[0] } flagSet.StringVar(&config.driver, DriverFieldName, DriverDefault, fmt.Sprintf("cache driver. Available drivers: [%s]", strings.Join(allDrivers, ","))) flagSet.DurationVar(&config.Expiration, ExpirationFieldName, ExpirationDefault, "default data retention time") }) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), }
Component is a ready-to-use Compogo component that provides a configurable cache. It automatically:
- Registers Config and Cache factory in the DI container
- Adds command-line flags for driver selection and TTL
- Validates the selected driver during Configuration phase
- Sets the default driver automatically if only one is registered
Usage:
compogo.WithComponents(
cache.Component,
// ... driver components (redis, bigcache, etc.)
)
Then in your service:
type Service struct {
cache cache.CacheInterface[[]byte]
}
var DriverDefault = ""
Functions ¶
func NewCache ¶
func NewCache(config *Config, appConfig *compogo.Config, logger logger.Logger, container container.Container) (cache.CacheInterface[[]byte], error)
NewCache creates a new cache instance with the selected driver. It:
- Looks up the getter function for the configured driver
- Creates the underlying store using the getter
- Wraps it with Prometheus metrics (using the app name as a label)
- Returns a cache.CacheInterface that can be used throughout the application
The returned cache is generic over []byte, but gocache's cache.CacheInterface can work with any serializable type through marshaling/unmarshaling.
func Registration ¶
Registration registers a new cache driver with its factory function. This should be called from driver packages (e.g., redis, bigcache) during init().
Example:
func init() {
cache.Registration("redis", NewRedisStore)
}
Types ¶
type Config ¶
type Config struct {
Driver Driver
Expiration time.Duration
// contains filtered or unexported fields
}
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) (*Config, error)
type Driver ¶
type Driver string
Driver is a type-safe identifier for cache backends. It implements fmt.Stringer for logging and display.
type Getter ¶
type Getter func(container container.Container) (store.StoreInterface, error)
Getter is a factory function that creates a cache store from a DI container. It receives the container to resolve any dependencies the store might need (configuration, connections, etc.) and returns a store.StoreInterface.