Advanced PHP Programming- P7
Số trang: 50
Loại file: pdf
Dung lượng: 494.91 KB
Lượt xem: 13
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu advanced php programming- p7, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P7278 Chapter 10 Data Component Caching A generation function for a project summary looks like this: Author: Summary: Availability: ”>click here 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 Integrating Caching into Application Code 279should make is to alter the get_cachefile function in order to avoid colliding withcache files from the full page: Author: Availability:”>clickhere 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 xmeth- ods.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: The current temp in is degrees Farenheit ”; Further Reading 281RPCs of any kind tend to be slow, so you would like to cache the weather report for awhile before invoking the call again.You could simply apply the techniques used inProject and cache the output of generate_navigation_weather() in a flat file.Thatmethod 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 insertthe logic to use the Cache_DBM class that you implemented earlier in this chapterrequires 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 seewhether you have a valid cached temperature value.You initialize the wrapper with anexpiration time of 3,600 seconds (1 hour) to ensure that the temperature data does notget too old.Then you perform the standard logic “if it’s cached, return it; if not, generateit, cache it, and return it.”Further ReadingA number of relational database systems implement query caches or integrate them intoexternal appliances. As of version 4.0.1, MySQL has an integrated query cache.You canread 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 chaptercontains a long list of additional resources. 11 Computational ReuseC OMPUTATIONAL REUSE IS A TECHNIQUE BY which intermediate data (that is, data thatis not the final output of a function) is remembered and used to make other calculationsmore efficient. Computational reuse has a long history in computer science, particularlyin computer graphics and computational mathematics. Don’t let these highly technicalapplications scare you, though; reuse is really just another form of caching. In the past two chapters we investigated a multitude of caching strategies. At theircore, all involve the same premise:You take a piece of data that is expensive to computeand save its value.The next time you need to perform that calculation, you look to seewhether you have stored the result already. If so, you return that value. Computational reuse is a form of caching that focuses on very small pieces of data.Instead of caching entire components of an application, computational reuse focuses onhow to cache individual objects or data created in the course of executing a function.Often these small elements can also be reused. Every complex operation is the combinedresult of many smaller ones. If one particular small operation constitu ...
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P7278 Chapter 10 Data Component Caching A generation function for a project summary looks like this: Author: Summary: Availability: ”>click here 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 Integrating Caching into Application Code 279should make is to alter the get_cachefile function in order to avoid colliding withcache files from the full page: Author: Availability:”>clickhere 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 xmeth- ods.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: The current temp in is degrees Farenheit ”; Further Reading 281RPCs of any kind tend to be slow, so you would like to cache the weather report for awhile before invoking the call again.You could simply apply the techniques used inProject and cache the output of generate_navigation_weather() in a flat file.Thatmethod 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 insertthe logic to use the Cache_DBM class that you implemented earlier in this chapterrequires 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 seewhether you have a valid cached temperature value.You initialize the wrapper with anexpiration time of 3,600 seconds (1 hour) to ensure that the temperature data does notget too old.Then you perform the standard logic “if it’s cached, return it; if not, generateit, cache it, and return it.”Further ReadingA number of relational database systems implement query caches or integrate them intoexternal appliances. As of version 4.0.1, MySQL has an integrated query cache.You canread 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 chaptercontains a long list of additional resources. 11 Computational ReuseC OMPUTATIONAL REUSE IS A TECHNIQUE BY which intermediate data (that is, data thatis not the final output of a function) is remembered and used to make other calculationsmore efficient. Computational reuse has a long history in computer science, particularlyin computer graphics and computational mathematics. Don’t let these highly technicalapplications scare you, though; reuse is really just another form of caching. In the past two chapters we investigated a multitude of caching strategies. At theircore, all involve the same premise:You take a piece of data that is expensive to computeand save its value.The next time you need to perform that calculation, you look to seewhether you have stored the result already. If so, you return that value. Computational reuse is a form of caching that focuses on very small pieces of data.Instead of caching entire components of an application, computational reuse focuses onhow to cache individual objects or data created in the course of executing a function.Often these small elements can also be reused. Every complex operation is the combinedresult of many smaller ones. If one particular small operation constitu ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
24 trang 354 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 302 0 0 -
74 trang 296 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0