Forge
1.x
Docs/Forge/Functions
Open

Reading2 min
Updated31 Jul 2026
Sourcev1/extensions/cache/functions.mdx

Extension Entry Points01

FunctionDescription
NewExtension(opts ...ConfigOption) forge.ExtensionCreate the cache extension with functional options
NewExtensionWithConfig(config Config) forge.ExtensionCreate the cache extension with a complete config struct
DefaultConfig() ConfigReturns the default configuration with sensible defaults

DI Helpers02

Resolve the cache service from the container or app:

FunctionDescription
GetCache(c forge.Container) (Cache, error)Resolve the Cache from the DI container
MustGetCache(c forge.Container) CacheSame as GetCache but panics on error
GetCacheFromApp(app forge.App) (Cache, error)Resolve the Cache from an app instance
MustGetCacheFromApp(app forge.App) CacheSame as GetCacheFromApp but panics on error

Cache Interface03

The core interface implemented by all backends:

MethodDescription
Connect(ctx) errorOpen the connection to the backend
Disconnect(ctx) errorClose the connection and release resources
Ping(ctx) errorVerify the backend is reachable
Get(ctx, key) ([]byte, error)Retrieve a value by key. Returns ErrNotFound if missing or expired
Set(ctx, key, value, ttl) errorStore a value with the given TTL. Uses DefaultTTL when ttl == 0
Delete(ctx, key) errorRemove a key from the cache
Exists(ctx, key) (bool, error)Check whether a key exists and is not expired
Clear(ctx) errorRemove all entries from the cache
Keys(ctx, pattern) ([]string, error)List keys matching a glob pattern (e.g. user:*)
TTL(ctx, key) (time.Duration, error)Get remaining time-to-live for a key
Expire(ctx, key, ttl) errorUpdate the expiration on an existing key. ttl <= 0 removes expiration

StringCache Interface04

Convenience methods for string values (implemented by in-memory backend):

MethodDescription
GetString(ctx, key) (string, error)Get a value as a string
SetString(ctx, key, value, ttl) errorStore a string value

JSONCache Interface05

JSON serialization helpers (implemented by in-memory backend):

MethodDescription
GetJSON(ctx, key, target) errorDeserialize a cached value into target
SetJSON(ctx, key, value, ttl) errorSerialize value as JSON and store it

CounterCache Interface06

Atomic counter operations (implemented by in-memory backend):

MethodDescription
Incr(ctx, key) (int64, error)Increment a counter by 1
IncrBy(ctx, key, value) (int64, error)Increment a counter by a given amount
Decr(ctx, key) (int64, error)Decrement a counter by 1
DecrBy(ctx, key, value) (int64, error)Decrement a counter by a given amount

MultiCache Interface07

Batch operations (implemented by in-memory backend):

MethodDescription
MGet(ctx, keys...) ([][]byte, error)Retrieve multiple values in one call
MSet(ctx, kvs, ttl) errorStore multiple key-value pairs with a shared TTL
MDelete(ctx, keys...) errorDelete multiple keys in one call

CacheService Methods08

The CacheService wraps a backend and implements the Vessel service lifecycle:

MethodDescription
Cache() CacheAccess the underlying Cache backend directly
Name() stringReturns "cache-service" (used by Vessel)