skills/source/core/frappe-core-cache/SKILL.md
Use when implementing Redis caching, cache invalidation, or distributed locking in Frappe. Prevents stale cache bugs, race conditions from missing locks, and memory bloat from unbounded cache keys. Covers frappe.cache(), @redis_cache decorator, cache.get_value/set_value, cache invalidation patterns, frappe.lock, TTL strategies. Keywords: cache, Redis, redis_cache, invalidation, locking, frappe.cache, get_value, set_value, TTL, distributed lock, data not refreshing, stale data, cache not clearing, Redis error, slow repeated queries..
npx skillsauth add OpenAEC-Foundation/ERPNext_Anthropic_Claude_Development_Skill_Package frappe-core-cacheInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| Action | Method | Notes |
|--------|--------|-------|
| Set value | frappe.cache.set_value(key, val) | With optional TTL |
| Get value | frappe.cache.get_value(key) | Returns None if missing |
| Get or generate | frappe.cache.get_value(key, generator=fn) | Calls fn() on cache miss |
| Delete value | frappe.cache.delete_value(key) | Single key or list of keys |
| Delete by pattern | frappe.cache.delete_keys(pattern) | Wildcard * matching |
| Hash set | frappe.cache.hset(name, key, val) | Redis hash field |
| Hash get | frappe.cache.hget(name, key) | Single hash field |
| Hash get all | frappe.cache.hgetall(name) | Full hash as dict |
| Hash delete | frappe.cache.hdel(name, key) | Remove hash field |
| Hash exists | frappe.cache.hexists(name, key) | Returns bool |
| Cached document | frappe.get_cached_doc(dt, dn) | Full doc from cache |
| Clear doc cache | frappe.clear_document_cache(dt, dn) | Invalidate cached doc |
| Decorator cache | @redis_cache | Auto-cache function result |
| Request cache | frappe.local.cache | Per-request dict (not Redis) |
What caching pattern do you need?
│
├─ Cache a function result automatically?
│ ├─ Pure function (same args → same result) → @redis_cache
│ └─ Need custom key/TTL → manual get_value/set_value
│
├─ Cache a document?
│ ├─ Read-only access → frappe.get_cached_doc()
│ └─ Need to invalidate → frappe.clear_document_cache()
│
├─ Cache structured data (multiple fields)?
│ └─ Redis hash → hset/hget/hgetall
│
├─ Per-request cache (avoid repeated DB calls in one request)?
│ └─ frappe.local.cache dict
│
├─ Prevent concurrent execution?
│ └─ Distributed lock → frappe.lock("resource_name")
│
└─ Invalidate cache?
├─ Single key → delete_value(key)
├─ Pattern → delete_keys("prefix*")
└─ All site cache → frappe.clear_cache()
# Set a value (persists until evicted or deleted)
frappe.cache.set_value("exchange_rate_USD", 1.08)
# Set with TTL (expires after N seconds)
frappe.cache.set_value("exchange_rate_USD", 1.08, expires_in_sec=3600)
# Get value (returns None if missing)
rate = frappe.cache.get_value("exchange_rate_USD")
# Get with generator (calls function on cache miss, stores result)
rate = frappe.cache.get_value(
"exchange_rate_USD",
generator=lambda: fetch_exchange_rate("USD"),
)
# Store per-user preference
frappe.cache.set_value("dashboard_layout", "compact", user="[email protected]")
# Retrieve for specific user
layout = frappe.cache.get_value("dashboard_layout", user="[email protected]")
# Single key
frappe.cache.delete_value("exchange_rate_USD")
# Multiple keys
frappe.cache.delete_value(["exchange_rate_USD", "exchange_rate_EUR"])
# Pattern-based deletion (wildcard)
frappe.cache.delete_keys("exchange_rate*")
Use hashes to group related fields under a single key.
# Set hash fields
frappe.cache.hset("config|notifications", "email_enabled", True)
frappe.cache.hset("config|notifications", "sms_enabled", False)
frappe.cache.hset("config|notifications", "max_retries", 3)
# Get single field
email_on = frappe.cache.hget("config|notifications", "email_enabled")
# Get all fields as dict
config = frappe.cache.hgetall("config|notifications")
# {"email_enabled": True, "sms_enabled": False, "max_retries": 3}
# Delete field
frappe.cache.hdel("config|notifications", "sms_enabled")
# Check existence
exists = frappe.cache.hexists("config|notifications", "email_enabled")
# hget with generator — calls function on miss
value = frappe.cache.hget(
"user|permissions",
"[email protected]",
generator=lambda: compute_permissions("[email protected]"),
)
Automatically cache function return values based on arguments.
from frappe.utils.caching import redis_cache
@redis_cache
def get_item_price(item_code, price_list):
"""Expensive query — cached automatically."""
return frappe.db.get_value("Item Price",
{"item_code": item_code, "price_list": price_list},
"price_list_rate",
)
# First call — hits database, stores in Redis
price = get_item_price("ITEM-001", "Standard Selling")
# Second call — returns from cache
price = get_item_price("ITEM-001", "Standard Selling")
# Clear all cached results for this function
get_item_price.clear_cache()
@redis_cache(ttl=300) # expires after 5 minutes
def get_exchange_rate(from_currency, to_currency):
return fetch_rate_from_api(from_currency, to_currency)
Rules for @redis_cache:
.clear_cache() when underlying data changes.frappe.local.cache is a plain Python dict that lives for the duration of a single HTTP request. It is NOT stored in Redis.
def get_user_settings():
"""Avoid repeated DB calls within a single request."""
if "user_settings" not in frappe.local.cache:
frappe.local.cache["user_settings"] = frappe.get_doc(
"User Settings", frappe.session.user
)
return frappe.local.cache["user_settings"]
Use frappe.local.cache when:
# Get cached document (read-only, no permission check)
settings = frappe.get_cached_doc("System Settings")
item = frappe.get_cached_doc("Item", "ITEM-001")
# Invalidate when document changes
frappe.clear_document_cache("Item", "ITEM-001")
# Cached single value
val = frappe.db.get_value("Item", "ITEM-001", "item_name", cache=True)
NEVER modify a document returned by frappe.get_cached_doc() — it returns a shared reference. Modifications corrupt the cache for all subsequent reads.
Prevent concurrent execution of critical sections using Redis-based locks.
# Context manager (recommended)
with frappe.lock("process_payroll"):
# Only one worker executes this block at a time
process_all_salary_slips()
# Lock auto-released on exit
# Manual lock/unlock
frappe.lock("inventory_sync")
try:
sync_inventory()
finally:
frappe.unlock("inventory_sync") # ALWAYS unlock in finally
Rules:
with frappe.lock() (context manager) to guarantee release.frappe.cache.set_value("dashboard_stats", compute_stats(), expires_in_sec=300)
Best for: Data that can be slightly stale (exchange rates, dashboard aggregates).
# In hooks.py
doc_events = {
"Item Price": {
"on_update": "my_app.cache.invalidate_price_cache",
"on_trash": "my_app.cache.invalidate_price_cache",
}
}
# In my_app/cache.py
def invalidate_price_cache(doc, method):
frappe.cache.delete_keys("item_price*")
# Or clear specific function cache:
# get_item_price.clear_cache()
Best for: Data that MUST be fresh immediately after changes.
@redis_cache(ttl=600)
def get_pricing_rules():
return frappe.get_all("Pricing Rule", fields=["*"])
# Event hook clears cache immediately on change
def on_pricing_rule_update(doc, method):
get_pricing_rules.clear_cache()
Best for: Frequently read data with occasional updates.
| Key Pattern | Content |
|-------------|---------|
| doctype::meta::{dt} | DocType metadata |
| user_permissions::{user} | User permission cache |
| bootinfo::{user} | User boot info |
| notifications::{user} | Notification counts |
| document_cache::{dt}::{dn} | Cached document |
NEVER write to internal cache keys directly. ALWAYS use the documented API methods (get_cached_doc, clear_document_cache, etc.).
frappe.local.cache for data needed multiple times within a single request — it avoids Redis round-trips entirely.frappe.clear_cache() as a routine invalidation strategy — it clears ALL cache keys for the site, causing a cold-cache performance hit.myapp|exchange_rate) to avoid collisions with Frappe internals.Default config: {bench}/config/redis_cache.conf
| Setting | Default | Description | |---------|---------|-------------| | Port | 13000 | Redis cache port | | Bind | 127.0.0.1 | Listen address | | maxmemory-policy | allkeys-lru | Eviction policy | | maxmemory | 256mb | Max memory (adjustable) |
All cache keys are automatically prefixed by Frappe with the site name:
# You write:
frappe.cache.set_value("my_key", "value")
# Redis stores:
# "mysite.localhost|my_key"
frappe.cache.make_key(key, user, shared) handles prefixing. The shared=True parameter removes the site prefix for cross-site keys (rare use case).
| Feature | v14 | v15 | v16 |
|---------|-----|-----|-----|
| frappe.cache.set_value | Available | Available | Available |
| @redis_cache | Not available | Available | Available |
| @redis_cache(ttl=) | Not available | Available | Available |
| frappe.lock context mgr | Available | Available | Available |
| frappe.local.cache | Available | Available | Available |
| hget with generator | Available | Available | Available |
frappe-core-database — Database queries that benefit from cachingfrappe-core-permissions — User permission cachingtools
Use when implementing OAuth providers, Connected Apps, Webhooks, Payment Gateways, or Data Import/Export in Frappe. Prevents authentication failures from wrong OAuth flow, missed webhook deliveries, and data corruption during bulk imports. Covers OAuth2 provider/client, Connected App DocType, Webhook DocType, Payment Gateway integration, Data Import, Data Export, frappe.integrations module. Keywords: OAuth, Connected App, Webhook, Payment Gateway, Data Import, Data Export, integration, API key, OAuth2, webhook trigger, connect to external service, OAuth setup, webhook configuration, import data, export data..
development
Use when implementing hooks.py configurations in a Frappe custom app. Covers step-by-step workflows for doc_events, scheduler_events, override/extend_doctype_class, permission hooks, extend_bootinfo, fixtures, asset injection, website hooks, and doctype_js. Prevents broken transactions, missed migrations, and multi-app conflicts. Keywords: hooks.py, doc_events, scheduler_events, override doctype,, how to add hook, when to use doc_events, scheduler setup, override existing behavior. extend doctype class, permission hook, scheduler job, fixtures, doctype_js, extend_bootinfo, website hooks.
development
Use when building a custom Frappe app from scratch. Covers bench new-app walkthrough, app structure decisions, adding DocTypes, hooks, patches, fixtures management, development workflow (bench migrate, build, clear-cache), testing, packaging, installing on another site, version management, and app dependencies for v14/v15/v16. Keywords: create custom app, new frappe app, bench new-app, app structure, module creation, doctype creation, fixtures, patches, deployment, packaging, data migration, patch file, patches.txt, migrate data between DocTypes, create new app from scratch.
development
Use when building Document Controllers in a custom Frappe app: file creation, lifecycle hooks, validation, autoname, submittable workflows, controller override, child table controllers, flags system, migration from hooks.py and Server Scripts. Keywords: how to implement controller, which hook to use, validate vs on_update, override controller, submittable document, autoname, flags, extend_doctype_class, controller testing, child table controller, which hook to use, when does validate run, how to override save, document lifecycle.