Caching in ColdFusion 2025
Implement a multi-layer caching strategy to improve performance: runtime cache for hot objects, edge cache for static assets, and distributed cache for sessions in clustered environments.
Caching Layers
A comprehensive caching strategy should use multiple layers to maximize performance:
- Edge Cache (CDN): Static assets, images, CSS, JavaScript
- HTTP Cache: Browser and reverse proxy caching
- Application Cache: Query results, computed values, objects
- Distributed Cache: Sessions, shared state across cluster
- Database Cache: Query plan cache, result cache
ColdFusion Built-in Caching
Template Cache
ColdFusion automatically caches compiled templates in memory. You can configure this behavior in the Administrator:
- Enable "Trusted Cache" in production environments for maximum performance
- Disable trusted cache in development so you can see template changes immediately
- Remember to clear the template cache after each deployment to production
Query Caching
<cfquery name="products" datasource="mydb" cachedWithin="#createTimeSpan(0,1,0,0)#"> SELECT * FROM products WHERE category = <cfqueryparam value="#category#"> </cfquery> <!-- Cache for 1 hour using createTimeSpan(hours, minutes, seconds, milliseconds) -->
Object Caching
<cfset cachePut("user_#userId#", userObject, createTimeSpan(0,0,30,0))> <cfset cachedUser = cacheGet("user_#userId#")> <cfif isNull(cachedUser)> <!--- Cache miss, load from database ---> </cfif>
Distributed Caching with Redis
For clustered or containerized environments, you should use Redis as a distributed cache to share data and sessions across all application nodes:
// Install Redis package via cfpm cfpm install redis --acceptEULA // Configure Redis cache in Application.cfc this.cache.configFile = "redis-cache-config.xml"; this.sessionManagement = true; this.sessionStorage = "redis";
Using Redis for session storage provides several important benefits:
- User sessions persist even when application servers restart
- Sessions are automatically shared across all cluster nodes
- Redis provides faster access than traditional database session storage
- Built-in automatic expiration and memory management keeps your cache clean
Cache Invalidation Strategies
"There are only two hard things in Computer Science: cache invalidation and naming things." - Phil Karlton
- Time-based: Expire after fixed duration
- Event-based: Invalidate on data change
- Manual: Clear specific keys when needed
- LRU: Least Recently Used eviction
<!--- Clear specific cache entry ---> <cfset cacheRemove("user_#userId#")> <!--- Clear all cache in region ---> <cfset cacheRemove(arrayNew(1), true, "userCache")> <!--- Clear specific query cache ---> <cfquery name="products" datasource="mydb" cachedWithin="#createTimeSpan(0,0,0,0)#"> <!--- This clears the cache for this query ---> </cfquery>
Container and Stateless Considerations
For cloud-native deployments, follow these guidelines to ensure your caching strategy works in ephemeral container environments:
- Use an external distributed cache like Redis or Memcached instead of in-memory caching
- Avoid storing cached data in local files since container filesystems are ephemeral
- Store all uploaded files in object storage services like Amazon S3 or Azure Blob Storage
- Use container orchestration tools to implement cache warming during startup
HTTP Caching Headers
You should set proper HTTP headers to enable effective browser and CDN caching:
<cfheader name="Cache-Control" value="public, max-age=3600"> <cfheader name="ETag" value="#hash(content)#"> <cfheader name="Last-Modified" value="#dateFormat(now(), 'ddd, dd mmm yyyy')# #timeFormat(now(), 'HH:mm:ss')# GMT"> <!--- For non-cacheable content ---> <cfheader name="Cache-Control" value="no-cache, no-store, must-revalidate"> <cfheader name="Pragma" value="no-cache"> <cfheader name="Expires" value="0">
Caching Checklist
- Trusted cache enabled in production
- Expensive queries cached appropriately
- Redis configured for distributed sessions
- Cache invalidation strategy defined
- HTTP cache headers configured
- CDN configured for static assets
- No local disk caching in containers
- Cache hit/miss rates monitored in PMT
Gotchas
- Trusted cache requires manual clear after code deployments
- Cached queries with dynamic SQL can cause memory bloat
- Session replication without external store causes race conditions
- Cache stampede: multiple threads regenerating same cache on expiry
- Stale cache after deployments if not properly invalidated
- Redis memory limits must be configured to prevent OOM
Need Help?
Convective can help design and implement effective caching strategies for ColdFusion applications. Find out more.