Cache

Configuration

Config structs, YAML examples, and option helpers for Cache

YAML Configuration Example

config.yaml
extensions:
  cache:
    driver: "inmemory"          # "inmemory", "redis", or "memcached"
    url: ""                     # connection string (redis/memcached only)
    defaultTTL: "5m"            # default expiration for entries
    maxSize: 10000              # max items in cache (inmemory only)
    cleanupInterval: "1m"       # expired entry cleanup interval (inmemory only)
    maxKeySize: 250             # max key size in bytes
    maxValueSize: 1048576       # max value size in bytes (1MB)
    prefix: ""                  # key namespace prefix
    connectionPoolSize: 10      # connection pool size (redis/memcached)
    connectionTimeout: "5s"
    readTimeout: "3s"
    writeTimeout: "3s"

The extension loads config from extensions.cache first, falling back to cache for backward compatibility.

Programmatic Configuration

ext := cache.NewExtension(
    cache.WithDriver("inmemory"),
    cache.WithDefaultTTL(10 * time.Minute),
    cache.WithMaxSize(50000),
    cache.WithPrefix("myapp:"),
)

Or provide a complete config:

ext := cache.NewExtensionWithConfig(cache.Config{
    Driver:     "redis",
    URL:        "redis://localhost:6379/0",
    DefaultTTL: 5 * time.Minute,
    Prefix:     "myapp:",
})

Config Struct Reference

Config

Source: config.go

FieldTypeDefaultDescription
Driverstring"inmemory"Cache backend: "inmemory", "redis", or "memcached"
URLstring""Connection string. Required for redis and memcached drivers
DefaultTTLtime.Duration5mDefault TTL applied when Set() is called with ttl == 0
MaxSizeint10000Maximum number of items (in-memory only). 0 means unlimited
CleanupIntervaltime.Duration1mHow often to sweep expired items (in-memory only)
MaxKeySizeint250Maximum key size in bytes
MaxValueSizeint1048576Maximum value size in bytes (1 MB)
Prefixstring""Prepended to all keys for namespacing
ConnectionPoolSizeint10Number of pooled connections (redis/memcached)
ConnectionTimeouttime.Duration5sTimeout for establishing a connection
ReadTimeouttime.Duration3sTimeout for read operations
WriteTimeouttime.Duration3sTimeout for write operations
RequireConfigboolfalseIf true, the extension fails to start when config is missing from ConfigManager

Validation

Config.Validate() checks:

  • Driver is one of inmemory, redis, memcached
  • URL is non-empty when driver is not inmemory
  • DefaultTTL, MaxKeySize, and MaxValueSize are non-negative

Option Helpers

FunctionDescription
WithDriver(driver string)Set the cache backend
WithURL(url string)Set the connection string
WithDefaultTTL(ttl time.Duration)Set the default TTL
WithMaxSize(size int)Set maximum item count (in-memory)
WithPrefix(prefix string)Set the key namespace prefix
WithConnectionPoolSize(size int)Set connection pool size
WithRequireConfig(require bool)Require config from ConfigManager
WithConfig(config Config)Provide a complete config object

How is this guide?

On this page