addserver("localhost"); return new Persistence(new mysqli("db-primary", "root", "", "altpath"), $memcache); } const OFFER_PATH_PAGE_SQL = "SELECT * FROM offers LEFT JOIN offer_paths ON offers.offer_id = offer_paths.offer_id LEFT JOIN path_templates ON path_templates.path_id = offer_paths.path_id LEFT JOIN templates ON templates.template_id = path_templates.template_id WHERE offers.offer_id = %s AND sequence = %s"; private $mysqli; private $memcache; public function __construct(mysqli $mysqli, Memcache $memcache) { $this->mysqli = $mysqli; $this->memcache = $memcache; } public function getOfferPathPage($offerId, $page) { return $this->getFromCache($offerId, $page); } private function getFromCache($offerId, $page) { $pageData = $this->memcache->get($this->getOfferPageCacheKey($offerId, $page)); if($pageData === false) { $sql = sprintf(Persistence::OFFER_PATH_PAGE_SQL, $offerId, $page); $resultData = $this->getFromDatabase($sql); if($resultData === false) { return false; } $pageData = $resultData[0]; $this->memcache->add($this->getOfferPageCacheKey($offerId, $page), $pageData); } return $pageData; } private function getFromDatabase($sql) { $results = $this->mysqli->query($sql); if($results === false) { return false; } $resultData = array(); while($row = $results->fetch_assoc()) { $resultData[] = $row; } if(empty($resultData)) { return false; } return $resultData; } private function getOfferPageCacheKey($offerId, $page) { return "page-" . $offerId . "-" . $page; } }