Introduction & Context
Performance issues in ERP systems rarely come from databases alone. Repeated computations, excessive queries, and redundant API calls often slow things down. This is where caching becomes essential.
In Frappe Framework v15, caching is a first-class feature, tightly integrated with Redis and designed to improve response times across Desk, Website, and API layers—without compromising data integrity.
This guide explains how caching works in Frappe v15, when to use it, and how to apply it correctly in real ERPNext projects.
What Is Caching in Frappe?
Caching in Frappe is the process of temporarily storing frequently accessed data in memory so it can be retrieved faster than recomputing it or querying the database again.
Frappe uses caching to:
- Reduce database load
- Speed up API responses
- Improve user experience in Desk and Website
- Optimize background jobs and integrations
Caching is optional but highly recommended for performance-critical logic.
Why Does Frappe Use Redis for Caching?
Frappe relies on Redis because it is:
- In-memory and extremely fast
- Shared across workers
- Persistent (optional)
- Well-suited for distributed systems
Redis enables cross-request and cross-process caching, which local Python memory cannot provide.
Types of Caching in Frappe Framework v15
Frappe v15 provides multiple caching layers, each with a specific purpose.
1. Request-Level (Local) Cache
Answer:
Request-level cache stores data only for the lifetime of a single HTTP request.
- Scope: One request
- Storage: Python memory
- Use case: Avoid repeated computation in the same request
Example Use Case
Fetching the same DocType metadata multiple times during one request.
This cache is automatically cleared after the request ends.
2. Redis-Based Global Cache
Answer:
Global cache stores data in Redis and is shared across all workers and requests.
- Scope: All users and processes
- Storage: Redis
- Use case: Reusable business logic results
This is the most commonly used caching mechanism in Frappe.
Implementation Details: Core Caching APIs
Frappe exposes a clean and consistent API for caching.
How to Set and Get Cache Values in Frappe
Basic Cache Operations
frappe.cache().set_value("my_key", "my_value")
value = frappe.cache().get_value("my_key")
- set_value stores data in Redis
- get_value retrieves cached data
If the key does not exist, None is returned.
How to Set Cache with Expiry
Answer:
You can control cache lifetime using an expiry (in seconds).
frappe.cache().set_value("temp_key", "data", expires_in_sec=300)
This automatically clears stale data and prevents memory bloat.
How to Delete Cached Data
frappe.cache().delete_value("my_key")
Use this when underlying data changes and cache must be invalidated.
Configuration & Setup: Cache Infrastructure in Frappe
Where Is Cache Configured?
Caching is configured automatically during bench setup.
Key components include:
- Redis Cache
- Redis Queue
- Redis SocketIO
These are defined in sites/common_site_config.json.
How Frappe Differentiates Cache Keys
Frappe internally namespaces keys by:
- Site name
- Environment
- Purpose
This prevents collisions across multi-tenant setups.
User Guidance: When Should You Use Caching?
You should use caching when:
- Data is read frequently but updated rarely
- Calculations are expensive
- APIs are called repeatedly
- Dashboard metrics are recalculated often
Avoid caching transactional or frequently changing data unless you manage invalidation carefully.
Best Practices & Tips
- Always invalidate cache after writes
- Use descriptive cache keys
- Set expiry whenever possible
- Avoid caching user-specific sensitive data globally
- Monitor Redis memory usage
Caching should optimize, not hide design problems.
Advanced Topics
Caching Function Results with @frappe.cache
Frappe supports decorator-based caching for pure functions.
@frappe.cache()
def get_company_defaults():
return frappe.get_single("Global Defaults")
This is ideal for configuration lookups.
Caching vs Background Jobs
Use caching for fast reads.
Use background jobs for slow writes or recalculations.
They complement each other but solve different problems.
Integration Patterns
Caching is commonly used with:
- REST APIs
- Dashboards and reports
- ERPNext website pages
- Third-party integrations
- High-traffic portals
It plays a critical role in scalable ERP architectures.
Troubleshooting Common Caching Issues
Cache Not Updating
- Ensure delete_value is called after updates
- Restart bench if Redis is out of sync
- Verify correct cache key
Redis Memory Issues
- Reduce expiry times
- Remove unused keys
- Avoid caching large objects
Industry Relevance
Caching is essential in:
- High-volume ERP deployments
- SaaS ERP platforms
- Manufacturing dashboards
- Real-time reporting systems
Without caching, ERPNext performance degrades quickly at scale.
Target Audience
- ERPNext Developers
- Frappe Framework Developers
- ERP Consultants
- Backend Engineers optimizing performance
Summary: Why Caching Matters in Frappe v15
Caching in Frappe Framework v15 is a powerful, safe, and essential mechanism for building scalable ERPNext systems. When used correctly, it dramatically improves performance while keeping business logic clean and maintainable. Understanding caching is not optional—it is a core skill for professional Frappe developers.