Skip to content

Commit

Permalink
Merge pull request #29 from bookboon/hotfix/memcached-bug
Browse files Browse the repository at this point in the history
Hotfix/memcached bug
  • Loading branch information
lkm committed Jan 21, 2019
2 parents 57bb845 + 3893682 commit 88900c7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.1",
"ext-curl": "*",
"ext-json" : "*",
"league/oauth2-client": "~2.3"
Expand Down
1 change: 1 addition & 0 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions src/Cache/HashTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/MemcachedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -90,6 +90,6 @@ public function delete($key)
*/
public function isInitialized()
{
return $this->cache == null;
return $this->cache !== null;
}
}
40 changes: 31 additions & 9 deletions src/Cache/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,58 @@ 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;

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')) {
return \Redis::SERIALIZER_PHP;
}
return defined('Redis::SERIALIZER_IGBINARY') ? \Redis::SERIALIZER_IGBINARY : \Redis::SERIALIZER_PHP;
}

/**
* @return bool
*/
public function isInitialized()
{
return $this->redis != null;
}
}
}
2 changes: 1 addition & 1 deletion src/Client/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 88900c7

Please sign in to comment.