DEV Community

Cover image for ColdFusion Query Caching Deep Dive: cachedWithin, cachedAfter, and Cache Invalidation
Deepak Sir
Deepak Sir

Posted on

ColdFusion Query Caching Deep Dive: cachedWithin, cachedAfter, and Cache Invalidation

ColdFusion query caching stores a query’s result set in server memory the first time it runs, then serves subsequent identical queries from memory instead of hitting the database — turning a round trip that might take hundreds of milliseconds into a sub-millisecond lookup. You enable it with one of two cfquery attributes: cachedWithin takes a createTimeSpan(days, hours, minutes, seconds) value and serves cached results until that timespan elapses (e.g., cachedWithin="#createTimeSpan(0,1,0,0)#" caches for one hour), and cachedAfter takes a date and uses cached data only if the original query ran after that date. The three things that trip teams up: the cache key is the exact SQL statement, so every variation of a dynamic query gets its own cache entry; the cache is a fixed-size FIFO buffer capped by a ColdFusion Administrator limit, so query 501 evicts the oldest when the limit is 500; and invalidation is the hard part — cached data stays stale until the TTL expires unless you actively clear it with cacheRemove(), the cache service's clearQueryCache(), or by giving the query an explicit cacheId you can target. This guide covers cachedWithin, cachedAfter, the gotchas, and how to invalidate correctly.
Read More

Top comments (0)