/ Published in: PHP
feed class with baseUrl and endpoint curling the target and caching on memcache
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class Articles { private $_content; private $_defaultHost = 'local'; private $_baseUrl = 'http://localhost/feed_endpoint/'; private $_endpoint; private $_memcache; 'host' => 'localhost', 'port' => '11211' ), 'host' => 'default-memcache-location', 'port' => '11211' ), 'host' => 'qa-memcache-location', 'port' => '11211' ), 'host' => 'live-memcache-location', 'port' => '11211' ) ) ); public function __construct() { $this->_setInstance() ->_memcacheConnect(); } public function getLastArticles() { return $this->setEndpoint('latest-articles-paginated?page=1') ->_doRequest() ->_sendResponse(); } public function setEndpoint($endpoint) { $this->_endpoint = $endpoint; return $this; } public function getEndpoint() { return $this->_endpoint; } private function _sendResponse() { } private function _doRequest() { $uri = $this->_baseUrl . $this->_endpoint; $this->_content = $this->_memcache->get($uri); if ($this->_content == false) { $this->_memcache->set($this->_endpoint, $this->_content); } return $this; } private function _setInstance() { $this->_host = $this->_defaultHost; // localhost $this->_host = "dev"; $this->_host = "qa"; $this->_host = "live"; return $this; } private function _memcacheConnect() { $this->_memcache = new Memcache; $this->_memcache->connect( $this->_settings['memcache'][$this->_host]['host'], $this->_settings['memcache'][$this->_host]['port'] return $this; } } $articles = new Articles();