$npx -y skills add Impertio-Studio/Frappe_Claude_Skill_Package --skill frappe-core-cacheUse 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 in
| 1 | # Frappe Cache & Locking |
| 2 | |
| 3 | ## Quick Reference |
| 4 | |
| 5 | | Action | Method | Notes | |
| 6 | |--------|--------|-------| |
| 7 | | Set value | `frappe.cache.set_value(key, val)` | With optional TTL | |
| 8 | | Get value | `frappe.cache.get_value(key)` | Returns `None` if missing | |
| 9 | | Get or generate | `frappe.cache.get_value(key, generator=fn)` | Calls `fn()` on cache miss | |
| 10 | | Delete value | `frappe.cache.delete_value(key)` | Single key or list of keys | |
| 11 | | Delete by pattern | `frappe.cache.delete_keys(pattern)` | Wildcard `*` matching | |
| 12 | | Hash set | `frappe.cache.hset(name, key, val)` | Redis hash field | |
| 13 | | Hash get | `frappe.cache.hget(name, key)` | Single hash field | |
| 14 | | Hash get all | `frappe.cache.hgetall(name)` | Full hash as dict | |
| 15 | | Hash delete | `frappe.cache.hdel(name, key)` | Remove hash field | |
| 16 | | Hash exists | `frappe.cache.hexists(name, key)` | Returns bool | |
| 17 | | Cached document | `frappe.get_cached_doc(dt, dn)` | Full doc from cache | |
| 18 | | Clear doc cache | `frappe.clear_document_cache(dt, dn)` | Invalidate cached doc | |
| 19 | | Decorator cache | `@redis_cache` | Auto-cache function result | |
| 20 | | Request cache | `frappe.local.cache` | Per-request dict (not Redis) | |
| 21 | |
| 22 | --- |
| 23 | |
| 24 | ## Decision Tree |
| 25 | |
| 26 | ``` |
| 27 | What caching pattern do you need? |
| 28 | │ |
| 29 | ├─ Cache a function result automatically? |
| 30 | │ ├─ Pure function (same args → same result) → @redis_cache |
| 31 | │ └─ Need custom key/TTL → manual get_value/set_value |
| 32 | │ |
| 33 | ├─ Cache a document? |
| 34 | │ ├─ Read-only access → frappe.get_cached_doc() |
| 35 | │ └─ Need to invalidate → frappe.clear_document_cache() |
| 36 | │ |
| 37 | ├─ Cache structured data (multiple fields)? |
| 38 | │ └─ Redis hash → hset/hget/hgetall |
| 39 | │ |
| 40 | ├─ Per-request cache (avoid repeated DB calls in one request)? |
| 41 | │ └─ frappe.local.cache dict |
| 42 | │ |
| 43 | ├─ Prevent concurrent execution? |
| 44 | │ └─ Distributed lock → frappe.lock("resource_name") |
| 45 | │ |
| 46 | └─ Invalidate cache? |
| 47 | ├─ Single key → delete_value(key) |
| 48 | ├─ Pattern → delete_keys("prefix*") |
| 49 | └─ All site cache → frappe.clear_cache() |
| 50 | ``` |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | ## String Operations |
| 55 | |
| 56 | ### Set and Get |
| 57 | |
| 58 | ```python |
| 59 | # Set a value (persists until evicted or deleted) |
| 60 | frappe.cache.set_value("exchange_rate_USD", 1.08) |
| 61 | |
| 62 | # Set with TTL (expires after N seconds) |
| 63 | frappe.cache.set_value("exchange_rate_USD", 1.08, expires_in_sec=3600) |
| 64 | |
| 65 | # Get value (returns None if missing) |
| 66 | rate = frappe.cache.get_value("exchange_rate_USD") |
| 67 | |
| 68 | # Get with generator (calls function on cache miss, stores result) |
| 69 | rate = frappe.cache.get_value( |
| 70 | "exchange_rate_USD", |
| 71 | generator=lambda: fetch_exchange_rate("USD"), |
| 72 | ) |
| 73 | ``` |
| 74 | |
| 75 | ### User-Scoped Values |
| 76 | |
| 77 | ```python |
| 78 | # Store per-user preference |
| 79 | frappe.cache.set_value("dashboard_layout", "compact", user="user@example.com") |
| 80 | |
| 81 | # Retrieve for specific user |
| 82 | layout = frappe.cache.get_value("dashboard_layout", user="user@example.com") |
| 83 | ``` |
| 84 | |
| 85 | ### Delete |
| 86 | |
| 87 | ```python |
| 88 | # Single key |
| 89 | frappe.cache.delete_value("exchange_rate_USD") |
| 90 | |
| 91 | # Multiple keys |
| 92 | frappe.cache.delete_value(["exchange_rate_USD", "exchange_rate_EUR"]) |
| 93 | |
| 94 | # Pattern-based deletion (wildcard) |
| 95 | frappe.cache.delete_keys("exchange_rate*") |
| 96 | ``` |
| 97 | |
| 98 | --- |
| 99 | |
| 100 | ## Hash Operations |
| 101 | |
| 102 | Use hashes to group related fields under a single key. |
| 103 | |
| 104 | ```python |
| 105 | # Set hash fields |
| 106 | frappe.cache.hset("config|notifications", "email_enabled", True) |
| 107 | frappe.cache.hset("config|notifications", "sms_enabled", False) |
| 108 | frappe.cache.hset("config|notifications", "max_retries", 3) |
| 109 | |
| 110 | # Get single field |
| 111 | email_on = frappe.cache.hget("config|notifications", "email_enabled") |
| 112 | |
| 113 | # Get all fields as dict |
| 114 | config = frappe.cache.hgetall("config|notifications") |
| 115 | # {"email_enabled": True, "sms_enabled": False, "max_retries": 3} |
| 116 | |
| 117 | # Delete field |
| 118 | frappe.cache.hdel("config|notifications", "sms_enabled") |
| 119 | |
| 120 | # Check existence |
| 121 | exists = frappe.cache.hexists("config|notifications", "email_enabled") |
| 122 | ``` |
| 123 | |
| 124 | ### Hash with Generator |
| 125 | |
| 126 | ```python |
| 127 | # hget with generator — calls function on miss |
| 128 | value = frappe.cache.hget( |
| 129 | "user|permissions", |
| 130 | "user@example.com", |
| 131 | generator=lambda: compute_permissions("user@example.com"), |
| 132 | ) |
| 133 | ``` |
| 134 | |
| 135 | --- |
| 136 | |
| 137 | ## @redis_cache Decorator |
| 138 | |
| 139 | Automatically cache function return values based on arguments. |
| 140 | |
| 141 | ```python |
| 142 | from frappe.utils.caching import redis_cache |
| 143 | |
| 144 | @redis_cache |
| 145 | def get_item_price(item_code, price_list): |
| 146 | """Expensive query — cached automatically.""" |
| 147 | return frappe.db.get_value("Item Price", |
| 148 | {"item_code": item_code, "price_list": price_list}, |
| 149 | "p |