Skip to main content

serverpod-caching

Serverpod caching — local and Redis caches, cache keys, lifetime, CacheMissHandler. Use when caching data, optimizing queries, or working with session.caches.

Informations de source

Dépôt
serverpod/serverpod
Dernière activité de la source
20 août 2026 à 16:48
Langue détectée de SKILL.md
anglais
Étoiles
3 317
Forks
382

Options d'installation

Le prompt qui vérifie d'abord la source est sélectionné par défaut. Vous pouvez passer à une commande directe ou télécharger une copie locale.

Vérifiez les fichiers source

Lisez SKILL.md et les fichiers associés affichés par SkillsMP avant de décider de l'installer.

Affichage de SKILL.md

SKILL.md
Instructions source · Aperçu en lecture seule
name
serverpod-caching
description
Serverpod caching — local and Redis caches, cache keys, lifetime, CacheMissHandler. Use when caching data, optimizing queries, or working with session.caches.
# Serverpod Caching In-memory and optional Redis caches via `session.caches`. Cached objects must be serializable models or primitives supported by Serverpod. ## Cache types - **`session.caches.local`** — in-memory, current server instance - **`session.caches.localPrio`** — in-memory, for frequently accessed entries - **`session.caches.global`** — Redis-backed, shared across instances (requires Redis config; do not use without Redis enabled) - **`session.caches.query`** — local query cache used by generated database helpers ## Basic usage ```dart await session.caches.local.put('UserData-$userId', userData, lifetime: Duration(minutes: 5)); var userData = await session.caches.local.get<UserData>('UserData-$userId'); ``` ## CacheMissHandler Load on miss and store automatically: ```dart var userData = await session.caches.local.get( 'UserData-$userId', CacheMissHandler( () async => UserData.db.findById(session, userId), lifetime: Duration(minutes: 5), ), ); ``` Returns `null` if the handler returns `null` (nothing stored). ## Collections and primitives ```dart await session.caches.local.put('userCount', 17, lifetime: Duration(minutes: 5)); var count = await session.caches.local.get<int>('userCount'); ``` If relevant set a **lifetime** to avoid unbounded growth. Use stable, unique keys (e.g. `'EntityName-$id'`). ## Pitfalls - `session.caches.global` asserts Redis is enabled; it is not a safe no-op fallback. - Cache groups (`put(..., group: 'name')` + `invalidateGroup('name')`) only work on the local caches. `invalidateGroup` throws `UnimplementedError` on the Redis-backed global cache, so invalidate those entries by key with `invalidateKey`.
Voir sur GitHub