What it does
Permission checks are fast, but they can add up. Caching stores permission checks in memory so repeated checks return instantly without hitting the database. A cache expires after a set time, keeping it fresh.
When to use it
Caching is almost always on:
- Default behavior — permly caches by default
- Production apps — faster checks, less database load
- Concurrent requests — multiple requests can reuse the same cached result
Disable caching only when you need real-time permission updates (rare).
How it works
- Check a permission:
await perms.user(42).can("posts.edit") - permly looks in the cache — if it's there, return instantly
- If not cached — query the database, cache the result
- Cache expires after a timeout (default 60 seconds)
- Next request — cache is fresh again for that user
The cache is in-process memory. If you have multiple app instances, each caches independently.
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// First check: queries the database
let allowed = await perms.user(42).can("posts.edit");
console.log(allowed); // From database
// Second check: from cache (instant)
allowed = await perms.user(42).can("posts.edit");
console.log(allowed); // Cached result
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
let allowed: boolean = await perms.user(42).can("posts.edit");
console.log(allowed);
allowed = await perms.user(42).can("posts.edit");
console.log(allowed);
Configure cache timeout
Set a custom cache expiration time. The ttl config is in seconds (default 60):
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit"],
roles: ["editor"],
cache: {
ttl: 120, // Cache expires after 120 seconds
},
});
await perms.sync();
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit"],
roles: ["editor"],
cache: {
ttl: 120,
},
});
await perms.sync();
Disable caching
If you need real-time permission updates, disable the cache:
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit"],
roles: ["editor"],
cache: {
ttl: 0, // Cache disabled
},
});
await perms.sync();
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: ["posts.create", "posts.edit"],
roles: ["editor"],
cache: {
ttl: 0,
},
});
await perms.sync();
Clear the cache
Manually clear cached permissions for a user:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Assign a permission
await perms.user(42).givePermission("posts.delete");
// Check it (cached)
let allowed = await perms.user(42).can("posts.delete");
console.log(allowed); // true
// Clear cache for all users
await perms.clearCache();
// Next check hits the database again
allowed = await perms.user(42).can("posts.delete");
console.log(allowed); // true (but from database)
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(42).givePermission("posts.delete");
let allowed: boolean = await perms.user(42).can("posts.delete");
console.log(allowed);
await perms.clearCache();
allowed = await perms.user(42).can("posts.delete");
console.log(allowed);
Common mistakes
Expecting instant permission updates — If you revoke a permission, cached clients won't see it for up to ttl seconds. Clear the cache or set a shorter TTL.
Not clearing cache after changes — Always clear the cache after assigning/revoking permissions.
Cache doesn't span instances — If you have 3 app servers, each caches independently. Use a distributed cache (Redis) for sync across instances (advanced).
Related pages
- Checking Permissions — How checks work
- Error Handling — Handle cache failures
- Syncing Config — Keep roles/permissions in sync