-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRedis.php
More file actions
120 lines (109 loc) · 3.43 KB
/
Redis.php
File metadata and controls
120 lines (109 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Redis store for simpleSAMLphp
*
* This store uses the Redis document store to store data from simpleSAMLphp.
* It implements the simpleSAMLphp datastore API, for easy integration with
* other parts of simpleSAMLphp.
*
* @author Jacob Christiansen jacob@colourbox.com
* @copyright 2015 Colourbox ApS
* @license http://opensource.org/licenses/MIT MIT-license
*/
class sspmod_redis_Store_Redis extends SimpleSAML_Store
{
private $redis;
private $prefix;
private $lifeTime;
public function __construct()
{
$redisConfig = SimpleSAML_Configuration::getConfig('module_redis.php');
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($redisConfig->getValue('parameters'), $redisConfig->getValue('options'))
);
} else {
$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);
}
}
/**
* Retrieve a value from Redis
*
* @param string $type The datatype
* @param string $key The key
* @return mixed|NULL The value
*/
public function get($type, $key)
{
$redisKey = "{$this->prefix}.$type.$key";
$value = $this->redis->get($redisKey);
if (is_null($value)) {
return null;
}
return unserialize($value);
}
/**
* Save a value to Redis
*
* If no expiration time is given, then the expiration time is set to the
* session duration.
*
* @param string $type The datatype
* @param string $key The key
* @param mixed $value The value
* @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires
*/
public function set($type, $key, $value, $expire = null)
{
$redisKey = "{$this->prefix}.$type.$key";
if (is_null($expire))
{
$expire = time() + $this->lifeTime;
}
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);
}
}
/**
* Delete a value from Redis
*
* @param string $type The datatype
* @param string $key The key
*/
public function delete($type, $key)
{
$redisKey = "{$this->prefix}.$type.$key";
try
{
$this->redis->del($redisKey);
}
catch(\ Exception $e)
{
//on shutdown sometime the auth is not set !
$this->auth();
$this->redis->del($redisKey);
}
}
}