程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 采用PEAR來緩沖PHP程序二

采用PEAR來緩沖PHP程序二

編輯:關於PHP編程

最後,我們來定制一個應用,綜合的來解釋 PEAR 緩沖機制的整體框架。 我們定義一個叫做 MySQL_Query_Cache 的類,緩沖 SELECT 的查詢結果。 我們首先定義類的變量: ’.’, ’filename_prefix’ => ’cache_’), $expires = 3600) { $this->Cache($container, $container_options); $this->expires = $expires; } function _MySQL_Query_Cache() { if (is_resource($this->connection)) { mysql_close($this->connection); } $this->_Cache(); } } ?> 在正式開始之前,我們需要一些輔助函數。 function connect($hostname, $username, $password, $database) { $this->connection = mysql_connect($hostname, $username, $password) or trigger_error(’數據庫連接失敗!’, E_USER_ERROR); mysql_select_db($database, $this->connection) or trigger_error(’數據庫選擇失敗!’, E_USER_ERROR); } function fetch_row() { if ($this->cursor < sizeof($this->result)) { return $this->result[$this->cursor++]; } else { return false; } } function num_rows() { return sizeof($this->result); } ?> 下面我們來看怎樣緩沖: result = $this->get($cache_id, ’mysql_query_cache’); if ($this->result == NULL) { // 緩沖丟失 $this->cursor = 0; $this->result = array(); if (is_resource($this->connection)) { // 盡可能采用 mysql_unbuffered_query() if (function_exists(’mysql_unbuffered_query’)) {$result = mysql_unbuffered_query($query, $this->connection); } else {$result = mysql_query($query, $this->connection); } // 取出所有查詢結果 while ($row = mysql_fetch_assoc($result)) {$this->result[] = $row; } // 釋放 MySQL 結果資源 mysql_free_result($result); // 把結果緩沖 $this->save($cache_id, $this->result, $this->expires, ’mysql_query_cache’); } } } else { // 沒有查詢結果,不需要緩沖 return mysql_query($query, $this->connection); } } ?> 例 3: 使用 MySQL 查詢緩沖 connect(’hostname’, ’username’, ’password’, ’database’); $cache->query(’select * from table’); while ($row = $cache->fetch_row()) { echo ’
’; print_r($row); echo ’
’; } ?> <全文完>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved