Are You Cashing In on Caching - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Are You Cashing In on Caching

Description:

Using ColdFusion on every project since July, 1995 ... Williams-Sonoma. Chicago Mercantile Exchange. International Broadcast Bureau / Voice of America ... – PowerPoint PPT presentation

Number of Views:261
Avg rating:3.0/5.0
Slides: 35
Provided by: Liz21
Category:

less

Transcript and Presenter's Notes

Title: Are You Cashing In on Caching


1
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

2
Presentation Overview
  • Caching overview and basic concepts
  • Identifying candidates for caching
  • ColdFusions template caching options
  • ColdFusions native query caching methods
  • ColdFusions native page/content caching methods
  • Alternatives to ColdFusions native caching
    methods

3
About Tyson Vanek
  • Independent Consultant
  • Currently engaged full-time with Webapper
    Services, LLC
  • 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
  • Certified ColdFusion Developer
  • Contributing author - Inside ColdFusion MX
  • Contributing author - Adobes 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

4

Caching Basics
5
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

6
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

7
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 users experience
  • Allows the user to accomplish tasks more quickly

8
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
  • 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)
  • Complex nested CFML logic
  • Database I/O
  • CFC invocation
  • CFHTTP calls
  • Web Service calls

9
Methods for Identifying Application Performance
Bottlenecks
  • The Old Fashioned Way
  • Examination of ColdFusion log files
  • Pages logged when ColdFusion Administrator Log
    slow pages taking longer than X seconds option
    is enabled.
  • Examination of ColdFusion debugging output
  • Displays execution times of templates, includes,
    custom tags, and CFC methods
  • Displayed in detail when ColdFusion Admin Report
    Execution Times option is enabled with summary
    mode selected
  • Query execution times
  • Cumulative template execution time
  • CFML Language
  • getTickCount()
  • ltcfloggt
  • ltcftracegt
  • Using New Server Analytics Tools
  • SeeFusion
  • FusionReactor
  • CFMX 8 Administrator

10
Important Questions to Ask Yourself When
Evaluating a Caching Candidate
  • How often do changes take place that will affect
    and/or invalidate the cached data?
  • Is up-to-the-second data absolutely critical?
  • How many cache variants are possible based on the
    variable input arguments?
  • What is the average count/size/length of each
    cache variant?

11
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 1ltcfset lastNameLetter Agtltcfquery
    nameqUsers datasourcemyDSNgt SELECT FROM
    tblUser WHERE left(lastName,1)
    lastNameLetterlt/cfquerygt
  • Variant 2ltcfset lastNameLetter Zgtltcfquery
    nameqUsers datasourcemyDSNgt SELECT FROM
    tblUse WHERE left(lastName,1)
    lastNameLetterlt/cfquerygt

12
Calculating Cache Variants
  • Query Example 1
  • SQL StatementSELECT FROM tblUserWHERE userid
    useridAND active active
  • Variable Input Arguments
  • userid (1,000 distinct values) active (2
    distinct values)
  • Variants
  • 1,000 x 2 2,0002,000 potential query
    variants exist
  • Query Example 2
  • SQL StatementSELECT FROM tblUserWHERE
    department departmentAND age ageAND
    gender genderAND active active
  • Variable Input Arguments
  • department (50 distinct values)age (100
    distinct values)gender (2 distinct
    values)active (2 distinct values)
  • Variants
  • 50 x 100 x 2 x 2 20,00020,000 potential
    query variants exist

But how many likely variants exist?
13
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

14
A Simple Measure of the Effects of Caching
  • Example 1 No Caching
  • Request execution time 500ms
  • Average requests per minute 60
  • Calculation500ms x 60 30s30 seconds of
    server timeallocated per minute30s x 60m
    1,800s1,800 seconds of server time allocated
    per hour
  • Example 2 - Caching
  • First request execution time 500ms
  • Cache TTL 60s
  • Cached request execution time 20ms
  • Average requests per minute 60
  • Calculation(500ms x 1) (20ms x 59)
    1.7s1.7 seconds of server timeallocated per
    minute1.7s x 60m 102s102 seconds of server
    timeallocated per hour
  • Result 94.3 Improvement!

15

ColdFusion ServerTemplate Caching Techniques
16
Understanding ColdFusions Template Request
Workflow
17
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
  • May cause unexpected behavior when templates are
    deployed via FTP or source control promotion of a
    previous version of the template

18
Template Caching with Trusted Cached (cont.)
  • Clearing cached items
  • Individually
  • 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
  • Restart ColdFusion Application Server (YIKES!)
  • ColdFusion Administrator Clear Template Cache
    Now button (CFMX 7)
  • Admin API call (may be programmatically invoked)
  • Event Gateway/Admin API Tool developed by Brian
    Szoszorek (CFMX 7)
  • http//www.adobe.com/devnet/coldfusion/articles/ca
    checlear.html

19
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

20
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 theres 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?

21

ColdFusion Query Caching
22
Native Query Caching with ColdFusion
  • Adding the cachedWithin or cachedAfter argument
    to the CFQUERY tag
  • Places the query result set into ColdFusions
    application server memory for the defined length
    of time
  • Queries cached using cachedWithin or cachedAfter
    are placed in the same cache, so theres 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
  • In earlier versions of ColdFusion, setting this
    value to 0 resulted in unlimited query
    caching (extremely dangerous)
  • 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
  • Query Name
  • SQL Statement (including formatting, tabs,
    spaces, etc.)
  • Datasource
  • Username/Password
  • Any input arguments to the SQL statement

23
Query Caching Examples
  • Example 1 - Using cachedWithin
  • Query without caching
  • ltcfquery
  • nameqTop10NewsStories
  • datasourcemyDSN
  • gt
  • select top 10
  • from tblNews
  • order by storyDate desc
  • lt/cfquerygt
  • Query with caching (15 minute cache)
  • ltcfquery
  • nameqTop10NewsStories
  • datasourcemyDSN
  • cachedWithincreateTimeSpan(0,0,15,0)
  • gt
  • select top 10
  • Example 2 - Using cachedAfter
  • Query without caching
  • ltcfquery
  • nameqTop10Videos_January2007
  • datasourcemyDSN
  • gt
  • select top 10 sum(countView), videoName
  • from tblVideoView
  • where viewDate gt 01/01/2007
  • and viewDate lt 02/01/2007
  • order by sum(countView) desc
  • lt/cfquerygt
  • Query with caching (after 2/1/2007)
  • ltcfquery
  • nameqTop10Videos_January2007
  • datasourcemyDSN
  • cachedAftercreateDateTime(2007,2,1,0,0,0)

24
Clearing the Query Cache
  • In Part
  • Execute query with cachedWithincreateTimeSpan(0
    ,0,0,0) or cachedAftercreateDateTime(2100,1,1
    ,0,0,0)
  • Only clears the single cache variant being
    executed
  • Does not clear all variants of the same cached
    query
  • In Whole
  • ltcfobjectcache actioncleargt
  • Clears all cached queries for the entire
    server/instance
  • Not application specific
  • ColdFusions ServiceFactory purgeQueryCache()
    method
  • ltcfset factory createObject(java,
    coldfusion.server.ServiceFactory)gt
  • ltcfset datasourceService factory.datasourceServi
    cegt
  • ltcfset datasourceService.purgeQueryCache()gt

25
Pros and Cons of ColdFusions Native Query Caching
  • Pros
  • Its built-in to ColdFusion and ready to use
  • Cached queries are placed in memory for fastest
    possible access time
  • ColdFusions 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
    ltcfqueryparamgt
  • Query caching cannot be used with ltcfstoredprocgt
  • Stored procedure calls can, however, be cached if
    called within a ltcfquerygt 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
  • Code from other applications may be using
    ltcfobjectcachegt or the purgeQueryCache(), thereby
    clearing the cache for the entire server/instance

26

Alternative Data Caching Methods
27
Alternative Methods for Caching Data
  • Placing data in shared scopes instead of local
    variables scope
  • ltcfquery namescopeName.queryNamegt
  • Server, Application, or Session scope
  • Requires that you programmatically manage/flush
    cached items
  • Items will automatically timeout if shared scope
    variable times out
  • Can be applied to queries as well as any other
    ColdFusion data type
  • Structures
  • Arrays
  • Objects
  • Simple Values
  • Must beware of application code that might be
    performing a duplicate() of shared scopes
  • 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
  • Currently designing and developing a cross-node
    in-memory cache synchronization system for
    clustered environments

28
Alternative Methods for Caching Data (cont.)
  • Various available custom solutions
  • Tyson Vaneks ltcf_extremeQueryCachegt custom tag
  • http//www.adobe.com/devnet/coldfusion/articles/qu
    ery_cache.html
  • Only supports caching of query record sets
  • Supports caching to memory or disk
  • Tyson Vaneks cacheItAll.cfc component
  • http//www.coldfusionmaster.com/cacheItAll/
  • Tyson Vaneks ltcf_accelerate2gt custom tag
  • http//www.coldfusionmaster.com/accelerate2/
  • Modified version of Brandon Purcells
    ltcf_accelerategt custom tag
  • Supports caching of both simple and complex
    variables
  • Supports caching to memory or to disk

29

ColdFusion Content Caching
30
Content Caching with ColdFusions ltCFCACHEgt Tag
  • ltCFCACHEgt 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
  • Cache files have .tmp extension
  • Cache files are simply an HTML representation of
    the generated content
  • 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.
  • 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)
  • Default location is cfroot\cache
  • Automatically creates unique caches based on URL
    variables
  • Support for unique caching by session (does it
    work?)

31
Pros and Cons of Caching with ltCFCACHEgt
  • Pros
  • Its 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
  • actionflush
  • 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
  • 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

32
Alternative Methods for Caching Content
  • HTML Block caching with ltcfsavecontent
    variableyourVarNamegt
  • Captures all content generated within ltcfoutputgt
    tags and saves it to the specified variable name
  • Variable can also be placed in shared scope for
    caching purposes
  • Management options similar to those discussed
    earlier regarding shared scope caching of complex
    variables
  • Various available custom solutions
  • ltcf_superCachegt
  • http//www.cfprimer.com/download.cfm?ffFilesuperc
    ache.cfm
  • ltcf_accelerategt
  • http//www.bpurcell.org/blog/index.cfm?modeentry
    entry963
  • ltcf_cacheOmaticgt
  • http//www.devx.com/webdev/Article/27618
  • ltcf_turboCachegt
  • http//www.hotfusion.co.uk/TurboCache/index.htm
  • ltcf_hyperCachegt
  • http//www.pixl8.co.uk/index.cfm/pcms/site.product
    s.CF_Hypercache/
  • Ray Camdens ScopeCache
  • http//ray.camdenfamily.com/downloads/scopecache.z
    ip
  • Andy Powells JohnnyCache
  • http//johnnycache.riaforge.org/

33
Summary
  • Discussed basic caching concepts and
    considerations
  • Outlined methods for finding and qualifying good
    cache candidates
  • Outlined methods for identifying application
    bottlenecks
  • Reviewed ColdFusions built-in template caching
    settings and effects
  • Reviewed ColdFusions built-in query caching
    mechanisms
  • Presented alternative methods for caching queries
    and complex data types
  • Reviewed ColdFusions built-in page caching
    mechanisms
  • Presented alternative methods for caching entire
    pages or smaller sections of content

34

Questions?
Write a Comment
User Comments (0)
About PowerShow.com