From: Michael J. Rubinsky Date: Thu, 23 Jul 2009 21:08:08 +0000 (-0400) Subject: Cache the results of all twitter GET requests if a cache object is X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=c50d0842a9bc862aec64b0b3f89e5fe8157984a1;p=horde.git Cache the results of all twitter GET requests if a cache object is present. --- diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter.php b/framework/Service_Twitter/lib/Horde/Service/Twitter.php index 7e116186d..c4c7772f1 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter.php @@ -32,6 +32,9 @@ class Horde_Service_Twitter */ protected $_responseCache; + protected $_cacheLifetime = 300; + + /** * * @var Horde_Log_Logger @@ -83,6 +86,9 @@ class Horde_Service_Twitter if (!empty($config['cache'])) { $this->_responseCache = $config['cache']; + if (!empty($config['cache_lifetime'])) { + $this->_cacheLifetime = $config['cache_lifetime']; + } } if (!empty($config['logger'])) { @@ -124,6 +130,10 @@ class Horde_Service_Twitter return $this->_auth; case 'request': return $this->_request; + case 'responseCache': + return $this->_responseCache; + case 'cacheLifetime': + return $this->_cacheLifetime; } // If not, assume it's a method/action class... diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php index 792b0d069..6b856c4e2 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php @@ -22,10 +22,20 @@ class Horde_Service_Twitter_Request_Basic extends Horde_Service_Twitter_Request public function get($url, $params = array()) { + $key = md5($url . 'get' . serialize($params) . $this->_twitter->auth->username); + $cache = $this->_twitter->responseCache; + if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) { + return $results; + } $client = new Horde_Http_Client(); $response = $client->get($url, array('Authorization' => $this->_twitter->auth->buildAuthorizationHeader())); - return $response->getBody(); + $body = $response->getBody(); + if (!empty($cache)) { + $cache->set($key, $body); + } + + return $body; } public function post($url, $params = array()) diff --git a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php index fa58c7148..6e19497d5 100644 --- a/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php +++ b/framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php @@ -22,6 +22,11 @@ class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request public function get($url, $params = array()) { + $key = md5($url . 'get' . serialize($params) . serialize($this->_twitter->auth->getAccessToken())); + $cache = $this->_twitter->responseCache; + if (!empty($cache) && $results = $cache->get($key, $this->_twitter->cacheLifetime)) { + return $results; + } $request = new Horde_Oauth_Request($url, $params); $request->sign($this->_twitter->auth->oauth->signatureMethod, $this->_twitter->auth->oauth, @@ -36,9 +41,19 @@ class Horde_Service_Twitter_Request_Oauth extends Horde_Service_Twitter_Request return '{"request":"' . $url . '", "error:", "' . $e->getMessage() . '"}'; } - return $response->getBody(); + $body = $response->getBody(); + if (!empty($cache)) { + $cache->set($key, $body); + } + + return $body; } + /** + * Send a POST request to the twitter API. Purposely do not cache results + * from these since POST requests alter data on the server. + * + */ public function post($url, $params = array()) { $request = new Horde_Oauth_Request($url, $params);