present.
*/
protected $_responseCache;
+ protected $_cacheLifetime = 300;
+
+
/**
*
* @var Horde_Log_Logger
if (!empty($config['cache'])) {
$this->_responseCache = $config['cache'];
+ if (!empty($config['cache_lifetime'])) {
+ $this->_cacheLifetime = $config['cache_lifetime'];
+ }
}
if (!empty($config['logger'])) {
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...
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())
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,
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);