Transcript Document
Are You “Cashing In” on Caching? An in-depth look at the caching methods available within ColdFusion applications Tyson Vanek Senior Technical Consultant Webapper Services, LLC Presentation Overview Caching overview and basic concepts Identifying candidates for caching ColdFusion’s template caching options ColdFusion’s native query caching methods ColdFusion’s native page/content caching methods Alternatives to ColdFusion’s native caching methods June 27th- 30th 2007 www.cfunited.com About Tyson Vanek Independent Consultant 12 years of ColdFusion experience Began working with ColdFusion v1.0 Using ColdFusion on every project since July, 1995 Former employee of Allaire/Macromedia Consulting Services Division Noteworthy Accomplishments Currently engaged full-time with Webapper Services, LLC Certified ColdFusion Developer Contributing author - “Inside ColdFusion MX” Contributing author - Adobe’s ColdFusion Developer Center 2 year author of BrainBench ColdFusion certification exams (5 & MX6) Noteworthy Clients Footlocker Eastbay Lockheed Martin Abbott Laboratories University of Virginia Adelphia Communications Williams-Sonoma Chicago Mercantile Exchange International Broadcast Bureau / Voice of America Discovery Communications / Discovery Channel / unitedstreaming June 27th- 30th 2007 www.cfunited.com Caching Basics June 27th- 30th 2007 www.cfunited.com What is Caching? A way of saving data after it has been originally computed so that subsequent requests for the same data are executed more quickly than the original call by retrieving the saved data instead of computing real-time June 27th- 30th 2007 www.cfunited.com What Can Be Cached? CFML Templates at the Server level Trusted Cache Class files Data Objects Query record sets Complex variables (e.g. structures, arrays, objects) Simple variables (e.g. strings, text blocks) Web Service Invocation Responses CFCs Rendered HTML Content Entire pages HTML blocks June 27th- 30th 2007 www.cfunited.com Why Should I Be Caching? Alleviates load from the server(s) Makes the server faster and “healthier” Allows the server to support more load Decreases page load time for the end user Improves the user’s experience Allows the user to accomplish tasks more quickly June 27th- 30th 2007 www.cfunited.com Defining Good Cache Candidates Items that can be described by any/all of the following characteristics: Global to all users Used throughout the application and accessed frequently Not subject to frequent change Not subject to a high number of variants o Limited number of input arguments and distinct values Takes a significant amount of time to render in real-time when not cached (application performance bottlenecks) o o o o o June 27th- 30th 2007 Complex nested CFML logic Database I/O CFC invocation CFHTTP calls Web Service calls www.cfunited.com Methods for Identifying Application Performance Bottlenecks The Old Fashioned Way Examination of ColdFusion log files o Pages logged when ColdFusion Administrator “Log slow pages taking longer than X seconds” option is enabled. Examination of ColdFusion debugging output o Displays execution times of templates, includes, custom tags, and CFC methods • o o Query execution times Cumulative template execution time CFML Language o o o Displayed in detail when ColdFusion Admin “Report Execution Times” option is enabled with “summary” mode selected getTickCount() <cflog> <cftrace> Using New Server Analytics Tools SeeFusion FusionReactor CFMX 8 Administrator June 27th- 30th 2007 www.cfunited.com Important Questions to Ask Yourself When Evaluating a Caching Candidate 1. How often do changes take place that will affect and/or invalidate the cached data? 2. Is up-to-the-second data absolutely critical? 3. How many cache variants are possible based on the variable input arguments? 4. What is the average count/size/length of each cache variant? June 27th- 30th 2007 www.cfunited.com What is a “Cache Variant”? A “cache variant” is a single set of data or content derived from the application code and based on a single combination of values for any variable input arguments Examples Variant #1 <cfset lastNameLetter = “A”> <cfquery name=“qUsers” datasource=“myDSN”> SELECT * FROM tblUser WHERE left(lastName,1) = ‘#lastNameLetter#’ </cfquery> Variant #2 <cfset lastNameLetter = “Z”> <cfquery name=“qUsers” datasource=“myDSN”> SELECT * FROM tblUse WHERE left(lastName,1) = ‘#lastNameLetter#’ </cfquery> June 27th- 30th 2007 www.cfunited.com Calculating Cache Variants Query Example #1 Query Example #2 SQL Statement: SELECT * FROM tblUser WHERE userid = #userid# AND active = #active# SQL Statement: SELECT * FROM tblUser WHERE department = #department# AND age = #age# AND gender = #gender# AND active = #active# But how many likely variants exist? Variable Input Arguments: userid (1,000 distinct values) active (2 distinct values) Variable Input Arguments: department age gender active Variants: 1,000 x 2 = 2,000 Variants: 2,000 potential query variants exist June 27th- 30th 2007 (50 distinct values) (100 distinct values) (2 distinct values) (2 distinct values) 50 x 100 x 2 x 2 = 20,000 20,000 potential query variants exist www.cfunited.com Why Calculating the Cache Variants is Important Each variant represents an instance of data/content that could potentially be cached The more items in the cache, the larger the cache becomes in size and complexity Size may affect storage decisions Complexity may affect access/seek time to cached items With larger caches, attention must be given to server resources (e.g. disk space, memory allocation, etc) The size of the cache may play a role in determining the best location to store the cache June 27th- 30th 2007 www.cfunited.com A Simple Measure of the Effects of Caching Example #1 – No Caching Example #2 - Caching Request execution time: 500ms Average requests per minute: Calculation: 60 First request execution time: Cache TTL: Cached request execution time: Average requests per minute: Calculation: 500ms 60s 20ms 60 500ms x 60 = 30s (500ms x 1) + (20ms x 59) = 1.7s 30 seconds of server time allocated per minute 1.7 seconds of server time allocated per minute 30s x 60m = 1,800s 1.7s x 60m = 102s 1,800 seconds of server time allocated per hour 102 seconds of server time allocated per hour Result: 94.3% Improvement! June 27th- 30th 2007 www.cfunited.com ColdFusion Server Template Caching Techniques June 27th- 30th 2007 www.cfunited.com Understanding ColdFusion’s Template Request Workflow June 27th- 30th 2007 www.cfunited.com Template Caching with Trusted Cached ColdFusion Administrator “Trusted Cache” setting Bypasses disk checks for more recent changes to the CFML template, caching the CFML template into RAM Impacts the “STARTUP, PARSING, COMPILING, LOADING, & SHUTDOWN” timer located in ColdFusion debugging output Typically only enabled on production servers where templates change infrequently or with scheduled releases Server-wide setting with no application-specific control Templates are cached based on file date/time stamps o May cause unexpected behavior when templates are deployed via FTP or source control promotion of a previous version of the template June 27th- 30th 2007 www.cfunited.com Template Caching with Trusted Cached (cont.) Clearing cached items Individually o Execute cached template while “Trusted Cache” setting is disabled in CF Administrator • Re-enable the “Trusted Cache” setting once the template has been executed Entire Cache o Restart ColdFusion Application Server (YIKES!) o ColdFusion Administrator “Clear Template Cache Now” button (CFMX 7+) o Admin API call (may be programmatically invoked) o Event Gateway/Admin API Tool developed by Brian Szoszorek (CFMX 7+) • http://www.adobe.com/devnet/coldfusion/articles/cacheclear.html June 27th- 30th 2007 www.cfunited.com The “Save Class Files” Option New option added with ColdFusion MX 6.1 Found in the CF Admin “Caching” settings When enabled, generates and saves a *.class file for each CFML template executed Files are stored in WEB-INF/cfclasses/ directory File-based storage means that cache persists even if ColdFusion is restarted Server-wide setting with no application-specific control June 27th- 30th 2007 www.cfunited.com Why You Should Consider NOT Enabling It Feature was automatically enabled (behind the scenes) in CFMX 6.0 due to performance issues with the compiler (javac/jikes) that was being used at the time Compiler in CFMX 6.1+ was replaced and improved CFMX 6.1 compiler is much faster, converting CFML files into bytecode Improved performance of the 6.1+ compiler has more or less eliminated the need for .class file generation/caching Creates additional overhead Potential for thousands of files in the /cfclasses/ directory ColdFusion will continue to check the date/time stamp of the .class file with every call to the corresponding .cfm template to ensure that the cached .class file is still valid (unless Trusted Cache is enabled or the template is not present in the Template Cache) Significant file/directory IO (500+ ms or more) Most customers report little or no difference with this option enabled or disabled on CFMX 6.1 or higher If there’s little or no performance to be gained, why bother with the option at all? Enabling this option adds more decision points to the ColdFusion Template Request Workflow, inherently slowing the request process. Does it even work? June 27th- 30th 2007 www.cfunited.com ColdFusion Query Caching June 27th- 30th 2007 www.cfunited.com Native Query Caching with ColdFusion Adding the cachedWithin or cachedAfter argument to the CFQUERY tag Places the query result set into ColdFusion’s application server memory for the defined length of time Queries cached using cachedWithin or cachedAfter are placed in the same cache, so there’s a possibility of re-use ColdFusion Administrator “Maximum Number of Cached Queries” setting Controls the physical number of queries that may be stored in the cache at any given time Once this threshold is reached, least recently used items in the cache will be dropped to make room for new items Setting value to “0” o In earlier versions of ColdFusion, setting this value to “0” resulted in “unlimited” query caching (extremely dangerous) o In CFMX 6.1 (6,1,0,83762)+ and CFMX 7+, setting this value to “0” results in disabling query caching • Technically, allows a single cached query Important point about query caching variants Every cached query is driven by a combination of the following items: o o o o o June 27th- 30th 2007 Query Name SQL Statement (including formatting, tabs, spaces, etc.) Datasource Username/Password Any input arguments to the SQL statement www.cfunited.com Query Caching Examples Example #1 - Using cachedWithin: Example #2 - Using cachedAfter: Query without caching: Query without caching: <cfquery name=“qTop10NewsStories” datasource=“#myDSN#” > select top 10 * from tblNews order by storyDate desc </cfquery> <cfquery name=“qTop10Videos_January2007” datasource=“#myDSN#” > select top 10 sum(countView), videoName from tblVideoView where viewDate >= ’01/01/2007’ and viewDate < ’02/01/2007’ order by sum(countView) desc </cfquery> Query with caching (15 minute cache): <cfquery name=“qTop10NewsStories” datasource=“#myDSN#” cachedWithin=“#createTimeSpan(0,0,15,0)#” > select top 10 * from tblNews order by storyDate desc </cfquery> June 27th- 30th 2007 Query with caching (after 2/1/2007): <cfquery name=“qTop10Videos_January2007” datasource=“#myDSN#” cachedAfter=“#createDateTime(2007,2,1,0,0,0)#” > select top 10 sum(countView), videoName from tblVideoView where viewDate >= ’01/01/2007’ and viewDate < ’02/01/2007’ order by sum(countView) desc </cfquery> www.cfunited.com Clearing the Query Cache In Part Execute query with cachedWithin=“#createTimeSpan(0,0,0,0)#” or cachedAfter=“#createDateTime(2100,1,1,0,0,0)#” o o Only clears the single cache variant being executed Does not clear all variants of the same cached query In Whole <cfobjectcache action=“clear”> o o Clears all cached queries for the entire server/instance Not application specific ColdFusion’s ServiceFactory purgeQueryCache() method <cfset factory = createObject(“java”, “coldfusion.server.ServiceFactory”)> <cfset datasourceService = factory.datasourceService> <cfset datasourceService.purgeQueryCache()> June 27th- 30th 2007 www.cfunited.com Pros and Cons of ColdFusion’s Native Query Caching Pros It’s built-in to ColdFusion and ready to use Cached queries are placed in memory for fastest possible access time ColdFusion’s debug output will automatically display whether a query was executed real-time or if the results were pulled from the query cache Cons No control over where the cached queries are stored or how they are organized “Maximum Number of Cached Queries” setting is server-wide, not application-specific Difficult to clear items from the cache Difficult to clear multiple items from the cache without clearing the entire cache Clearing items from the cache forces them to execute again Queries cannot be cached if they make use of <cfqueryparam> Query caching cannot be used with <cfstoredproc> o Stored procedure calls can, however, be cached if called within a <cfquery> tag (using exec, call, etc.) No way of viewing/dumping the cache in whole or in part No way to control how ColdFusion manages the cache and cache pops No way to track the size/usage of the cache Query Cache is server/instance wide, not application specific o June 27th- 30th 2007 Code from other applications may be using <cfobjectcache> or the purgeQueryCache(), thereby clearing the cache for the entire server/instance www.cfunited.com Alternative Data Caching Methods June 27th- 30th 2007 www.cfunited.com Alternative Methods for Caching Data Placing data in shared scopes instead of local variables scope <cfquery name=“scopeName.queryName”> o Server, Application, or Session scope Requires that you programmatically manage/flush cached items o Items will automatically timeout if shared scope variable times out Can be applied to queries as well as any other ColdFusion data type o o o o Structures Arrays Objects Simple Values Must beware of application code that might be performing a duplicate() of shared scopes o Placing these additional items in the shared scope for caching could negatively impact the performance of duplicating the scope No native support for clustered cache synchronization o Currently designing and developing a cross-node in-memory cache synchronization system for clustered environments June 27th- 30th 2007 www.cfunited.com Alternative Methods for Caching Data (cont.) Various available custom solutions Tyson Vanek’s <cf_extremeQueryCache> custom tag o http://www.adobe.com/devnet/coldfusion/articles/query_cache.html • • Only supports caching of query record sets Supports caching to memory or disk Tyson Vanek’s cacheItAll.cfc component o http://www.coldfusionmaster.com/cacheItAll/ Tyson Vanek’s <cf_accelerate2> custom tag o http://www.coldfusionmaster.com/accelerate2/ • • • June 27th- 30th 2007 Modified version of Brandon Purcell’s <cf_accelerate> custom tag Supports caching of both simple and complex variables Supports caching to memory or to disk www.cfunited.com ColdFusion Content Caching June 27th- 30th 2007 www.cfunited.com Content Caching with ColdFusion’s <CFCACHE> Tag <CFCACHE> Tag Place tag at beginning of any page that should be cached Options include server-side or client-side caching (action argument) Creates rendered HTML output and saves file to disk on the server or client depending on the action indicated o Cache files have .tmp extension o Cache files are simply an HTML representation of the generated content o First line of the .tmp cache file is an HTML comment that identifies the template and URL parameters used to generate the cached file • This comment might cause browser validation issues Caches content based on the timeSpan argument, much like CFQUERYs cachedWithin argument. o If no value is specified for timeSpan, the content will be cached indefinitely • Cache would have to be cleared manually Cached files (.tmp) can be stored in a directory of your specification (using the cacheDirectory argument) o Default location is $cfroot$\cache Automatically creates unique caches based on URL variables Support for unique caching by session (does it work?) June 27th- 30th 2007 www.cfunited.com Pros and Cons of Caching with <CFCACHE> Pros It’s built-in to ColdFusion and ready to use CFCACHE tag evaluates each unique URL parameter to ensure that different combinations result in different cached pages Supports both server-side and client-side caching for maximum efficiency Supports unique caching by session Cached entry is automatically expired if the CFML template is changed Cache can be cleared programmatically o o action=“flush” expireURL=“*/view.cfm?id=*” Cons Can only be used to cache an entire page, not smaller sections of HTML Limited control over how the cached pages are stored or how they are organized Difficult to view/dump the cache in whole or in part o o Can view/delete HTML .tmp file written to the cacheDirectory Files are named using an MD5 hash of the CGI.SCRIPT_NAME and CGI.QUERY_STRING • • • Might make it difficult to identify the .tmp file(s) you are looking for Programmatic clearing of cached items can be challenging Variants are not created with consideration to CGI.PATH_INFO (SES URLs) No way to control how ColdFusion manages the cache and cache pops ColdFusion will not automatically remove “expired” cache items June 27th- 30th 2007 www.cfunited.com Alternative Methods for Caching Content HTML Block caching with <cfsavecontent variable=“yourVarName”> Captures all content generated within <cfoutput> tags and saves it to the specified variable name Variable can also be placed in shared scope for caching purposes o Management options similar to those discussed earlier regarding shared scope caching of complex variables Various available custom solutions <cf_superCache> o http://www.cfprimer.com/download.cfm?ffFile=supercache.cfm <cf_accelerate> <cf_cacheOmatic> o o http://www.bpurcell.org/blog/index.cfm?mode=entry&entry=963 http://www.devx.com/webdev/Article/27618 <cf_turboCache> <cf_hyperCache> Ray Camden’s ScopeCache o o o http://www.hotfusion.co.uk/TurboCache/index.htm http://www.pixl8.co.uk/index.cfm/pcms/site.products.CF_Hypercache/ http://ray.camdenfamily.com/downloads/scopecache.zip Andy Powell’s JohnnyCache Tyson Vanek’s cacheItAll.cfc component o o http://johnnycache.riaforge.org/ http://www.coldfusionmaster.com/cacheItAll/ Tyson Vanek’s <cf_accelerate2> custom tag o June 27th- 30th 2007 http://www.coldfusionmaster.com/accelerate2/ www.cfunited.com Summary Discussed basic caching concepts and considerations Outlined methods for finding and qualifying good cache candidates Outlined methods for identifying application bottlenecks Reviewed ColdFusion’s built-in template caching settings and effects Reviewed ColdFusion’s built-in query caching mechanisms Presented alternative methods for caching queries and complex data types Reviewed ColdFusion’s built-in page caching mechanisms Presented alternative methods for caching entire pages or smaller sections of content June 27th- 30th 2007 www.cfunited.com Questions? June 27th- 30th 2007 www.cfunited.com