Multi-Driver Cache (pkg/cache)
Import path: github.com/krewire/krewire/pkg/cache
The pkg/cache library delivers a unified key-value caching abstraction with support for TTL expiration, LRU eviction bounds, background cleanup sweeps, and the atomic Remember pattern across the Krewire ecosystem.
1. Core Interface
package cache
import (
"context"
"time"
)
type Cache interface {
Get(ctx context.Context, key string) ([]byte, error)
GetString(ctx context.Context, key string) (string, error)
Set(ctx context.Context, key string, val []byte, ttl time.Duration) error
SetString(ctx context.Context, key string, val string, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Has(ctx context.Context, key string) (bool, error)
Clear(ctx context.Context) error
Remember(ctx context.Context, key string, ttl time.Duration, fn func() ([]byte, error)) ([]byte, error)
Close() error
}
type Driver = Cache
2. Pluggable Cache Drivers
- In-Memory Cache (
cache.NewMemoryCache):
High-speed in-process cache with mutex synchronization, configurable capacity (default 10,000 items), and LRU eviction.
memCache := cache.NewMemoryCache(1 * time.Minute)
defer memCache.Close()
- Redis / Valkey / Dragonfly (
cache.NewRedisCache):
Pure-Go RESP protocol client with automatic connection pooling and reconnect logic:
redisCache, err := cache.NewRedisCache(cache.RedisConfig{
Address: "127.0.0.1:6379",
Password: os.Getenv("REDIS_PASSWORD"),
DB: 0,
})
- Filesystem Cache (
cache.NewFileCache):
Persistent disk-backed cache with atomic file writes and periodic cleanup sweeps.
fileCache, err := cache.NewFileCache("/tmp/krewire-cache", 10*time.Minute)
- Memcached (
cache.NewMemcachedCache):
Pure-Go Memcached driver supporting multi-server clusters.
3. Usage Example & Remember Pattern
The Remember pattern transparently retrieves cached items or executes a fallback resolver on cache miss:
package main
import (
"context"
"fmt"
"time"
"github.com/krewire/krewire/pkg/cache"
)
func GetUserProfile(ctx context.Context, c cache.Cache, userID string) ([]byte, error) {
key := fmt.Sprintf("users:%s", userID)
return c.Remember(ctx, key, 30*time.Minute, func() ([]byte, error) {
// Executed only on cache miss: fetch from primary database
return FetchUserFromDatabase(userID)
})
}
4. Alignment with the Three Pillars (3S)
- Secure (S1):
FileCacheenforces restricted file permissions (0600) and rejects path traversal attempts. - Sustainable (S2): Pure Go without CGO or heavy external Redis client dependencies.
- Scalable (S3): Seamlessly transition from
MemoryCacheon a single VPS toRedisCacheorMemcachedin distributed clusters without rewriting business logic.