Web程序员最常使用的数据库封装方式就是DAO,其实和马丁大爷在PoEAA中所说的表数据入口差不多:
01 class ArticleDAO 02 { 03 public function findById($id) 04 { 05 // SELECT * FROM article WHERE id = ... 06 } 07 08 public function findByTitle($title) 09 { 10 // SELECT * FROM article WHERE title LIKE ... 11 } 12 }
01 class ArticleCache 02 { 03 protected $cache; 04 protected $dao; 05 06 public function __construct($cache, $dao) 07 { 08 $this->cache = $cache; 09 $this->dao = $dao; 10 } 11 12 public function findById($id) 13 { 14 $key = 'id_' . $id; 15 if (!($result = $this->cache->get($key))) { 16 $result = $this->cache->findById($id); 17 $this->cache->set($key, $result); 18 } 19 return $result; 20 } 21 22 public function findByTitle($title) 23 { 24 $key = 'title_' . $title; 25 if (!($result = $this->cache->get($key))) { 26 $result = $this->cache->findByTitle($title); 27 $this->cache->set($key, $result); 28 } 29 return $result; 30 } 31 } 32 33 $memcache = new Memcache(); 34 $memcache->connect('host', 'port'); 35 36 $dao = new ArticleDAO(); 37 38 $cache = new ArticleCache($memcache, $dao); 39 40 $cache->findById('id'); 41 $cache->findByTitle('title');
01 class Cache 02 { 03 protected $cache; 04 protected $dao; 05 06 public function __construct($cache, $dao) 07 { 08 $this->cache = $cache; 09 $this->dao = $dao; 10 } 11 12 public function __call($name, $arguments) 13 { 14 if (strtolower(substr($name, 0, 4)) != 'find') { 15 return false; 16 } 17 18 $key = md5(strtolower(get_class($this->dao) . $name . serialize($arguments))); 19 if (!($result = $this->cache->get($key, $flags))) { 20 $result = call_user_func_array(array($this->dao, $name), $arguments); 21 $this->cache->set($key, $result); 22 } 23 return $result; 24 } 25 } 26 27 $memcache = new Memcache(); 28 $memcache->connect('host', 'port'); 29 30 $dao = new ArticleDAO(); 31 32 $cache = new Cache($memcache, $dao); 33 34 $cache->findById('id'); 35 $cache->findByTitle('title');