Cache Backends
Nimbus supports five cache drivers, all sharing the same cache.Store interface. Choose based on your deployment: single process, multi-instance, or edge.
Store Interface
type Store interface {
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Has(ctx context.Context, key string) bool
Flush(ctx context.Context) error
}
Memory
In-process, zero config. Default driver. Data lost on restart.
CACHE_DRIVER=memory
store := cache.NewMemoryStore()
store.Set(ctx, "user:1", data, 5*time.Minute)
val, _ := store.Get(ctx, "user:1")
- Automatic cleanup of expired entries
- Best for: development, single-instance apps, testing
Redis
Distributed, shared across instances. Supports prefix-based invalidation and namespaces.
CACHE_DRIVER=redis
REDIS_URL=redis://localhost:6379
import "github.com/CodeSyncr/nimbus/redis"
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
store := cache.NewRedisStore(rdb)
// With custom prefix
store := cache.NewRedisStoreWithPrefix(rdb, "myapp:")
- Native TTL via Redis SETEX
- Best for: production multi-instance deployments
Memcached
Distributed, multi-server. Key limit 250 bytes, value limit 1MB.
CACHE_DRIVER=memcached
MEMCACHED_SERVERS=localhost:11211,10.0.0.2:11211
store := cache.NewMemcachedStore("localhost:11211", "10.0.0.2:11211")
- Multi-server sharding built-in
- Best for: high-throughput read-heavy workloads
DynamoDB
Serverless, AWS-native. Create a DynamoDB table with partition key pk (string) and optional TTL attribute ttl.
CACHE_DRIVER=dynamodb
CACHE_DYNAMO_TABLE=nimbus-cache
AWS_REGION=us-east-1
store := cache.NewDynamoDBStore(cache.DynamoConfig{
Table: "nimbus-cache",
Region: "us-east-1",
})
- Auto-expiry via DynamoDB TTL (enable on
ttlattribute) - Best for: serverless/Lambda, AWS-native architectures
Cloudflare KV
Edge-distributed, eventually consistent. Minimum TTL 60 seconds.
CACHE_DRIVER=cloudflare
CLOUDFLARE_ACCOUNT_ID=your_account_id
CLOUDFLARE_NAMESPACE_ID=your_namespace_id
CLOUDFLARE_API_TOKEN=your_api_token
store := cache.NewCloudflareKVStore(cache.CloudflareConfig{
AccountID: os.Getenv("CLOUDFLARE_ACCOUNT_ID"),
NamespaceID: os.Getenv("CLOUDFLARE_NAMESPACE_ID"),
APIToken: os.Getenv("CLOUDFLARE_API_TOKEN"),
})
- Global edge distribution, low-latency reads
- Best for: CDN-cached content, edge functions
Comparison
| Driver | Distributed | Persistent | TTL | Setup |
|---|---|---|---|---|
| Memory | โ | โ | โ | None |
| Redis | โ | โ | โ | Redis server |
| Memcached | โ | โ | โ | Memcached server |
| DynamoDB | โ | โ | โ | AWS table |
| Cloudflare KV | โ (edge) | โ | โ (60s min) | CF namespace |
Namespaces
Use cache.Namespace to prefix keys and invalidate groups:
ns := cache.NewNamespace(store, "users")
ns.Set(ctx, "profile:1", data, time.Hour) // key: "users:profile:1"
ns.Flush(ctx) // invalidates all "users:*" keys
Boot (Auto-config)
Use cache.Boot() to auto-select the driver from environment variables:
store := cache.Boot() // reads CACHE_DRIVER env var