From 1a24474440007c37bf03d9e612b0c1cabfd4c13a Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Mon, 21 Jan 2019 17:37:51 +0000 Subject: [PATCH 1/2] Bug preventing memcached from ever working Signed-off-by: Lasse Mammen --- src/Cache/MemcachedCache.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cache/MemcachedCache.php b/src/Cache/MemcachedCache.php index 622a1f3..5e2ceb3 100644 --- a/src/Cache/MemcachedCache.php +++ b/src/Cache/MemcachedCache.php @@ -59,12 +59,12 @@ public function get($key) } /** - * Save an object. + * Save in cache * - * @param $key + * @param string $key * @param $data - * - * @return bool + * @param int|null $ttl + * @return bool if successful */ public function save($key, $data, ?int $ttl = null) { @@ -90,6 +90,6 @@ public function delete($key) */ public function isInitialized() { - return $this->cache == null; + return $this->cache !== null; } } From 3893682d2468bcc8c882793bb574dc7bf3dfc85d Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Mon, 21 Jan 2019 17:38:10 +0000 Subject: [PATCH 2/2] Minor code quality changes and upgrade composer to php 7.1 minimum Signed-off-by: Lasse Mammen --- composer.json | 2 +- src/Cache/Cache.php | 1 + src/Cache/HashTrait.php | 6 +----- src/Cache/RedisCache.php | 40 ++++++++++++++++++++++++++++--------- src/Client/RequestTrait.php | 2 +- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 218a9c4..be639d8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.6", + "php": ">=7.1", "ext-curl": "*", "ext-json" : "*", "league/oauth2-client": "~2.3" diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index bffd777..f6d5477 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -33,6 +33,7 @@ public function get($key); * * @param string $key * @param $data + * @param int|null $ttl * @return bool if successful */ public function save($key, $data, ?int $ttl = null); diff --git a/src/Cache/HashTrait.php b/src/Cache/HashTrait.php index 2ea7b6b..79a8d05 100644 --- a/src/Cache/HashTrait.php +++ b/src/Cache/HashTrait.php @@ -39,11 +39,7 @@ public function hash($url, $id, array $headers) */ public function isCachable($url, $httpMethod) { - if ($httpMethod === Client::HTTP_GET && $this->isInitialized()) { - return true; - } - - return false; + return $httpMethod === Client::HTTP_GET && $this->isInitialized(); } /** diff --git a/src/Cache/RedisCache.php b/src/Cache/RedisCache.php index e73f617..cf2bb0d 100644 --- a/src/Cache/RedisCache.php +++ b/src/Cache/RedisCache.php @@ -43,11 +43,26 @@ public function __construct($server = 'localhost', $port = 6379, $ttl = 600, $ti } } + /** + * Get a cached object. + * + * @param $key + * + * @return mixed False is not found + */ public function get($key) { return $this->redis !== null ? $this->redis->get($key) : false; } + /** + * Save in cache + * + * @param string $key + * @param $data + * @param int|null $ttl + * @return bool if successful + */ public function save($key, $data, ?int $ttl = null) { $ttl = $ttl ?? $this->ttl; @@ -55,11 +70,26 @@ public function save($key, $data, ?int $ttl = null) return $this->redis !== null ? $this->redis->setex($key, $ttl, $data) : false; } + /** + * Delete a cached object. + * + * @param $key + * + * @return bool if successful true + */ public function delete($key) { return $this->redis->delete($key) === 1; } + /** + * @return bool + */ + public function isInitialized() + { + return $this->redis !== null; + } + protected function getSerializerValue() { if (defined('HHVM_VERSION')) { @@ -67,12 +97,4 @@ protected function getSerializerValue() } return defined('Redis::SERIALIZER_IGBINARY') ? \Redis::SERIALIZER_IGBINARY : \Redis::SERIALIZER_PHP; } - - /** - * @return bool - */ - public function isInitialized() - { - return $this->redis != null; - } -} \ No newline at end of file +} diff --git a/src/Client/RequestTrait.php b/src/Client/RequestTrait.php index 9c472a6..3f95713 100644 --- a/src/Client/RequestTrait.php +++ b/src/Client/RequestTrait.php @@ -68,8 +68,8 @@ public function makeRequest($relativeUrl, array $variables = array(), $httpMetho $postVariables = $variables; } + if ($this->getCache() !== null && $this->getCache()->isCachable($queryUrl, $httpMethod) && $shouldCache) { - if ($this->getCache() != null && $this->getCache()->isCachable($queryUrl, $httpMethod) && $shouldCache) { $result = $this->getFromCache($queryUrl); if ($result === false) {