class test
{
protected $_tmp = array();
public function foo()
{
$id = '123';
$tmp = $this->get_tmp($id);
echo '';
var_dump($tmp);
echo '';
}
public function bar()
{
$id = '223';
$tmp = $this->get_tmp($id);
echo '';
var_dump($tmp);
echo '';
}
public function get_tmp($id)
{
// 已經有資料時直接回傳
if (count($this->_tmp) > 0)
{
if (isset($this->_tmp[$id]))
{
return $this->_tmp[$id];
}
return FALSE;
}
// 取得資料
$tmp = array(
'123' => array('id' => '123', 'name' => 'xdy'),
'223' => array('id' => '223', 'name' => 'xdasdfsafdy'),
'323' => array('id' => '323', 'name' => 'xsdfsdfdy'),
'423' => array('id' => '423', 'name' => 'xsadasdasdffdy'),
);
// 作快取
$this->_tmp = $tmp;
// 檢查是否有需要的資料
if (isset($this->_tmp[$id]))
{
return $this->_tmp[$id];
}
return FALSE;
}
}
$test = new test();
$test->foo(); // 抓取的資料會做第一次的快取
$test->bar(); // 這次就會直接從快取抓資料
$tmp = $this->get_tmp('323'); // 這次也是直接抓快取資料
Friday, May 20, 2011
資料快取的概念
Monday, May 16, 2011
Sunday, May 15, 2011
Subscribe to:
Comments (Atom)