Friday, May 20, 2011

資料快取的概念


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'); // 這次也是直接抓快取資料

Sunday, May 15, 2011

Saturday, April 16, 2011

滾石三十【快樂天堂】


Tuesday, March 29, 2011

TIMESTAMP 的使用 memo

查詢某個 TIMESTAMP 欄位在一天內的資料

SELECT * FROM table WHERE DATE_SUB(NOW(), INTERVAL 1 DAY) > time
DATE_SUB 的參考文獻
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

TIMESTAMP 的幾種 Default 值:
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP

Monday, March 28, 2011

MVC Frameworks Document (1)

./controllers/api_welcomes_controller.php
/**
 * Example Controller
 * 網址範例 http://localhost/api.php?c=api_welcomes&a=index&p[]=p1&p[]=p2
 * 參數 p 是一個矩陣, 可以帶到 action 使用
 * 目前 index 這個 actin 有兩個引數 ($p1 和 $p2)
 * 所以要帶兩個參數 p (p[]=p1&p[]=p2) 來滿足該 action 的引數需求
 */
class STOCK_ApiWelcomesController extends STOCK_AppController
{
  public function index($p1, $p2)
  {
    // using argument example
    d($p1);
    d($p2);
    
    // load model example
    $this->load->model('example');
    $this->m_example->info();
    $this->load->model('example2');
    $this->m_example2->info();
    
    // load config example
    $config = $this->load->config('auth_exception');
    
    // set view data
    $this->view->set('abc.def', 123456);
    $this->view->set('abc.f', 456);
    $this->view->set('abc.def.xyz', 6);
    // render a element
    $this->view->render_element(
      'example',    // element name
      array('test' => 'this is a element test (1).')
    );
    // set veiw layout name
    $this->view->set_layout('');
    // render a layout
    $this->view->render();
  }
}

./models/example.php
class STOCK_Example extends STOCK_AppModel
{
  public function __construct()
  {
    parent::__construct();
  }
  
  public function info()
  {
    $this->load_database();
    d($this->db);
    $this->load_memcache();
    d($this->mc);
    // load model is also avaliable!
    $this->load_model('user');
    d($this->m_user);
  }
}

./views/api_welcomes/index.php
<p>
---------- This is work! ---------- line 2 ----------
</p>
<?php echo d($abc); ?>
<?php
$this->render_element(
  'example',
  array('test' => 'this is a element test (2).')
);
?>

./views/elements/example.php
<div><?php echo $test; ?></div>

在任何地方插入下面的程式, 可以完成事件的處理 (Event Handle), event 的程式放在 ./events/ 下, 檔名即為 event name
include(event('example'));

Wednesday, March 16, 2011

Race Condition of Memcached

最近在思考使用 memcached 時產生的 race condition 的問題, 因此開始 Google 了一些資料, 目前找到最有用的資料莫過於官網上的文件, 原理還算簡單易懂, 但因為沒有使用過這種方案的經驗, 還不知道實際執行的效果為何, 下個月應該就能作實驗了吧, 希望能有不錯的收獲!