Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Caching page fragments

From: ColdFusion 9 New Features

Video: Caching page fragments

One of the most annoying things about application development from an architectural perspective is just how much redundant code gets executed. We have pages full of queries and loops that get executed on every single page request, when they could just be executed one time and then that data saved out and reused. Caching is the way to get rid of this redundant and unnecessary duplication of effort by the server. Unfortunately caching is usually hard to implement and hard to manage. But in this video, I am going to show you how to add cache page fragments to memory and pull those fragments back out to be used later on using new caching features in ColdFusion 9.

Caching page fragments

One of the most annoying things about application development from an architectural perspective is just how much redundant code gets executed. We have pages full of queries and loops that get executed on every single page request, when they could just be executed one time and then that data saved out and reused. Caching is the way to get rid of this redundant and unnecessary duplication of effort by the server. Unfortunately caching is usually hard to implement and hard to manage. But in this video, I am going to show you how to add cache page fragments to memory and pull those fragments back out to be used later on using new caching features in ColdFusion 9.

So to get started in Chapter 6, we'll open pagefragments.cfm. Now in this file I have essentially some of the same content we used from Chapter 3. I have a query here the top to Select * From Artists. I have also provided a Where clause in here that we are going to use to filter these results. And then I have a stock message and I show the sales report for a particular artist. So let's just take a look at this in the browser and see exactly what we are outputting. So we can see on this page, I am outputting every single artist.

If I reload, the date and time is changing here with each page reload, and if I add a URL on the end, I can just filter it to a single artist and again on every page I reload, the date and time is going to change. But this data in our case, let's say, only refreshes once a night when we run all the orders together and get everything set up. So there's no sense in reloading this data from the database every time it gets requested. So if we go back to CF Builder, we are going to simply add a cfcache tag around all of the data here.

Now if I go back to my browser, I am going to take the query string over here, and load the page, we can see it was generated at 4:37 and 23 seconds. If I reload this page, it's pulling all this data from memory. It's not going back and hitting the database again. But I want the page to eventually reload this data. As it stands right now with just a plain cfcache tag, it's going to stay in the cache until the server actually gets restarted. So to add that, we just simply add a timespan and use the CreateTimeSpan function to tell how long you want to keep this data in the cache.

In this case we are going to keep in the cache for 10 seconds at a time. So if I go back here, I'll reload my page, you can see it's changed to 4:38 and 1 second. We wait a little bit here, and reload, and now see it's changed to 4:38 and 13 seconds. So every 10 seconds ColdFusion is going to run this again and pull that data back out of the cache. So let's go ahead and add id=1 on the end and when the page loads it shows it at 4:39 and 9 seconds and it shows that single artist.

If I take that off, it still shows 4:39 and 9 seconds and it's still showing only the one artist when I wanted to display everything. So on the cfcache tag in ColdFusion 9, there is a new attribute called dependson. So, what I can do is add a dependson attribute and say that this depends on the URL.id. We'll create a unique entry in the cache that depends on what the ID value is and to make this easier to demonstrate, I am going to increase the time out here to 30 seconds instead of 10.

And I'll go back to my browser and I am going to reload this page, and you can see it loaded all artists at 4:39 and 52 seconds. I am going to add id=1 and that's at 4:40. I'll take this off. Again all artists at 4:39 and 52 seconds. I'll add it back on and it should say 4:40. So, as you can see it's now caching unique pages based off what that ID is. Now the dependson attribute can take multiple Ids.

So I can say it dependson URL.id and session.username and then I could cache a particular page for that ID for each unique session. Now one thing about using an in-memory cache is you need to be careful about how much data you're shoving in there. So in here, we'll go ahead and view all artists again and take a look at the source code. Now we have seen the source code. It's nicely formatted. It's got all kinds of tabs in there and whatnot, but the tabs really aren't important, line breaks aren't important, none of that stuff really matters to how the HTML gets rendered.

So it's counterproductive to store that data in the cache. So I'll close my source code and go back to CF Builder and I'll add one more attribute on the end of my cfcache tag, which is stripwhitespace, and I'll set that as true, save my file, go back to the browser here, and refresh again. I notice I have got a new time there. So I know it's got the latest stuff in the cache, and if I choose View Source we can now see that it's removed all the tabs and all empty line breaks, so that we have as little data in the cache as possible.

So the ability to easily cache page fragments and set the dependson values is a huge advancement to caching in ColdFusion. Just remember that to cache your data wisely so that you don't end up having a negative impact on the server performance due to memory issues. In another video you'll learn how to manage the cache and take a peek at exactly what's inside.

Show transcript

This video is part of

Image for ColdFusion 9 New Features
ColdFusion 9 New Features

36 video lessons · 2967 viewers

Daniel Short
Author

 
Expand all | Collapse all
  1. 4m 30s
    1. Welcome
      53s
    2. Using the exercise files
      3m 37s
  2. 7m 48s
    1. Adding managing and comparing servers
      5m 26s
    2. Monitoring server health
      2m 22s
  3. 22m 10s
    1. Setting the default data source and SMTP
      5m 6s
    2. Getting programmatic debugging and exception handling
      5m 54s
    3. Reviewing the new onCFCRequest method
      5m 29s
    4. Using ApplicationStop()
      2m 24s
    5. Working with Server.cfc and onServerStart
      3m 17s
  4. 17m 45s
    1. Using ternary and chained operations
      5m 46s
    2. Using New and Import
      4m 2s
    3. Reviewing CFScript enhancements
      7m 57s
  5. 20m 23s
    1. Building with implicit getters and setters
      4m 26s
    2. Working with LOCAL scope in functions
      4m 35s
    3. Serializing objects
      6m 6s
    4. Writing components in CFScript
      5m 16s
  6. 19m 27s
    1. Getting started with ORM
      5m 51s
    2. Logging ORM activity
      2m 13s
    3. Saving objects to the database
      6m 10s
    4. Defining table relationships
      5m 13s
  7. 16m 59s
    1. Caching page fragments
      5m 29s
    2. Managing the cache
      4m 48s
    3. Working with in-memory files using VFS (RAM disks)
      6m 42s
  8. 12m 39s
    1. Working with Excel spreadsheets
      4m 56s
    2. Working with Word documents
      4m 4s
    3. Working with PowerPoint presentations
      3m 39s
  9. 28m 58s
    1. Loading Google maps with markers onto a page
      4m 53s
    2. Adding a Flash movie with the built-in FLV player
      2m 14s
    3. Using multi-file upload control
      6m 7s
    4. Building a progress indicator
      3m 37s
    5. Working with accordion layouts
      3m 18s
    6. Showing information with data grid enhancements
      5m 1s
    7. Informing users with Confirm Alert and Prompt dialog boxes
      3m 48s
  10. 9m 8s
    1. Accessing IMAP accounts
      6m 31s
    2. Working with the new array functions
      2m 37s
  11. 15s
    1. Goodbye
      15s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed ColdFusion 9 New Features.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.