Caching

Configure template caching, trusted cache, and query cache strategies

Overview

The Caching page provides controls for ColdFusion's various caching mechanisms, which are critical for optimal performance. Proper cache configuration can dramatically reduce server load and improve response times by eliminating redundant compilation and database queries. This page covers template caching, trusted cache mode, query caching, and object caching settings.

Template Cache Settings

Maximum Number of Cached Templates

PurposeLimits how many compiled CFML templates are stored in memory
Default1024 templates
RecommendationSet to the total number of unique CFM/CFC files in your application(s)
ImpactWhen cache is full, least recently used templates are evicted and must be recompiled
Memory ConsiderationEach cached template uses approximately 5-50 KB depending on complexity
Best PracticeMonitor cache hit ratio and increase if you see frequent evictions

Trusted Cache

PurposeWhen enabled, ColdFusion never checks if cached templates have been modified
DefaultDisabled
Production RecommendationEnable for maximum performance
Development RecommendationKeep disabled to see code changes immediately
ImpactEliminates file system checks on every request, significantly improving performance
Deployment ProcessClear template cache after deploying new code (via administrator or programmatically)
Performance Gain10-30% reduction in request processing time
Trade-offMust manually clear cache after code changes

Cache Template in Request

PurposeCaches templates for the duration of a single request
DefaultDisabled
RecommendationEnable if you include the same template multiple times per request
Use CaseRecursive includes or heavily reused utility templates
ImpactPrevents redundant file operations within a single request

Component Cache

PurposeControls caching of CFC metadata and compiled code
DefaultEnabled
RecommendationAlways enable (disabling severely impacts performance)
ImpactCFCs are cached after first instantiation; changes require cache clear
Memory UsageLarger than template cache as CFCs include metadata

Save Class Files

PurposePersist compiled Java class files to disk
DefaultEnabled
RecommendationAlways enable to speed up server restarts
ImpactFaster server startup as templates don't need full recompilation
LocationStored in cfusion/lib/neo-classes or similar directory
CleanupPeriodically delete old class files during maintenance windows

Query Caching Settings

Maximum Number of Cached Queries

PurposeLimits how many query result sets can be cached simultaneously
Default100 queries
RecommendationSet based on your application's caching strategy (500-2000 for heavy use)
ImpactWhen limit is reached, oldest cached queries are evicted
Memory ConsiderationEach cached query stores full result set in memory
Best PracticeUse cachedwithin attribute strategically on expensive queries

Query Cache Size Limit

PurposeMaximum memory (MB) allocated to query cache
DefaultVaries by edition and heap size
Recommendation256-512 MB for applications with extensive query caching
ImpactPrevents query cache from consuming excessive memory
MonitoringWatch for cache evictions due to size constraints

Clear Template Cache Now

PurposeImmediately flush all cached templates and CFCs
Use CaseAfter deploying code changes in production with trusted cache enabled
ImpactNext request for each template will recompile (temporary performance hit)
Programmatic AlternativeUse createObject("java", "coldfusion.server.ServiceFactory").getCacheService().clearTrustedCache()

Clear Query Cache Now

PurposeFlush all cached query results
Use CaseWhen underlying data has changed and cached results are stale
Granular ControlUse cachedwithin timespan for automatic expiration
Programmatic AlternativeUse createObject("java", "coldfusion.server.ServiceFactory").getCacheService().clearQueryCache()

Object Caching (Enterprise Edition)

Maximum Number of Cached Objects

PurposeLimit for objects stored via cfobjectcache or cachePut()
Default1000 objects
RecommendationSet based on application needs (5000-50000 for data-intensive apps)
ImpactLRU eviction when limit reached
Use CaseSession data, frequently accessed business objects, API responses

Object Cache Memory Limit

PurposeMaximum memory for application-level object cache
DefaultBased on heap size
Recommendation512 MB - 2 GB depending on caching strategy
MonitoringUse FusionReactor or built-in monitoring to track usage

Best Practices

Production Environment

  • Enable trusted cache for maximum performance
  • Set template cache size to accommodate all templates (monitor for evictions)
  • Clear template cache after each code deployment
  • Use query caching strategically with appropriate cachedwithin values
  • Enable save class files to speed up restarts
  • Monitor cache hit ratios and adjust sizes accordingly
  • Set conservative memory limits to prevent OutOfMemory errors
  • Document cache clearing procedures in deployment runbooks

Development Environment

  • Disable trusted cache to see code changes immediately
  • Keep template cache size moderate (1024-2048)
  • Disable query caching or use very short timeouts
  • Manually clear caches when debugging caching issues
  • Test cache clearing procedures before production use

Security Considerations

  • Ensure cached queries don't expose sensitive data longer than necessary
  • Consider security implications of caching user-specific data
  • Clear caches after security patches or sensitive code changes
  • Implement cache key strategies that prevent cache poisoning
  • Monitor for unusual cache behavior that might indicate attacks

Performance Tuning

  • Trusted cache provides 10-30% performance improvement in production
  • Query caching can reduce database load by 50-90% for read-heavy applications
  • Monitor template cache evictions - increase size if evictions are frequent
  • Use shorter cache timeouts for frequently changing data
  • Balance cache size against available heap memory
  • Profile application to identify best candidates for query caching
  • Consider external caching solutions (Redis, Memcached) for distributed deployments

Common Issues and Solutions

Code Changes Not Appearing (Trusted Cache)

Symptom: Deployed code changes not visible to users
Solution:
  • Cause: Trusted cache enabled, template cache not cleared
  • Clear template cache via administrator or programmatically
  • Prevention: Automate cache clearing in deployment scripts

OutOfMemoryError with Large Caches

Symptom: Server crashes or becomes unresponsive
Solution:
  • Cause: Cache sizes exceed available heap memory
  • Reduce cache sizes or increase JVM heap memory
  • Analysis: Use heap dumps to identify cache memory consumption
  • Best Practice: Total cache memory should not exceed 30-40% of heap

Stale Query Results

Symptom: Users seeing outdated data
Solution:
  • Cause: Query cache timeout too long or not clearing after data updates
  • Reduce cachedwithin timespan or clear cache after updates
  • Pattern: Clear specific cached queries using cacheClear() with ID

Template Cache Thrashing

Symptom: High CPU usage, frequent recompilations
Solution:
  • Cause: Template cache too small for application size
  • Increase maximum number of cached templates
  • Detection: Monitor cache hit ratio via server monitoring tools
  • Target: Cache hit ratio should be 95%+

Inconsistent Behavior Across Cluster

Symptom: Different servers showing different versions of code
Solution:
  • Cause: Cache cleared on some servers but not others
  • Ensure cache clearing is performed on all cluster nodes
  • Tool: Use cluster manager or deployment automation to clear all caches

Cache Clearing Strategies

Manual Cache Clearing

  • Via Administrator: Click "Clear Template Cache Now" or "Clear Query Cache Now"
  • Requires administrator login and manual action on each server
  • Best for development or emergency situations

Programmatic Cache Clearing

Programmatic Cache Clearing
Clear ColdFusion caches programmatically using Java services
// Clear template cache
cacheService = createObject("java", "coldfusion.server.ServiceFactory")
    .getCacheService();
cacheService.clearTrustedCache();

// Clear query cache
cacheService.clearQueryCache();

// Clear specific cached query
cacheClear("myQueryCacheName");

// Clear all application caches
applicationStop();
<!--- Clear template cache --->
<cfset cacheService = createObject("java", "coldfusion.server.ServiceFactory")
    .getCacheService()>
<cfset cacheService.clearTrustedCache()>

<!--- Clear query cache --->
<cfset cacheService.clearQueryCache()>

<!--- Clear specific cached query --->
<cfset cacheClear("myQueryCacheName")>

<!--- Clear all application caches --->
<cfset applicationStop()>

Deployment Script Integration

  • Include cache clearing in deployment automation
  • Call cache clearing endpoint after code deployment
  • Verify cache was cleared on all cluster nodes
  • Monitor for errors during cache clear operations

Monitoring Cache Performance

Key Metrics to Track

  • Template Cache Hit Ratio: Should be 95%+ in production
  • Template Cache Evictions: Frequent evictions indicate cache too small
  • Query Cache Size: Monitor memory consumption
  • Query Cache Hit Ratio: Varies by application (50-90% typical)
  • Compilation Time: Spikes indicate cache clears or evictions

Monitoring Tools

  • ColdFusion Server Monitor (included with Enterprise)
  • FusionReactor APM (third-party, highly recommended)
  • Custom logging via getMetrics() or similar APIs
  • JVM monitoring tools (JConsole, VisualVM)

Related Resources