> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encoreos.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Platform Cache

> Centralized in-memory cache for query results, configuration, and reference data. All keys are tenant-scoped: org-{orgId}:{namespace}:{key}.

**Status:** ✅ Complete\
**Spec:** PF-51 Caching Layer & Strategy

## Purpose

Centralized in-memory cache for query results, configuration, and reference data. All keys are tenant-scoped: `org-{orgId}:{namespace}:{key}`.

## Public API

| Export                       | Type         | Description                                           |
| ---------------------------- | ------------ | ----------------------------------------------------- |
| cacheService                 | CacheService | get, set, delete, clear(pattern), invalidate(pattern) |
| useCachedQuery               | hook         | TanStack Query-backed cache for React                 |
| createCacheKey               | function     | Build full key from orgId, namespace, key             |
| requireOrganizationId        | function     | Throw if missing org                                  |
| warmCache                    | function     | Warm cache for an organization (async)                |
| getCacheStats                | function     | Hit/miss counts and hit rate (programmatic)           |
| invalidateCacheForUser       | function     | Invalidate keys for org-{orgId}:user-{userId}:\*      |
| purgeCacheForDeactivatedUser | function     | Same as invalidateCacheForUser                        |

## Key format

* Full key: `org-{organizationId}:{namespace}:{key}`.
* Organization ID is required; omit only when using helpers that inject it (e.g. useCachedQuery uses current org from context).
* Missing org throws: "Cache requires organization context".

## Usage

```typescript theme={null}
import { cacheService, createCacheKey, useCachedQuery } from '@/platform/cache';

// With explicit org (e.g. in queryFn)
const key = createCacheKey(orgId, 'my-namespace', 'item-1');
await cacheService.set(key, data, { ttl: 300 });
const value = await cacheService.get(key);

// React: useCachedQuery injects org from useOrganization()
const { data } = useCachedQuery(['my-query'], () => fetchData(), { staleTime: 300000 });
```

## Security

* No PHI/PII in cache keys or values per policy.
* Tenant isolation enforced by key format and org validation.
* Cache is cleared on sign-out (wired in ResponsiveNav).
* User cache is invalidated on role assignment changes (wired in PermissionService).
