Use cache for tweeter request to avoid hitting rate limits V06.008
authorymh <ymh.work@gmail.com>
Sun, 23 Sep 2018 17:13:34 +0200
changeset 1478 adb28b75f2c7
parent 1477 bdf75c8cff6b
child 1479 2b510a3a0f7d
Use cache for tweeter request to avoid hitting rate limits
web/composer.json
web/composer.lock
web/search_tweets.php
--- a/web/composer.json	Sun Sep 23 15:20:40 2018 +0200
+++ b/web/composer.json	Sun Sep 23 17:13:34 2018 +0200
@@ -3,7 +3,8 @@
     "type": "project",
     "require": {
         "abraham/twitteroauth": "^0.9",
-        "zendframework/zend-i18n": "^2.9"
+        "zendframework/zend-i18n": "^2.9",
+        "tedivm/stash": "^0.15.1"
     },
     "authors": [
         {
--- a/web/composer.lock	Sun Sep 23 15:20:40 2018 +0200
+++ b/web/composer.lock	Sun Sep 23 17:13:34 2018 +0200
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "3d5f2c50faad9a67bd8606d1c8cda8f4",
+    "content-hash": "ade900337552d77090ae25724b6767a9",
     "packages": [
         {
             "name": "abraham/twitteroauth",
@@ -61,6 +61,112 @@
             "time": "2018-07-04T01:28:41+00:00"
         },
         {
+            "name": "psr/cache",
+            "version": "1.0.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/cache.git",
+                "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8",
+                "reference": "d11b50ad223250cf17b86e38383413f5a6764bf8",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Cache\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common interface for caching libraries",
+            "keywords": [
+                "cache",
+                "psr",
+                "psr-6"
+            ],
+            "time": "2016-08-06T20:24:11+00:00"
+        },
+        {
+            "name": "tedivm/stash",
+            "version": "v0.15.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/tedious/Stash.git",
+                "reference": "a6f14f7787e765efbc8f1a871519f74caa0fd6db"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/tedious/Stash/zipball/a6f14f7787e765efbc8f1a871519f74caa0fd6db",
+                "reference": "a6f14f7787e765efbc8f1a871519f74caa0fd6db",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.0",
+                "psr/cache": "~1.0"
+            },
+            "provide": {
+                "psr/cache-implementation": "1.0.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^2.8",
+                "phpunit/phpunit": "^6",
+                "satooshi/php-coveralls": "1.0.*"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Stash\\": "src/Stash/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause"
+            ],
+            "authors": [
+                {
+                    "name": "Robert Hafner",
+                    "email": "tedivm@tedivm.com"
+                },
+                {
+                    "name": "Josh Hall-Bachner",
+                    "email": "charlequin@gmail.com"
+                }
+            ],
+            "description": "The place to keep your cache.",
+            "homepage": "http://github.com/tedious/Stash",
+            "keywords": [
+                "apc",
+                "cache",
+                "caching",
+                "memcached",
+                "psr-6",
+                "psr6",
+                "redis",
+                "sessions"
+            ],
+            "time": "2018-02-19T21:12:55+00:00"
+        },
+        {
             "name": "zendframework/zend-i18n",
             "version": "2.9.0",
             "source": {
--- a/web/search_tweets.php	Sun Sep 23 15:20:40 2018 +0200
+++ b/web/search_tweets.php	Sun Sep 23 17:13:34 2018 +0200
@@ -9,39 +9,39 @@
 
 include_once 'common.php';
 use Abraham\TwitterOAuth\TwitterOAuth;
+use Stash\Pool;
+use Stash\Driver\Sqlite;
+
+$cacheDriver = new Stash\Driver\Sqlite(array('path' => sys_get_temp_dir()."/polemictweet_cache.db"));
+$cachePool = new Stash\Pool($cacheDriver);
 
 /**
  * Check for a POSTed status message to send to Twitter
  */
 if (!empty($_GET)
 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {
-    /**
-     * Easiest way to use OAuth now that we have an Access Token is to use
-     * a preconfigured instance of Zend_Http_Client which automatically
-     * signs and encodes all our requests without additional work
-     */
-     
-    // if (isset($_GET['endpoint'])) {
-    // 	$endpoint = $_GET['endpoint'];
-	// 	unset($_GET['endpoint']);
-    // } else {
-    // 	$endpoint = "search/tweets";
-    // }
+
+    $itemCachePath="search/tweets?" . http_build_query($_GET);
+
+    $cachedStatusesResp = $cachePool->getItem($itemCachePath);
+
+    $statusesStr = $cachedStatusesResp->get();
 
-    $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
-    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']);
-
+    if ($cachedStatusesResp->isMiss()) {
+        $cachedStatusesResp->lock();
+        
+        $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
+        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']);
+    
+    
+        $statuses = $connection->get("search/tweets", $_GET);
+        $statusesStr = json_encode($statuses);
 
-    // $statuses = $connection->get("search/tweets", $parameters);
-    $statuses = $connection->get("search/tweets", $_GET);
-    // $client = $token->getHttpClient($configuration);
-    // $client->setUri("https://api.twitter.com/1.1/$endpoint.json");
-    // $client->setMethod(Zend_Http_Client::GET);
-    // $client->setParameterGet($_GET);
-    // $response = $client->request();
+        $cachedStatusesResp->set($statusesStr)->expiresAfter(3);
+        $cachePool->save($cachedStatusesResp);
+    }
 
-    // echo $response->getBody();
-    echo json_encode($statuses);
+    echo($statusesStr);
 
 } else {
     /**