Skip to content

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=null

Docker Setup

Redis 7 is included in the Docker Compose stack:

yaml
redis:
    image: 'redis:7'
    ports:
        - '${FORWARD_REDIS_PORT:-6379}:6379'
    networks:
        - sail

Cache Drivers

DriverUse CaseEnvironment
databaseSimple caching, no Redis neededDevelopment
redisFast caching, production-readyProduction
arrayIn-memory, per-request onlyTesting

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

DataTTLWhy
Reference/lookup data (payment methods, PO statuses, suppliers)1 hour+Rarely changes, queried frequently
Branch settings30 minutesChanges infrequently
Enum values1 hourStatic within a deployment
Dashboard statistics5 minutesExpensive 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=redis

This keeps the database free from session/queue overhead and improves performance.

CPR - Clinical Patient Records