mcpField
S
serverpod-caching

serverpod-caching

github.com/serverpodDevelopment3.3k44 installsUpdated 1d ago
Repository

About serverpod-caching

serverpod-caching is a development skill covering Serverpod’s caching API for Serverpod developers. It explains local and Redis-backed caches, cache-miss handling, and invalidation behavior.

Key features

  1. 01In-memory local caches
  2. 02Redis-backed shared cache
  3. 03Load and store on cache miss
  4. 04Group and key invalidation

Use cases

  1. 01Cache frequently accessed data
  2. 02Share cache entries across Redis-enabled instances
  3. 03Set entry lifetimes to limit cache growth

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/serverpod/serverpod --skill serverpod-caching

After installing, type/serverpod-cachingin Claude Code to confirm it loaded.

Details

From the project README

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

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:

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

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.

Comments

Markdown supported