# Redis

All keys are prefixed `pt:{APP_ENV}:` (`REDIS_PREFIX`). Production uses phpredis;
local uses predis (`REDIS_CLIENT=predis`).

## Databases

| DB | Connection (`config/database.php`) | Used by |
| --- | --- | --- |
| 0 | `default` | health check ping, misc |
| 1 | `cache` | cache store `redis` (`CACHE_STORE=redis`), store prefix `cache:` |
| 2 | `sessions` | `SESSION_DRIVER=redis` (Next.js CMS cookie sessions) |
| 3 | `queues` | queue connection `redis` + Horizon metadata (`pt:{env}:horizon:`) |
| 4 | `locks` | cache store `locks` (prefix `locks:`): atomic locks + `RateLimiter` (`cache.limiter`) |
| 5 | `counters` | story view counters: `views:h:{YmdH}`, `views:d:{Ymd}` (sorted sets), `views:seen:{story}:{sha256}` (30 min de-dupe) |

## Key patterns

Full key = `pt:{env}:` + store prefix + key.

| Key (after prefix) | Store | TTL | Purpose |
| --- | --- | --- | --- |
| `cache:auth:2fa:challenge:{sha256(token)}` | cache | 5 min | pending TOTP login challenge `{user_id, method, attempts}` |
| `cache:2fa:last:{user_id}` | cache | 3 min | last accepted TOTP timestamp (replay protection) |
| `cache:2fa:recent:{user_id}:{sha256(context)}` | cache | 15 min | "2FA confirmed recently" for a token/session (dangerous actions) |
| `locks:lock:audit:chain` | locks | 10 s | serialises audit hash-chain appends |
| `locks:lock:story:{id}:edit` | locks | 5 min + heartbeat | story edit lock (Prompt 3+, via `CacheService::lockHandle`) |
| `locks:lock:wf:{story_id}` | locks | short | workflow transition lock (later) |
| `locks:{md5(limiter+key)}` | locks | window | `throttle:*` named limiters (see security.md) |
| `locks:login-lock:{user_id}` | locks | 15 min | failed-login counter → account lockout after 5 |
| `cache:public:story:{sha256(slug\|uid)}` | cache (tags `stories`, `public`) | 60 s | published story detail |
| `cache:public:feed:{query}:{cursor}`, `public:latest:*`, `public:ids:*`, `public:province:*` | cache (tags `stories`, `public`) | 30–300 s | public lists |
| `cache:public:home` | cache (flexible; tags `stories`, `public`, `home`, `site:*`, `modules`) | 30 s fresh / 2 min stale | resolved homepage |
| `cache:site:menu:{key}`, `site:layout:active:{page}`, `site:settings:public`, `site:page:{slug}`, `site:team`, `site:breaking:live` | cache (tags `site:*`) | 30 s–1 h | site structure |
| `cache:site:redirect:{sha256(path)}` | cache (tag `site:redirects`) | 1 h | redirect rule lookup |
| `cache:preview:{sha256(token)}` | cache | 1 h | story preview token → uid |
| `cache:public:xml:*` | cache (tags `stories`, `public`) | 10 min | sitemaps and RSS |
| `cache:workflow:public-status-ids` | cache (tag `workflow`) | 1 h | public workflow status ids |
| `views:h:{YmdH}`, `views:d:{Ymd}`, `views:seen:{story}:{sha256(visitor)}` | counters | 48 h / 35 d / 30 min | trending, most read, de-dupe |
| `an:events` | counters | stream max length 1,000,000 | pseudonymous events pending MySQL ingestion; consumed entries deleted |
| `an:m:{YmdHi}`, `an:u:{YmdHi}` | counters | 3 h | UTC minute event hashes and active-reader HyperLogLogs |
| `an:seen:p:{sha1(path)}:{reader_hash}` | counters | 30 min | anonymous page-view dedupe; story views share `views:seen:*` |
| `locks:lock:analytics:consume` | locks | 120 s | serializes event stream draining |
| `cache:analytics:*`, `cache:analytics:public-counts:{story}` | cache (tag `analytics`) | 60 s / 300 s | reports and public totals, invalidated by rollups |
| `cache:scorecards:*`, `cache:scorecards:weights`, `cache:scorecards:peers:*` | cache (tags `scorecards`, `scorecards:user:{id}`) | 600 s / 1 h / 600 s | cards, credibility weights and newsroom percentiles |
| `ads:imp:{Ymd}:{campaign}:{creative}:{zone}`, `ads:clk:…` | counters | 3 d | ad impressions / clicks (flushed to `ad_daily_stats`) |
| `ads:day:{Ymd}:{campaign}`, `ads:freq:{campaign}:{sha256(visitor)}` | counters | 2 d / cap window | daily cap + pacing, frequency cap |
| `cache:radio:*`, `video:*`, `epaper:*`, `horoscope:*`, `market:*`, `ads:zone:*`, `ads:running:*` | cache (tags `radio`, `video`, `epaper`, `horoscope`, `market`, `ads`) | 30 s–1 d | Prompt 6 reads |
| `cache:weather:{district}`, `cache:weather:coords:{district}` | cache (tags `weather`, `weather-coords`) | 30 min / forever | weather + geocoded coordinates |
| `locks:otp-send:{target_hash}` | locks | 10 min | OTP sends per target (max 3) |
| `cache:perm:user:{id}` | cache | ≤ 1 hour / next override expiry | Effective access snapshot; tags `permissions`, `permissions:user:{id}` |
| `cache:perm:catalogue` | cache | 1 hour | Permission danger metadata, tag `permissions` |
| `cache:geo:*` | cache | forever | Location lists and scope expansion, tag `geo` |
| `cache:calendar:bs-months` | cache | forever | Calendar month lengths, tag `calendar` |
| `cache:modules:all` | cache | forever | Module registry, tag `modules` |
| `cache:spatie.permission.cache` | cache | 24 hours | Spatie catalogue via Redis; invalidated on mutations |
| `cache:tag:*` / tagged entries | cache | varies | `CacheService::remember/flexible` with tags; `invalidate([tags])` flushes |

Rules: services use `App\Support\Cache\CacheService` (never `Cache::` directly); hot public
data uses `flexible()` (stale-while-revalidate); invalidate tags from event listeners.

## Taxonomy (Prompt 3A)

## Workflow configuration (Prompt 4A)

- Cache DB 1: `workflow:templates`, `workflow:settings`, tag `workflow`, TTL 3600s.
  Both are invalidated after committed configuration changes.
- Locks DB 4: `lock:workflow:config`, TTL 60s, serializes graph/default and setting
  version changes. Optimistic `base_version` checks reject stale edits.

- Cache DB 1: `taxonomy:categories:catalogue`, tag `taxonomy:categories`, TTL 3600s.
  Purged immediately by the after-commit `TaxonomyChanged` event listener.
- Locks DB 4: `lock:taxonomy:write`, TTL 60s, serializes taxonomy mutations and tag
  merges. Hierarchy checks use fresh rows inside a database transaction.

