- Introduction
- Connecting the Client to the Server
- Caching Responses
- Conclusion
Caching Responses
The Post object has three methods that control the functionality of the entire application: get, save, and delete (see Listing 3). Each method does exactly what you’d expect, based on its name, and returns either an XML response constructed from database values or a cached response from the server. New responses are constructed from the database and cached when a user makes a request and the previously cached file time is greater than five minutes. This time can be modified easily by changing the number, multiplied by 60.
The cache file is saved to a folder named cache on the server. You can change the folder at will, even placing it in a hidden or protected folder for security purposes.
I’ve also added a parameter to the get method that allows other methods to override and create a new response. This strategy is particularly useful when new data has been added to or removed from the database and we want it to be reflected on the client side. If a cache file exists and the previous conditions are not matched, the get method returns the file contents of the cached response file. I’ve appended a comment to the beginning of the cache file when it’s returned to the client. This provides an easy way to confirm the performance of the get method when viewing the response.
Listing 3 Caching responses (Post.class.php).
function get($override) { $cfile = "cache/posts.xml"; $ctime = (5 * 60); if($override == true || !(file_exists($cfile) && (time() - $ctime < filemtime($cfile)))) { $this->dbConnect(); $query = "SELECT * FROM $this->table ORDER BY id asc"; $result = mysql_db_query (DB_NAME, $query, LINK); $xml = ’<posts>’; while($row = mysql_fetch_array($result)) { $xml .= ’<post>’; $xml .= ’<id>’. $row[’id’] .’</id>’; $xml .= ’<date>’. $row[’date’] .’</date>’; $xml .= ’<title><![CDATA[’. $row[’title’] .’]]></title>’; $xml .= ’<description><![CDATA[’. $row[’description’] .’]]></description>’; $xml .= ’</post>’; } $xml .= ’</posts>’; mysql_close(); $fp = fopen($cfile, ’w’); fwrite($fp, $xml); fclose($fp); return $xml; } else { return "<!-- Cached ".date(’jS F Y H:i’, filemtime($cfile))." -->" . file_get_contents($cfile); } }
If you take a look at the constructor function of the Post object, you’ll notice that we require a file called mysql_connect.php, which can be found in the sample download. The database-connection information in this file must match your server. For security purposes, I also recommend putting the file in a secure directory that’s not accessible from a browser, and be sure to change the path in the Post object’s constructor function. The table for this project must also exist in your database for the sample to work. You can run the SQL script in Listing 4 to create the database on your server.
Listing 4 Creating the database table (informit_ajax_caching.sql).
CREATE TABLE ´informit_ajax_caching´ ( ´id´ int(11) NOT NULL auto_increment, ´date´ datetime NOT NULL default ’0000-00-00 00:00:00’, ´description´ longtext NOT NULL, ´title´ varchar(33) NOT NULL default ’’, PRIMARY KEY (´id´) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;