Tim Dietrich

Custom Software Developer

Home Services Portfolio Blog About Contact Newsletter

The Power of FM WebFrame's Caching

This morning I launched a new Web site called FileMaker Digest. My goal with FileMaker Digest is to provide a reliable, comprehensive, no-nonsense source for all of the things that matter to those of us in the FileMaker community. If you get a moment, please check it out and let me know what you think. http://filemakerdigest.com

I'll write more about what went into developing the site later. But I wanted to share something that shows how FM WebFrame (which the site is running on) comes into play.

Specifically, I want to show you how FM WebFrame's caching function can be used to dramatically improve performance of data-driven sites, and how easy it is to implement.

Let's start by looking at this code, which I had been using to pull the data from FileMaker.

// Initialize the request.
@require_once ( dirname(__FILE__) . '/../FMWebFrame/FMWebFrame.php' );

// Get recent posts.
$sql = "SELECT Title, Publication_Date, URL, Summary, Post_Type FROM Posts WHERE Is_Online = 'Yes' ORDER BY Publication_Date DESC, Post_ID DESC FETCH FIRST 25 ROWS ONLY";	
		
$posts = fmExecuteSQL ( $fmDigest, $sql );	

It's pretty straightforward. We're using a simple SQL SELECT statement to grab the 25 most recently approved records (Is_Online = 'Yes') and display them in reverse chronoloical order by date (ORDER BY Publication_Date DESC, Post_ID).

There's nothing wrong with this code. In fact, it works like a charm. However, imagine what would happen if a large number of visitors came to the site at the same time. The PHP would send that SELECT statement to FileMaker Server every time that the home page was requested, and potentially causing heavy load on the database server as a result. This would negatively impact the visitors to the site, as well as the users of the databases on the database server.

A solution to this problem is to grab the data as few times as possible, store it on the Web server, and only update it periodically. This is a technique known as caching, and there's support for caching built into FM WebFrame.

Here's an example.

// Initialize the request.
@require_once ( dirname(__FILE__) . '/../FMWebFrame/FMWebFrame.php' );

// Try to get the data from the cache.
$home_news_cache = fmCacheGet ( 'home_news' );	

// If there is no cache available, or the cache is more than 1 hour old...
if ( ( $home_news_cache === null ) or ( $home_news_cached['Cache_Age'] > ( 360 ) ) ) {

	// Get recent posts.
	$sql = "SELECT Title, Publication_Date, URL, Summary, Post_Type FROM Posts WHERE Is_Online = 'Yes' ORDER BY Publication_Date DESC, Post_ID DESC FETCH FIRST 25 ROWS ONLY";	
		
	$posts = fmExecuteSQL ( $fmDigest, $sql );	
	
	// Cache the results.
	fmCachePut ( $posts, 'home_news' );			
				
} else {

	// Get the contents of the cache.
	$posts = $home_news_cache['Cache_Contents'];

}	

First, we use FM WebFrame's "fmCacheGet" function to see if there is a cache file named "home_news" available. If so, and if the file isn't terribly old (in this case, no more than an hour old), then we use the data in the cache. However, if the cache isn't available, or if it is "stale," then we send the SQL SELECT statement to the server, and then use FM WebFrame's "fmCachePut" function to store the retrieved data in the cache.

As you can see, implementing caching with FM WebFrame is really easy to do. It took less than five minutes to wire this up. And the improvement in performance is amazing to see.

For more information on FM WebFrame, visit: http://fmwebframe.com