Cache
Key-value caching with memory, file, and Redis drivers.
RapidGo provides a unified caching interface with three storage backends.
Configuration
CACHE_DRIVER=memory # memory, file, redis
CACHE_PREFIX=rapidgo_ # Key prefix (optional)
Cache Store Interface
type Store interface {
Get(key string) (string, error)
Set(key string, value string, ttl time.Duration) error
Delete(key string) error
Flush() error
}
Creating a Cache Store
import "github.com/RAiWorks/RapidGo/v2/core/cache"
// Create from driver name and optional prefix
store, err := cache.NewStore("memory", "myapp_")
Available Drivers
Memory
In-process cache with TTL-based expiry. Fast but not shared across processes.
store, _ := cache.NewStore("memory", "")
File
File-system cache stored in storage/cache/ (configurable via CACHE_FILE_PATH).
store, _ := cache.NewStore("file", "myapp_")
Redis
Shared cache using Redis. Reads REDIS_HOST, REDIS_PORT, REDIS_PASSWORD from environment.
store, _ := cache.NewStore("redis", "myapp_")
Usage Examples
// Set a value with 5-minute TTL
store.Set("user:42:profile", jsonString, 5*time.Minute)
// Get a value
val, err := store.Get("user:42:profile")
if val == "" {
// Cache miss — fetch from database
}
// Delete a key
store.Delete("user:42:profile")
// Clear all cached data
store.Flush()
Key Prefixing
When you pass a prefix to NewStore, all keys are automatically prefixed. This prevents collisions when multiple apps share the same Redis instance:
store, _ := cache.NewStore("redis", "myapp_")
store.Set("users", data, time.Hour)
// Actual Redis key: "myapp_users"