{"modulePath":"github.com/Compogo/cache","version":"v0.0.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cache\n\n## Constants\n\n```go\nconst (\n\tDriverFieldName     = \"cache.driver\"\n\tExpirationFieldName = \"cache.expiration\"\n\n\tExpirationDefault = 5 * time.Minute\n)\n```\n\n## Variables\n\n```go\nvar Component = \u0026component.Component{\n\tInit: component.StepFunc(func(container container.Container) error {\n\t\treturn container.Provides(\n\t\t\tNewConfig,\n\t\t\tNewCache,\n\t\t)\n\t}),\n\tBindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error {\n\t\treturn container.Invoke(func(config *Config) {\n\t\t\tallDrivers := drivers.Keys()\n\t\t\tif len(allDrivers) == 1 {\n\t\t\t\tDriverDefault = allDrivers[0]\n\t\t\t}\n\n\t\t\tflagSet.StringVar(\u0026config.driver, DriverFieldName, DriverDefault, fmt.Sprintf(\"cache driver. Available drivers: [%s]\", strings.Join(allDrivers, \",\")))\n\t\t\tflagSet.DurationVar(\u0026config.Expiration, ExpirationFieldName, ExpirationDefault, \"default data retention time\")\n\t\t})\n\t}),\n\tConfiguration: component.StepFunc(func(container container.Container) error {\n\t\treturn container.Invoke(Configuration)\n\t}),\n}\n```\nComponent is a ready-to-use Compogo component that provides a configurable cache. It automatically:\n\n  - Registers Config and Cache factory in the DI container\n  - Adds command-line flags for driver selection and TTL\n  - Validates the selected driver during Configuration phase\n  - Sets the default driver automatically if only one is registered\n\nUsage:\n\n\tcompogo.WithComponents(\n\t    cache.Component,\n\t    // ... driver components (redis, bigcache, etc.)\n\t)\n\nThen in your service:\n\n\ttype Service struct {\n\t    cache cache.CacheInterface[[]byte]\n\t}\n\n```go\nvar DriverDefault = \"\"\n```\n\n## Functions\n\n```go\nfunc NewCache(config *Config, appConfig *compogo.Config, logger logger.Logger, container container.Container) (cache.CacheInterface[[]byte], error)\n```\nNewCache creates a new cache instance with the selected driver. It:\n\n  - Looks up the getter function for the configured driver\n  - Creates the underlying store using the getter\n  - Wraps it with Prometheus metrics (using the app name as a label)\n  - Returns a cache.CacheInterface that can be used throughout the application\n\nThe returned cache is generic over \\[]byte, but gocache's cache.CacheInterface can work with any serializable type through marshaling/unmarshaling.\n\n```go\nfunc Registration(d Driver, getter Getter)\n```\nRegistration registers a new cache driver with its factory function. This should be called from driver packages (e.g., redis, bigcache) during init().\n\nExample:\n\n\tfunc init() {\n\t\tcache.Registration(\"redis\", NewRedisStore)\n\t}\n\n## Types\n\n```go\ntype Config struct {\n\tDriver     Driver\n\tExpiration time.Duration\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc Configuration(config *Config, configurator configurator.Configurator) (*Config, error)\n```\n\n```go\nfunc NewConfig() *Config\n```\n\n```go\ntype Driver string\n```\nDriver is a type-safe identifier for cache backends. It implements fmt.Stringer for logging and display.\n\n```go\nfunc (d Driver) String() string\n```\nString returns the driver name as a string.\n\n```go\ntype Getter func(container container.Container) (store.StoreInterface, error)\n```\nGetter 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.\n\n","path":"github.com/Compogo/cache","name":"cache","synopsis":"","isRedistributable":true}
