caching in on cf performance n.
Skip this Video
Loading SlideShow in 5 Seconds..
“Caching In” on CF Performance PowerPoint Presentation
Download Presentation
“Caching In” on CF Performance

Loading in 2 Seconds...

play fullscreen
1 / 49
Download Presentation

“Caching In” on CF Performance - PowerPoint PPT Presentation

Download Presentation

“Caching In” on CF Performance

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. “Caching In” on CF Performance Charlie Arehart (

  2. Topics • Why worry about caching? • The top 3 caching features • Several more caching options • Caching outside of CF • Resources for learning more

  3. Introduction: Caching in CF • Will see more than a dozen ways • Can you name more than 3? • Some set/controlled in code, others in Admin • More caching outside of CF • Database, web server, browser, etc. • Do you really understand caching you’ve used? • Often conflicting recommendations • Easy to make a mistake, make things worse • We’ll review over a dozen approaches

  4. Introduction • Presentation, code will be posted • I collected and organized much more info than we have time to cover • Will skip some details for you to read about later • Will try to balance educating beginning/adv devs • The details cover each kind of caching • Introduce, help you understand purpose • Provide info on making choices • Discuss gotchas and surprises • Help solve problems when things go wrong • Show how to manage/flush each cache • If available, show how to monitor each cache • Most info applies to CF5, 6, 7, and 8 (& BD)

  5. About Charlie Arehart • Independent consultant • Formerly CTO, New Atlanta (BlueDragon), 4/03-4/06 • 10 yrs CF experience (25 in Enterprise IT) • Member, Adobe Community Experts • Certified Adv CF Developer (4 - 7), Cert. Adobe Instructor • Co-author, ColdFusion MX Bible (Wiley) • Frequent contrib. to ColdFusion Dev Journal, blogs, lists • Tech Editor, CFDJ (2001-2003) • Until recently, President, Atlanta ColdFusion User Group • Now co-lead (w/ Ray Camden) of Online ColdFusion Meetup ( • Frequent speaker: UGs, conf’s worldwide • New web home at • Hosts my blog; lists all my past articles, presentations • UGTV: recordings of dozens of CFUG speakers • AskCharlie: per-minute telephone & web-based CF support

  6. Client Computer Browser Anatomy of a Typical Page Request Server(s) WebServer DBMS ColdFusion Network

  7. Network Anatomy of a Typical Page Request (cont) Server(s) WebServer ColdFusion Page Creation CFQUERY CFQUERY Page Request DBMS CFHTTP,CFINVOKE, CFOBJECT Other Server(s) Client Computer

  8. Anatomy of a Typical Page Request (cont) ColdFusion Page Creation Template Execution Template Cache Page Request Class File Creation Template Compilation CFML Source

  9. Anatomy of a Typical Page Request (cont) ColdFusion Template Cache Page Request Template Compilation Source Changed? CFML Source

  10. Anatomy of a Typical Page Request (cont) ColdFusion Template Cache Trusted Cache Page Request Template Compilation Source Changed? CFML Source

  11. Why Worry About Caching? • Many operations are expensive • Database I/O, page creation, CFC invocation, CFHTTP, web svc calls • During development, may not notice a problem • Once done hundreds, thousands, or millions of times a day, the cost can become significant • Worse, as database size grows, problem can get worse • Caching can help solve performance problems • Caching simply means saving the result of some expensive operation, for later reuse • Some CFML operations do it automatically, some must be indicated • Others must be implemented manually • Yet can’t use it blindly, need to understand impact • Never using caching is likely also bad • OO-style vs. traditional development • Different caching solutions will appeal to each camp • Be patient if you see something you wouldn’t do

  12. Top 3 Caching Features • Query Caching • Template Caching • Page Caching • Unlike some talks, these are not the sole focus of this talk

  13. Top 3 Caching: Query Caching • Basics • Database operations are often very expensive • Yet frequently the returned data does not change • Sometimes never at all, like list of states • Or perhaps very infrequently, like list of employees, products • Might data be updated only overnight? • Or might user be willing to briefly live without absolute latest data? • CFQuery’s CachedWithin & CachedAfter attributes solve this • Data requested once and saved for subsequent reuse • CachedWithin: cached if query re-executed within given timeframe • Use #CreateTimeSpan(days,hours,minutes,seconds)# to indicate duration • CachedAfter: cached if query executed after a given date/time • #createdatetime(year(now()),month(now()),day(now()),hour,min,sec)# • Don’t use *just* the time, or CF assumes date of 12/30/99, so caches immediately • Debugging output tells you if you’re using a cached query • See resources later for docs, examples

  14. Top 3 Caching: Query Caching (cont) • Surprises • Caching is saved per unique SQL statement (demo) • So can have use with dynamic queries driven by form, url variables • Shared across entire server (demo) • Any page using same QueryName, SQL, DSN, uname, pw, etc. benefits from same cached result • Again, though, if SQL is different, creates different cached result • CachedWithin and CachedAfter share same cache • Query cached with one can benefit a query cached with other • Consider benefits of micro-caching (very short cache times, like minutes or even seconds) • Setting admin “max number of cached queries” to 0 permits just 1 • Was a time when did not disable, but meant unlimited! • • Note true in either CFMX 7 or 6.1 (6,1,0,83762)

  15. Top 3 Caching: Query Caching (cont) • Managing/Flushing • Admin console option to limit max number of cached query results • How to flush • Set createtimespan to 0 in cache attributes (demo) • Consider setting cachetime in variable, to affect all queries of interest • But are you flushing all cached SQL variations (like URL-driven)? • Good place to consider a CFC holding queries, so you could manage queries • Will see alternative approaches that help solve above problem • CFObjectCache (demo) • Clears all cached queries (good for problem above) • But clear across entire CF server, all apps, so think twice • Does same as servicefactory’s purgeQueryCache method in DataSourceService • Could also have insert/update/delete code invalidate the cache • Good job for a CFC

  16. Top 3 Caching: Query Caching (cont) • Monitoring • Ability to tracking query cache usage is coming in CF8 • In CF7, can use admin api to access max number of cached queries possible (demo) • CFIDE.adminapi.runtime.cfc’s getCacheProperty("MaxCachedQuery") method • Gotchas • “per unique SQL” aspect even means spaces, line breaks • Can’t use CFQueryParam in a cached query • Can’t use with CFSTOREDPROC • but SPs can be called with CFQUERY • Managing cached results is challenging, as discussed above • Be careful about using too much memory • Conversely, max setting in admin may be too low for you • If you find that expected cached result doesn’t “stay cached”, beware that someone (outside your app) may be using CFOBJECTCACHE to clear the query cache

  17. Top 3 Caching: Query Caching (cont) • Alternative: caching queries in variables, discussed later • BlueDragon adds new features for better cache management • CacheName attribute permits giving cached results a name • Action attribute permits flushing named or all cached queries, with optional • CFObjectCache CacheDomain attribute to flush all on a server • BlueDragon 7 adds new cache invalidation feature • BD 6 and 7 support CFQUERYPARAM within cached queries • Many would look forward to CF adding that • Resources • • See Resources later: on Admin settings, articles covering caching options

  18. Candidates for Query Caching • Any one or more of these would make a query a good candidate for caching • Called very frequently • Whether in a page, across many pages, or even many users • Or returns data that takes long to gather • Or returns data that does not change frequently • Or has no or a small number of input arguments causing variation in SQL • Given the limiter for cached queries • CachedAfter better suited when data being cached is updated at a set time (like 2am)

  19. Top 3 Caching: Template Caching • Basics • Page Request Process • Size is set in Admin • Trusted cache setting • Surprises • Ashwin on admin sizing (blog entry URL’s offered later) • Managing/Flushing • Challenge with trusted cache, when updating source code • To reload a single page, must run/compile it while trusted cache disabled • Cfcompile.bat command line tool in C:\CFusionMX7\bin • CF7 Admin new button: flushes entire cache • Also cfide.adminapi.runtime.cfc clearTrustedCache() method • Both clear the entire cache (all templates) • CF7 gateway-based tool to track changes to a directory •

  20. Top 3 Caching: Template Caching (cont) • Gotchas • Not about caching output of templates! • Problem of old files being ftp'ed or extracted from version control (or zip) – if change not detected • • Monitoring • In CF5, had available “cache pops” stat (cp/sec) in cfstat, perfmon, getmetricdata() • CFMX no longer provides that data (there, but -1) • Available admin CFC methods, in runtime.cfc • Get/setCacheProperty() for TrustedCache, TemplateCacheSize • CF8 will add monitoring • how many templates in cache, est. size, cache hit ratio • Resources • See Resources later for more on Admin settings, and articles covering multiple caching options

  21. Top 3 Caching: Page Caching • Basics: CFCache • CF saves generated HTML content of a page into a temp file • Under covers, that is served to users on later requests • Useful when entire page’s execution (not just CFQUERY) takes time but results don’t change much between frequent repeated requests • Demo • Managing • Can control duration of cached result • using TimeSpan attribute and CreateTimeSpan() function • If none, cached result is “permanent” (until flushed manually) • Can choose location of cached result, server or client • Now defaults to caching on server and client • Storing as ClientCache is key for personalized pages • For server cache, can specify Directory attribute to name where to store cached (tmp) files • Defaults to cf_root/cache • Supports both HTTP and HTTPS, as well as USERNAME/Password for WS-protected files

  22. Top 3 Caching: Page Caching (cont) • Surprises • Many changes as of CFMX 6 • Used to cache only on server. 4.5 added cache on client • CFMX defaults to doing both (was “optimal” option in 4.5+) • Can vary based on user input (URL variables) • http://server/view.cfm?id=1 and http://server/view.cfm?id=2 are cached separately • Can vary based on sessions! • No longer uses old file to track cached results stored on server • Flushing • Available action=“flush” • Can point to directory where cache results are stored • Available expireURL attribute to flush entries cached based on URL • Supports use of wildcards • Cache is also flushed if you change the CFML file itself

  23. Top 3 Caching: Page Caching (cont) • Gotchas • Caches entire page: no support for partial page caching • Will discuss alternate solution later • While it recognizes URL vars, does not recognize different path_info values • Search-engine-safe URLs using this will serve wrong cached result (in other words, as if path_info ignored) • CF puts a CFML comment at top of generated output • Makes page non-standard, forces IE to “quirks” mode • I couldn’t get ClientCache feature to work at all • Monitoring • Since page caching is not “in memory”, no need to track that concern • But the filespace used for caching may be important • Would do well to monitor directory space (default or as named) • Not aware of any auto-cleanup process, though one may well exist • No real means to monitor if a page is being cached • other than to view that embedded comment mentioned above • Resources •

  24. Several More Caching Options • Caching queries in scopes • Caching queries to disk • Partial page caching • Caching CFCs • Caching CFHTTP, Web Service calls • Still more CF caching features • Perhaps unexpected caching

  25. More: Caching Queries in Scopes • Basics • Did you know you can put query results into a scope other than local/variables? • Either in CFQUERY NAME or by assignment • Demo • If placed in shared scope, remains in memory • Can use session, application, or server scopes • Before CachedWithin and CachedAfter were added, this was the technique • Because they don’t permit CFQueryParam, many still use the approach • Resources • Ample coverage in resources at end of presentation • Surprise • Consider this benefit: if DB goes down, app could keep running since it’s not talking to it • if need nothing but cached data

  26. More: Caching Queries in Scopes (cont) • Managing • If in shared scope, will timeout when scope does • Or you can manually manage purge/refresh • when time passes or data changes • Must also manage “singleton” creation • Ensure scope only loaded with data once for desired cache lifetime • All the above must be done with code • In application.cfm, with code to test for existence • Or in Application.cfc onSessionstart, onApplicationstart • Or in a CFC that does the work and returns results • Gotchas • You also must manually manage max use of memory • No easy way to track query size or total cache use • Some coders duplicate shared scopes on each request • Application.cfm/OnRequestEnd.cfm • Beware that cached queries could balloon that process • Also doesn’t work well when app spread across cluster • Might be better to use disk rather than memory…—

  27. More: Caching Queries to Disk • Previous page discusses caching to memory. Could instead cache to disk • Available CF_ExtremeQueryCache custom tag does this • Resource • “Caching Queries to Disk or to Memory with ColdFusion” • • Uses wddx serialization • Which is something anyone could use for custom caching solution

  28. More: Partial Page Caching • CFSAVEContent • Can save any portion of output generated in a page by wrapping in cfsavecontent • Can save to a local or shared scope variable • Can then manage that as with query caching in scopes • Resources • • See Resources later for articles covering multiple caching options • •

  29. More: Partial Page Caching (cont) • Various custom alternatives • “Caching options in ColdFusion” • • CF_CacheOMatic • • Brandon Purcell’s CF_Accelerate • • Inspired by older CF_SuperCache • Extension to that to support file-based caching • • Ray Camden’s ScopeCache • • Andy Powell’s JohnnyCache • • CF_TurboCache • • CF_HyperCache • • CFCacheContent (in BD) • Adds option to cache to disk, as well, and more management options

  30. More: Caching CFCs • Invocation of CFCs can be expensive • With CFOBJECT, createObject you could save instance in shared scope • Again, either using shared scope to hold result, or by copying to it • <cfobject component="cfcname" name="application.myinstance"> • Gotchas • Has same management challenges as scope-cached queries • Singleton creation, memory max use, refresh challenges • Bigger problem regarding changes to CFC code once cached • Will no longer see code changes reflected immediately. Must reload CFC into scope • Might code for a cachereset URL var (processed in in app.cfm) • Or create a single page that you call as a developer to do reload the cache • If CFINVOKE used, no instance name created, so no means to cache instance • Resources • “Caching ColdFusion Components (CFCs) in Shared Memory” • • Ashwin Matthew’s SoftCache CFC •

  31. More: Caching CFHTTP, Web Service calls • When you use CFHTTP or invoke a web service, that too can be an expensive, slow operation • Yet many forget to consider caching the result • Again, can save result in a shared scope • Same benefits and challenges as before • Resource • “Cache complex Web service objects with ColdFusion” •

  32. More: Still More CF Caching Features • CF7 charting (disk and memory) • There are built-in, configurable caching options • See CF Admin • Available monitoring • Admin API Runtime.cfc getChartProperty("CacheSize") as well as cachetype and cachepath • Can also set with setchartproperty • Verity K2 Collection caching • C:\CFusionMX7\verity\k2\common\K2ServerConfigurationMetaData.xml • DB driver connection pooling • “Maintain connections”, and more in CF Admin DSN

  33. More: Perhaps Unexpected Caching • Multiple virtual hosts sharing same code • Cacherealpath option in CF Admin • Available get/setCacheProperty() in Runtime Admin CFC, for CacheRealPath • CF caches hostname, affects some tags (like CFHTTP) • • • • WebService Proxy stub is cached • Can clear that programmatically in CFMX 6, 7 •

  34. More: Perhaps Unexpected Caching (cont) • CF compilations seem cached • • CF caching 404 error pages • • Browser caching authentication state • • Impact of using Select * in CFQUERYPARAM • •

  35. Caching Outside of CF • Caching content on server • Content caching in browser • Caching on the database • JVM caching/garbage collection

  36. Non-CF: Caching Content on Server • Various tools, means to cache content not in CF, but in the web server • Options built into web servers • • Tools like Squid, xcache, CacheRight • • • • Servlet filter-based caching • Since CFMX is built atop J2EE, can leverage servlet filters • And there are filters which implement page content caching for you • as alternative to CFCACHE, for instance • See my article for more info, and resources • • Another alternative: • Proxy caching •

  37. Non-CF: Caching Content on the Network (Lan/Wan) • Cache content on the router/network • Alternatives such as • F5 Web Accelerator • • Akamai Web Application Accelerator • • Radware Cache Server Director •

  38. Non-CF: Caching Content in Browser • Basics • Happens automatically, browser caches pages in memory, on disk • Can be controlled in browser setup • Can also be controlled in server-side directives to control caching a page • <cfheader name="Expires" value="#GetHttpTimeString(Now())#"> • Can help or hurt you • Surprises • Caching can happen with scripts/stylesheets in src files, images, errors • Can often use Ctrl-click on refresh button, or ctrl-f5, to force browser refresh • Any new URL variables will force browser to refresh • Can use any ?xxx to force refresh • Can also be controlled on web server, for static filetypes at least • In IIS:

  39. Non-CF: Caching Content in Browser (cont) • Gotchas • Different browsers honor different cache expiration directives • Tempting to change your browser to “never cache”. Be careful, now not like your users • Problem when outputting alternative content types • May never see result of changes to your code • May need to open new browser to force browser to open the page correctly • Browser caching can also affect data sent to flex/ajax/flash apps • Use techniques above to force refresh on demand, or in coding • Be aware of Google Web Accelerator • • Resources • • Caching Tutorial for Web Authors and Webmasters • • "Preventing Caching in Flash Player“, • Can also configure in web server •

  40. Non-CF: Caching on the Database • Database connection pooling • Connecting from CF to DB can be expensive, especially as traffic grows • Option in CF Admin datasource setup to “maintain connections” • Also “advanced settings” and connection string options to control pooling further • Buffer caching • Database engine will likely most do some caching of retrieved content, in memory of the DB server • Traditionally, this has been just internal “pages” or “blocks”, held in “buffers” • DB engine controls entirely what’s cached, flushed • Though configurable in DBMS, perhaps per database • Query plan caching • Your SQL statement causes creation of a “query plan” in most DBs • DB engine will typically cache that, internally • You can affect it with CFQUERYPARAM, or hints (depending on DB) • Maxpooledstatements in SQL Server driver • Its impact on prepared statements in SQL Server

  41. Non-CF: Caching on the Database (cont) • SQL statement result caching • More recently, some DBMSs have started supporting caching of results per SQL Statement • MySQL offers query caching • “The MySQL Query Cache” • • Oracle 11G adding SQL caching • Resources • See resources at end for general Admin page configuration docs • • Analyzing SQL Server 2000 Data Caching •

  42. Non-CF: JVM Caching/Garbage Collection • Underlying JVM does its own caching of objects in memory • All your CF Code creates some object, perhaps many • Some may end with conclusion of request • Some may live on much longer time • When objects no longer needed, they’re “garbage collected” • See Peter Frietag’s articles • • • • Many others, such as: •

  43. Flushing Strategies Summary • Rolling interval since last use • Fixed interval at recurring time • All at once • By storing subsets in a struct • When data changes • Based on some other event • On server restart • On application, session restart • Each of the options mentioned in this talk use one or more • Just keep them in mind for your own solutions • And that’s it for the caching options we’ll discuss

  44. Resources for Learning More • This presentation (and its details) • Available at • CFMX documentation • See CFML Reference pointers, as well as • CFMX Developers Guide, “Optimizing ColdFusion applications” • • Cache-related Admin settings • • Admin API •

  45. Resources for Learning More (cont) • “Classic” Articles • Caching in ColdFusion (CF 6 era, covers the “big three”) • • Programmatic Caching in ColdFusion (from CF5 era) • • Query Caching in ColdFusion (also CF5-era) • • • Recent blog entries from Ashwin Matthews of Adobe • • • •

  46. Resources for Learning More (cont) • Community MX Articles (subscribers) • Beyond ColdFusion Query Basics • • A Quick Approach to Home-Brewed Content Caching in ColdFusion - Part 1: The Theory • • A Quick Approach to Home-Brewed Content Caching in ColdFusion - Part 2: The CFC • • A Quick Approach to Home-Brewed Content Caching in ColdFusion - Part 3: In-Memory Caching •

  47. Other Resources • CF-specific caching resources • “Strategies For Scaling ColdFusion: Caching” • • Various techniques • • “Good Developers Practice Safe Query Caching” • • “Custom Caching Solutions”, Article • • Generic caching resources • • “Web Caching and Content Delivery Resources” • • “Web Caching: Making the Most of Your Internet Connection” • • “Caching and Cache Control” •

  48. Summary • Introduced caching concepts • Covered over a dozen forms of caching • Query caching: tag- and scope-based • Page caching: Full and partial • CFCACHE, CFSAVECONTENT, custom tags • Template caching and trusted cache setting • Several more caching options • Caching queries in scopes, or to disk • Caching CFCs • Caching CFHTTP, Web Service calls • Still more CF caching features, and perhaps unexpected caching • Caching outside of CF • Caching content on server • Content caching in browser • Caching on the database • JVM caching/garbage collection • Introduced various gotchas and surprises • Showed how to manage/flush cache and monitor cache effectiveness

  49. Questions on presentation • Charlie Arehart • • I’d really appreciate your feedback • • Also available for setup and implementation consulting • Also other developer productivity coaching, system admin and tuning support, and more • Remote or on-site • New Per-minute Phone/Web support •