Skip to content

Commit

Permalink
Merge pull request #1 from delboy1978uk/master
Browse files Browse the repository at this point in the history
Compatibility for all versions above PHP 5.3.3
  • Loading branch information
naroga committed Mar 31, 2017
2 parents ddc77b2 + ee8a821 commit dd80ce1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 66 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,9 +1,14 @@
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
install:
- composer install
script:
- php vendor/bin/phpunit --bootstrap vendor/autoload.php -c ./phpunit.xml ./Tests
after_script:
- php vendor/bin/coveralls -v
- php vendor/bin/coveralls -v
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -2,7 +2,7 @@

[![Build status](https://travis-ci.org/naroga/redis-cache.svg?branch=master)](https://travis-ci.org/naroga/redis-cache) [![Coverage Status](https://coveralls.io/repos/github/naroga/redis-cache/badge.svg?branch=master)](https://coveralls.io/github/naroga/redis-cache?branch=master)

This is a simple Redis driver that implements PSR-16.
This is a simple Redis driver that implements PSR-16 compatible with PHP 5.3.3+.

## Installation

Expand All @@ -26,14 +26,15 @@ You can check [Predis' documentation here](https://github.com/nrk/predis#connect
require_once "vendor/autoload.php";

use Naroga\RedisCache\Redis;
use Predis\Client;

$config = [
$config = array(
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379
]
);

$redis = new Redis(new Predis\Client($config));
$redis = new Redis(new Client($config));

if (!$redis->has('myKey')) {
$redis->set('myKey', 'myValue', 1800); //Just call any PSR-16 methods here.
Expand All @@ -42,4 +43,4 @@ if (!$redis->has('myKey')) {

## License

This library is released under the MIT license. Check [LICENSE.md](LICENSE.md) for more information .
This library is released under the MIT license. Check [LICENSE.md](LICENSE.md) for more information .
25 changes: 13 additions & 12 deletions Redis.php
Expand Up @@ -8,6 +8,7 @@
use Predis\ClientInterface;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\DateInterval;
use Traversable;

/**
* Class Redis
Expand Down Expand Up @@ -101,11 +102,11 @@ public function clear()
/** @inheritDoc */
public function getMultiple($keys, $default = null)
{
if (!is_array($keys) && !$keys instanceof \Traversable) {
if (!is_array($keys) && !$keys instanceof Traversable) {
throw new InvalidArgumentException("Keys must be an array or a \\Traversable instance.");
}

$result = [];
$result = array();
foreach ($keys as $key) {
$result[$key] = $this->get($key, $default);
}
Expand All @@ -116,18 +117,18 @@ public function getMultiple($keys, $default = null)
/** @inheritDoc */
public function setMultiple($values, $ttl = null)
{
if (!is_array($values) && !$values instanceof \Traversable) {
if (!is_array($values) && !$values instanceof Traversable) {
throw new InvalidArgumentException("Values must be an array or a \\Traversable instance.");
}

try {
$responses = $this->client->transaction(function ($tx) use ($values, $ttl) {
$redis = $this;
$responses = $this->client->transaction(function ($tx) use ($values, $ttl, $redis) {
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {
if (!$redis->set($key, $value, $ttl)) {
throw new TransactionFailedException();
}
}
});
}});
} catch (TransactionFailedException $e) {
return false;
}
Expand All @@ -138,18 +139,18 @@ public function setMultiple($values, $ttl = null)
/** @inheritDoc */
public function deleteMultiple($keys)
{
if (!is_array($keys) && !$keys instanceof \Traversable) {
if (!is_array($keys) && !$keys instanceof Traversable) {
throw new InvalidArgumentException("Keys must be an array or a \\Traversable instance.");
}

try {
$transaction = $this->client->transaction(function ($tx) use ($keys) {
$redis = $this;
$transaction = $this->client->transaction(function ($tx) use ($keys, $redis) {
foreach ($keys as $key) {
if (!$this->delete($key)) {
if (!$redis->delete($key)) {
throw new TransactionFailedException();
}
}
});
}});
} catch (TransactionFailedException $e) {
return false;
}
Expand Down

0 comments on commit dd80ce1

Please sign in to comment.