Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Advanced PHP Programming- P7 doc
Nội dung xem thử
Mô tả chi tiết
278 Chapter 10 Data Component Caching
<?php
function generate_navigation($tag) {
list($topic, $subtopic) = explode(‘-’, $tag, 2);
if(function_exists(“generate_navigation_$topic”)) {
return call_user_func(“generate_navigation_$topic”, $subtopic);
}
else {
return ‘unknown’;
}
}
?>
A generation function for a project summary looks like this:
<?php
require_once ‘Project.inc’;
function generate_navigation_project($name) {
try {
if(!$name) {
throw new Exception();
}
$project = new Project($name);
}
catch (Exception $e){
return ‘unknown project’;
}
?>
<table>
<tr>
<td>Author:</td><td><?= $project->author ?>
</tr>
<tr>
<td>Summary:</td><td><?= $project->short_description ?>
</tr>
<tr>
<td>Availability:</td>
<td><a href=”<?= $project->file_url ?>”>click here</a></td>
</tr>
<tr>
<td><?= $project->long_description ?></td>
</tr>
</table>
<?php
}
?>
This looks almost exactly like your first attempt for caching the entire project page, and
in fact you can use the same caching strategy you applied there.The only change you
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Integrating Caching into Application Code 279
should make is to alter the get_cachefile function in order to avoid colliding with
cache files from the full page:
<?php
require_once ‘Project.inc’;
function generate_navigation_project($name) {
try {
if(!$name) {
throw new Exception;
}
$cache = new Cache_File(Project::get_cachefile_nav($name));
if($text = $cache->get()) {
print $text;
return;
}
$project = new Project($name);
$cache->begin();
}
catch (Exception $e){
return ‘unkonwn project’;
}
?>
<table>
<tr>
<td>Author:</td><td><?= $project->author ? >
</tr>
<tr>
<td>Summary:</td><td><?= $project->short_description ?>
</tr>
<tr>
<td>Availability:</td><td><a href=”<?= $project->file_url ?>”>click
here</a></td>
</tr>
<tr>
<td><?= $project->long_description ?></td>
</tr>
</table>
<?php
$cache->end();
}
And in Project.inc you add this:
public function get_cachefile_nav($name) {
global $CACHEBASE;
return “$CACHEBASE/projects/nav/$name.cache”;
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
280 Chapter 10 Data Component Caching
}
?>
It’s as simple as that!
Implementing a Query Cache
Now you need to tackle the weather element of the navigation bar you’ve been working
with.You can use the Simple Object Application Protocol (SOAP) interface at xmethods.net to retrieve real-time weather statistics by ZIP code. Don’t worry if you have not
seen SOAP requests in PHP before; we’ll discuss them in depth in Chapter 16,“RPC:
Interacting with Remote Services.” generate_navigation_weather() creates a Weather
object for the specified ZIP code and then invokes some SOAP magic to return the
temperature in that location:
<?php
include_once ‘SOAP/Client.php’;
class Weather {
public $temp;
public $zipcode;
private $wsdl;
private $soapclient;
public function _ _construct($zipcode) {
$this->zipcode = $zipcode;
$this->_get_temp($zipcode);
}
private function _get_temp($zipcode) {
if(!$this->soapclient) {
$query = “http://www.xmethods.net/sd/2001/TemperatureService.wsdl”;
$wsdl = new SOAP_WSDL($query);
$this->soapclient = $wsdl->getProxy();
}
$this->temp = $this->soapclient->getTemp($zipcode);
}
}
function generate_navigation_weather($zip) {
$weather = new Weather($zip);
?>
The current temp in <?= $weather->zipcode ?>
is <?= $weather->temp ?> degrees Farenheit\n”;
<?php
}
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Further Reading 281
RPCs of any kind tend to be slow, so you would like to cache the weather report for a
while before invoking the call again.You could simply apply the techniques used in
Project and cache the output of generate_navigation_weather() in a flat file.That
method would work fine, but it would allocate only one tiny file per ZIP code.
An alternative is to use a DBM cache and store a record for each ZIP code.To insert
the logic to use the Cache_DBM class that you implemented earlier in this chapter
requires only a few lines in _get_temp:
private function _get_temp($zipcode) {
$dbm = new Cache_DBM(Weather::get_cachefile(), 3600);
if($temp = $dbm->get($zipcode)) {
$this->temp = $temp;
return;
}
else {
if(!$this->soapclient) {
$url = “ http://www.xmethods.net/sd/2001/TemperatureService.wsdl”;
$wsdl = new SOAP_WSDL($url);
$this->soapclient = $wsdl->getProxy();
}
$this->temp = $this->soapclient->getTemp($zipcode);
$dbm->put($zipcode, $this->temp);
}
}
function get_cachefile() {
global $CACHEBASE;
return “$CACHEBASE/Weather.dbm”;
}
Now when you construct a Weather object, you first look in the DBM file to see
whether you have a valid cached temperature value.You initialize the wrapper with an
expiration time of 3,600 seconds (1 hour) to ensure that the temperature data does not
get too old.Then you perform the standard logic “if it’s cached, return it; if not, generate
it, cache it, and return it.”
Further Reading
A number of relational database systems implement query caches or integrate them into
external appliances.As of version 4.0.1, MySQL has an integrated query cache.You can
read more at www.mysql.com.
mod_rewrite is detailed on the Apache site, http://httpd.apache.org.
Web services, SOAP, and WSDL are covered in Chapter 16.The end of that chapter
contains a long list of additional resources.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.