diff --git a/composer.json b/composer.json index 16f5176..83c2086 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "wiki": "https://github.com/ColourboxDevelopment/simplesamlphp-module-redis/wiki" }, "require": { - "php": "^5.4", + "php": ">=5.4", "predis/predis": "~1.0", "simplesamlphp/composer-module-installer": "^1.1" }, diff --git a/lib/Redis/DualRedis.php b/lib/Redis/DualRedis.php index af88a79..7639704 100644 --- a/lib/Redis/DualRedis.php +++ b/lib/Redis/DualRedis.php @@ -23,6 +23,20 @@ public function set($key, $value) $this->newHost->set($key, $value); } + public function auth($key) + { + $this->newHost->auth($key); + $this->oldHost->auth($key); + } + + public function keys($pattern) + { + return array_unique(array_merge( + $this->newHost->keys($pattern), + $this->oldHost->keys($pattern) + )); + } + public function del($key) { $this->newHost->del($key); diff --git a/lib/Store/Redis.php b/lib/Store/Redis.php index 18d2f52..5032bc1 100644 --- a/lib/Store/Redis.php +++ b/lib/Store/Redis.php @@ -18,19 +18,31 @@ class sspmod_redis_Store_Redis extends SimpleSAML_Store public function __construct() { - $config = SimpleSAML_Configuration::getConfig('module_redis.php'); + $redisConfig = SimpleSAML_Configuration::getConfig('module_redis.php'); - if ($config->hasValue('oldHost')) { - $oldHost = $config->getValue('oldHost'); + if ($redisConfig->hasValue('oldHost')) { + $oldHost = $redisConfig->getValue('oldHost'); $this->redis = new sspmod_redis_Redis_DualRedis( new Predis\Client($oldHost['parameters'], $oldHost['options']), - new Predis\Client($config->getValue('parameters'), $config->getValue('options')) + new Predis\Client($redisConfig->getValue('parameters'), $redisConfig->getValue('options')) ); } else { - $this->redis = new Predis\Client($config->getValue('parameters'), $config->getValue('options')); + $this->redis = new Predis\Client($redisConfig->getValue('parameters'), $redisConfig->getValue('options')); + } + + $this->auth(); + + $this->prefix = $redisConfig->getString('prefix', 'simpleSAMLphp'); + $this->lifeTime = $redisConfig->getInteger('lifetime', 28800); // 8 hours + } + + protected function auth() + { + $redisConfig = SimpleSAML_Configuration::getConfig('module_redis.php'); + if($auth = $redisConfig->getString('auth', '')) + { + $this->redis->auth($auth); } - $this->prefix = $config->getString('prefix', 'simpleSAMLphp'); - $this->lifeTime = $config->getInteger('lifetime', 28800); // 8 hours } /** @@ -66,12 +78,23 @@ public function get($type, $key) public function set($type, $key, $value, $expire = null) { $redisKey = "{$this->prefix}.$type.$key"; - $this->redis->set($redisKey, serialize($value)); - - if (is_null($expire)) { + if (is_null($expire)) + { $expire = time() + $this->lifeTime; } - $this->redis->expireat($redisKey, $expire); + + try + { + $this->redis->set($redisKey, serialize($value)); + $this->redis->expireat($redisKey, $expire); + } + catch(\ Exception $e) + { + //on shutdown sometime the auth is not set ! + $this->auth(); + $this->redis->set($redisKey, serialize($value)); + $this->redis->expireat($redisKey, $expire); + } } /** @@ -83,6 +106,15 @@ public function set($type, $key, $value, $expire = null) public function delete($type, $key) { $redisKey = "{$this->prefix}.$type.$key"; - $this->redis->del($redisKey); + try + { + $this->redis->del($redisKey); + } + catch(\ Exception $e) + { + //on shutdown sometime the auth is not set ! + $this->auth(); + $this->redis->del($redisKey); + } } }