Return to Snippet

Revision: 87200
at October 7, 2021 16:12 by ramirog89


Updated URL

                                

Updated Code
<?php

class Articles
{
	
	private $_content;

	private $_defaultHost = 'local';

	private $_baseUrl = 'http://localhost/feed_endpoint/';

	private $_endpoint;

	private $_memcache;

	private $_settings = array(
		'memcache' => array(
			'local' => array(
				'host' => 'localhost',
				'port' => '11211'
			),
			'dev' => array(
				'host' => 'default-memcache-location',
				'port' => '11211'
			),
			'qa' => array(
				'host' => 'qa-memcache-location',
				'port' => '11211'
			),
			'live' => array(
				'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()
	{
		return json_decode($this->_content);
	}

	private function _doRequest()
	{
		$uri = $this->_baseUrl . $this->_endpoint;

		$this->_content = $this->_memcache->get($uri);

		if ($this->_content == false) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $uri);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
			$this->_content = curl_exec($ch);
			curl_close($ch);

			$this->_memcache->set($this->_endpoint, $this->_content);
		}

		return $this;
	}

	private function _setInstance()
	{
		$this->_host = $this->_defaultHost; // localhost

		$host = strtolower($_SERVER['HTTP_HOST']);
		if(strstr( $host, "-d.com") > -1 ) 
			$this->_host = "dev"; 
		else if(strstr( $host,"-q.com") > -1 ) 
			$this->_host = "qa"; 
		else if(strstr( $host,"prod.com") > -1) 
			$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']
		) or die ('Could not connect to memcache server');

		return $this;
	}

}

$articles = new Articles();

Revision: 69859
at September 23, 2015 05:15 by ramirog89


Initial Code
<?php

class Feed
{
	
	private $_content;

	private $_defaultHost = 'local';

	private $_baseUrl = 'http://noticias.mtvla.com/feeds/';

	private $_endpoint;

	private $_memcache;

	private $_settings = array(
		'memcache' => array(
			'local' => array(
				'host' => 'localhost',
				'port' => '11211'
			),
			'dev' => array(
				'host' => 'international-memcache-001.1515.mtvi.com',
				'port' => '11211'
			),
			'qa' => array(
				'host' => 'international-memcache-002.1515.mtvi.com',
				'port' => '11211'
			),
			'live' => array(
				'host' => 'international-memcache-005.811.mtvi.com',
				'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()
	{
		return json_decode($this->_content);
	}

	private function _doRequest()
	{
		$uri = $this->_baseUrl . $this->_endpoint;

		$this->_content = $this->_memcache->get($uri);

		if ($this->_content == false) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $uri);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
			$this->_content = curl_exec($ch);
			curl_close($ch);

			$this->_memcache->set($this->_endpoint, $this->_content);
		}

		return $this;
	}

	private function _setInstance()
	{
		$this->_host = $this->_defaultHost; // localhost

		$host = strtolower($_SERVER['HTTP_HOST']);
		if(strstr( $host, "-d.mtvi.com") > -1 ) 
			$this->_host = "dev"; 
		else if(strstr( $host,"-q.mtvi.com") > -1 ) 
			$this->_host = "qa"; 
		else if(strstr( $host,"mtv.com") > -1) 
			$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']
		) or die ('Could not connect to memcache server');

		return $this;
	}

}

$feed = new Feed();

Initial URL

                                

Initial Description
feed class with baseUrl and endpoint curling the target and caching on memcache

Initial Title
feeds with memcache

Initial Tags
php, service

Initial Language
PHP