Cache the results of all twitter GET requests if a cache object is
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 23 Jul 2009 21:08:08 +0000 (17:08 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 23 Jul 2009 21:09:41 +0000 (17:09 -0400)
present.

framework/Service_Twitter/lib/Horde/Service/Twitter.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Basic.php
framework/Service_Twitter/lib/Horde/Service/Twitter/Request/Oauth.php

index 7e11618..c4c7772 100644 (file)
@@ -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...
index 792b0d0..6b856c4 100644 (file)
@@ -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())
index fa58c71..6e19497 100644 (file)
@@ -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);