Cache

Functions

Public API reference for the Cache extension

Extension Entry Points

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 Helpers

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 Interface

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 Interface

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 Interface

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 Interface

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 Interface

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 Methods

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)

How is this guide?

On this page