Cache
Kit provides a Laravel-inspired Cache facade for storing and retrieving data with optional TTL (time-to-live) expiration. The cache automatically uses Redis when available, falling back to an in-memory cache when Redis is unavailable.Overview
The cache system is automatically initialized when your server starts - no manual setup required. It tries to connect to Redis first, and if Redis isn’t available, it seamlessly falls back to an in-memory cache.Quick Start
Configuration
The cache uses environment variables for configuration. All are optional with sensible defaults.| Variable | Description | Default |
|---|---|---|
REDIS_URL | Redis connection URL | redis://127.0.0.1:6379 |
REDIS_PREFIX | Prefix for all cache keys | kit_cache: |
CACHE_DEFAULT_TTL | Default TTL in seconds (0 = no expiration) | 3600 |
Example .env
Basic Operations
Storing Items
Retrieving Items
Removing Items
The Remember Pattern
Theremember method retrieves an item from the cache, or stores a default value if it doesn’t exist:
Atomic Counters
Increment and decrement numeric values atomically:Testing
In tests, you can use the in-memory cache implementation directly:Type Safety
The cache works with any type that implementsSerialize and Deserialize:
Redis vs In-Memory
| Feature | Redis | In-Memory |
|---|---|---|
| Persistence | Yes (if configured) | No |
| Shared across processes | Yes | No |
| TTL support | Yes | Yes |
| Atomic operations | Yes | Yes |
| Default | When REDIS_URL is accessible | Fallback |
- Redis: When
REDIS_URLenvironment variable is set and Redis is accessible - In-Memory: When Redis is not available (development, testing, or Redis failure)