Muahahaha!<br />I just bit a byte!<br />Why is my code so slow?<br />Caching & Performance<br />In ColdFusion<br />Present...
Programming For Speed<br />Write OOP Applications<br />OOP applications run faster and perform better than procedural code...
Programming For Speed<br />Exit As Quickly As Possible<br />Logic in applications should always be written to exit as quic...
Programming For Speed<br />Limit Nested Loops<br />Nested loops are sometimes unavoidable, but they should be avoided when...
Programming For Speed<br />Know Your ColdFusion<br />ColdFusion offers a number of handy functions which should be avoided...
Programming For Speed<br />Test Execution Speed<br />The most often overlooked data point in testing is execution speed.<b...
Programming For Speed<br />Use Stored Procedures<br />Stored procedures take away much of the overhead involved with deali...
Cashing In On Cache<br />Use Caching<br />Caching is a fancy way of saying – keep this just as it is and give it to anyone...
Cashing In On Cache<br />Page Caching<br /><cfcachetimespan="#CreateTimespan(1, 0, 0, 0)#" directory="e:/temp/page_cache“>...
Cashing In On Cache<br />Partial Page Caching<br /><cfsavecontentvariable=“SESSION.myVar”></cfsavecontent><br />One handy ...
Cashing In On Cache<br />Query Caching<br /><cfquerydatasource=“myds" name=“qGetStates" cachedWithin="#CreateTimeSpan(0,1,...
Cashing In On Cache<br />Flushing The Page Cache<br /><cfcacheaction=“flush” [directory="e:/temp/page_cache“]><br />You ca...
Cashing In On Cache<br />Flushing The Query Cache<br /><cfquerydatasource=“myds" name=“qGetStates" cachedWithin="#CreateTi...
Cashing In On ORM Cache<br />ORM and Caching – Session and Secondary Level Caches<br />Cache data of a persistent object<b...
Additional Resources<br />ORM and Caching – Session and Secondary Level Caches<br />Optimizing ColdFusion Applications<br ...
Upcoming SlideShare
Loading in …5
×

Caching & Performance In Cold Fusion

1,684 views

Published on

This presentation was recently given at the NVCFUG meeting in June, 2011 and covers basic caching and performance topics for the active ColdFusion developer.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,684
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Caching & Performance In Cold Fusion

  1. 1. Muahahaha!<br />I just bit a byte!<br />Why is my code so slow?<br />Caching & Performance<br />In ColdFusion<br />Presented By DenardSpringle<br />NVCFUG June 2011<br />
  2. 2. Programming For Speed<br />Write OOP Applications<br />OOP applications run faster and perform better than procedural coded applications. OOP application’s use of encapsulation allows only those functions which need to be fired to fire, and those that don’t… don’t.<br />Java, the underlying engine behind ColdFusion, is a true Object Oriented language. OO in ColdFusion has been developed to map as much as possible to the Java model that underlies the ColdFusion engine. This allows CF code to execute faster when written in an OO fashion.It also makes for happy developers, managers andcustomers. <br />O… O… Who?<br />
  3. 3. Programming For Speed<br />Exit As Quickly As Possible<br />Logic in applications should always be written to exit as quickly as possible under all circumstances.<br />Programmers have a tendency to write logic that checks for error conditions passively (at the end) rather than actively (at the beginning). This is a time honored tradition and one that is continually perpetuated in coding books. Stop it! Always check for error conditions first and plan the fastest exit strategy for all code. The less code that has to execute, the faster your application will run.<br />Where’s the Exit?!<br />
  4. 4. Programming For Speed<br />Limit Nested Loops<br />Nested loops are sometimes unavoidable, but they should be avoided whenever possible.<br />Nested looping is both a blessing and a curse to developers. On the one hand it offers much needed functionality to iterate over multiple sets of inter-related data points. On the other hand, it kills servers. There are often alternatives to nested loops that developers ignore (mapping your data into a struct, for example) and many developers simply abuse the ability to nest loops without realizing the implicationsfor the performance of the application.<br />I’m Loopy!<br />
  5. 5. Programming For Speed<br />Know Your ColdFusion<br />ColdFusion offers a number of handy functions which should be avoided like the plague.<br />ColdFusion is great. It gives us things like inline if (IIF) which one would logically assume runs as quickly as an if-then-else, but that’s a poor assumption. There are a number of functions in ColdFusion that don’t operate on the premise many developers think they do (structFind(), <cfmodule>, incrementValue(), etc.). As such, avoiding the processor intensive functions for those that behave better is always a preferred choice (e.g. struct.key, <cfinclude>, a=a+1).<br />Uhh...<br />
  6. 6. Programming For Speed<br />Test Execution Speed<br />The most often overlooked data point in testing is execution speed.<br />Testing your code for execution speed is key to understanding which functions take too long. Database queries, math intensive reporting and internet protocol requests are no brainers for taking a long time and are often suspect enough to improve application performance significantly. There are other, less obvious, suspects to consider however – locking shared scope variables, for example. Using GetTickCount() at the beginning and end of your functions will help you isolate problem areas and keep you from pulling out hair. <br />Faster! Faster!<br />
  7. 7. Programming For Speed<br />Use Stored Procedures<br />Stored procedures take away much of the overhead involved with dealing with database queries.<br />Stored procedures take a tremendous load off of your ColdFusion server and place it squarely in the hands of your database server. Understand that this is literally a transfer of CPU and memory load from your application server to your database server. Stored procedures execute faster on most database servers than a query does from ColdFusion so it is typically a win/win situation.<br />I should store *this* guy!<br />
  8. 8. Cashing In On Cache<br />Use Caching<br />Caching is a fancy way of saying – keep this just as it is and give it to anyone who asks for it.<br />For performance benefits, nothing beats caching. Caching of pages and queries, and indeed with ORM, allows you to provide quicker responsiveness for both static and dynamic* content. Pages and queries (e.g. ‘US States’ table in a DB) that never change should always be cached. ColdFusion doesn’t have to process the page or the query again until the cache expires, providing instant-on access to pages andcritical data (e.g. reporting).<br />*dynamic content that changes infrequently<br />Cash?!<br />
  9. 9. Cashing In On Cache<br />Page Caching<br /><cfcachetimespan="#CreateTimespan(1, 0, 0, 0)#" directory="e:/temp/page_cache“><br />You tell ColdFusion to cache the page results by putting a cfcache tag on your ColdFusion page before code that outputs text. The tag lets you specify the following information:<br />Whether to cache the page results on the server, the client system, or both. The default is both. <br />The directory on the server in which to store the cached pages. <br />The time span that indicates how long the page lasts until it is automatically flushed. <br />Oh, *Cache*<br />
  10. 10. Cashing In On Cache<br />Partial Page Caching<br /><cfsavecontentvariable=“SESSION.myVar”></cfsavecontent><br />One handy but not very obvious way to use <cfsavecontent> is in the Application or Session scope of your application (typically in OnApplicationStart and/or OnSessionStart). By saving infrequently changing but frequently presented complex data in this way you can significantly decrease individual page load times. Because of the overhead of locking shared scope variables, use this technique only if the processing overhead of generating the output is more than theprocessing overhead of locking the shared scope.<br />
  11. 11. Cashing In On Cache<br />Query Caching<br /><cfquerydatasource=“myds" name=“qGetStates" cachedWithin="#CreateTimeSpan(0,1,0,0)#"> <br />The cfquery tag cachedWithin attribute tells ColdFusion to save the results of a database query for a specific period of time. This way, ColdFusion accesses the database on the first page request, and does not query the database on further requests until the specified time expires. Using the cachedWithin attribute can significantly limit the overhead of accessing databases that do not change rapidly. This technique is useful if the database contents only change at specific, known times, or the query does not require up-to-date results.(e.g. Google Adwordsreports are delayed 1 hr.)<br />Day, Hour, Minute, Second!<br />
  12. 12. Cashing In On Cache<br />Flushing The Page Cache<br /><cfcacheaction=“flush” [directory="e:/temp/page_cache“]><br />You can use the cfcache tag with the action="flush" attribute to immediately flush one or more cached pages. You can optionally specify the directory that contains the cached pages to be flushed and a URL pattern that identifies the pages to flush. If you do not specify a URL pattern, all pages in the directory are flushed. The URL pattern can include asterisk (*) wildcards to specify parts of the URL that can vary. If you have a ColdFusion function that updates data that you use in cached pages, that function should include a cfcache tag that flushes all pages that use the data.<br />FLUSH!<br />
  13. 13. Cashing In On Cache<br />Flushing The Query Cache<br /><cfquerydatasource=“myds" name=“qGetStates" cachedWithin="#CreateTimeSpan(0,0,0,0)#"> <br />One of the first hurdles most developers face implementing query caching is how to undo the cache. Often in testing phases one discovers the database table didn’t have all the data it needed or needs a new field (or two) ‘manager special’ but one already has a cached query and doesn’t understand why they’re always getting the same results. I know it seems obvious, but it really isn’t when you first play with query caching ;)Setting your cachedWithintimespan to nothing (0,0,0,0)flushes the cached query and immediately calls thedatabase for your precious missing data! w00t!<br />That’s what she said!<br />
  14. 14. Cashing In On ORM Cache<br />ORM and Caching – Session and Secondary Level Caches<br />Cache data of a persistent object<br /><cfcomponent persistent="true" schema="APP" table="Artists" cachename="artist" cacheuse="read-only"><br />Cache the association data of a persistent object<br /><cfpropertyname="art" fieldtype="one-to-many" cfc="CArt" fkcolumn="ArtID" cachename="ArtistArts" cacheuse="read-only"><br />Cache query data<br />availableArts= ORMExecuteQuery("from CArt where issold=0", {}, false, {cacheable=true, cachename="availableArtsQuery"});<br />See the Developing ColdFusion 9 Applications guide / ColdFusion ORM / Performance optimization / Cachingsection for full details of ORM caching techniques.<br />Nom NomNom<br />
  15. 15. Additional Resources<br />ORM and Caching – Session and Secondary Level Caches<br />Optimizing ColdFusion Applications<br />There is no blog post for ColdFusion tags to avoid!!<br />Performance tuning ColdFusion servers<br /> We hope you enjoyed this!<br />w00t!<br />

×