Caching
The CPR backend supports Redis for caching, session management, and queue processing.
Cache Configuration
Environment Variables
env
# Development (default)
CACHE_STORE=database
# Production (recommended)
CACHE_STORE=redis
CACHE_PREFIX=cpr_api
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=nullDocker Setup
Redis 7 is included in the Docker Compose stack:
yaml
redis:
image: 'redis:7'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
networks:
- sailCache Drivers
| Driver | Use Case | Environment |
|---|---|---|
database | Simple caching, no Redis needed | Development |
redis | Fast caching, production-ready | Production |
array | In-memory, per-request only | Testing |
Using the Cache
Basic Usage
php
use Illuminate\Support\Facades\Cache;
// Store a value (with TTL in seconds)
Cache::put('key', $value, 3600);
// Retrieve (with default)
$value = Cache::get('key', 'default');
// Remember pattern (cache or compute)
$patients = Cache::remember('patients.list', 3600, function () {
return Patient::all();
});
// Forget
Cache::forget('key');Cache Tags (Redis only)
php
// Tag-based caching for easy invalidation
Cache::tags(['patients'])->put("patient.{$id}", $patient, 3600);
// Flush all patient-related cache
Cache::tags(['patients'])->flush();What to Cache
| Data | TTL | Why |
|---|---|---|
| Reference/lookup data (payment methods, PO statuses, suppliers) | 1 hour+ | Rarely changes, queried frequently |
| Branch settings | 30 minutes | Changes infrequently |
| Enum values | 1 hour | Static within a deployment |
| Dashboard statistics | 5 minutes | Expensive aggregate queries |
What NOT to Cache
- Patient records (frequently updated, privacy-sensitive)
- Queue tickets (real-time state)
- Transaction data (must be current)
- Any data that changes per-request
Session and Queue
In production, sessions and queues also use Redis:
env
SESSION_DRIVER=redis
SESSION_LIFETIME=120
QUEUE_CONNECTION=redisThis keeps the database free from session/queue overhead and improves performance.