From 262d9a66fa4cdec76e2f539a0456c985b656880a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 3 Mar 2016 08:32:14 -0800 Subject: [PATCH] cache the HTTP response --- controllers/Parse.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/controllers/Parse.php b/controllers/Parse.php index 7cdea93..12b236e 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -7,16 +7,20 @@ use XRay\Formats; class Parse { public $http; + public $mc; + private $_cacheTime = 300; private $_pretty = false; public function __construct() { $this->http = new p3k\HTTP(); + $this->mc = new Memcache(); + $this->mc->addServer('127.0.0.1'); } - public static function debug($msg) { + public static function debug($msg, $header='X-Parse-Debug') { syslog(LOG_INFO, $msg); if(array_key_exists('REMOTE_ADDR', $_SERVER)) - header("X-Parse-Debug: " . $msg); + header($header . ": " . $msg); } private function respond(Response $response, $code, $params, $headers=[]) { @@ -84,7 +88,15 @@ class Parse { $url = \normalize_url($url); // Now fetch the URL and check for any curl errors - $result = $this->http->get($url); + if($cached=$this->mc->get('xray-'.md5($url))) { + $result = json_decode($cached, true); + self::debug('using HTML from cache', 'X-Cache-Debug'); + } else { + $result = $this->http->get($url); + $cacheData = json_encode($result); + if(strlen($cacheData) < 1000000) // App Engine limits the size of cached items, so don't cache ones larger than that + $this->mc->set('xray-'.md5($url), $cacheData, MEMCACHE_COMPRESSED, $this->_cacheTime); + } if($result['error']) { return $this->respond($response, 400, [