ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Redis.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleSAML\Store;
4 
5 use \SimpleSAML_Configuration as Configuration;
6 use \SimpleSAML\Store;
7 
13 class Redis extends Store
14 {
15  public $redis;
16 
20  public function __construct($redis = null)
21  {
22  assert($redis === null || is_subclass_of($redis, 'Predis\\Client'));
23 
24  if (!class_exists('\Predis\Client')) {
25  throw new \SimpleSAML\Error\CriticalConfigurationError('predis/predis is not available.');
26  }
27 
28  if ($redis === null) {
29  $config = Configuration::getInstance();
30 
31  $host = $config->getString('store.redis.host', 'localhost');
32  $port = $config->getInteger('store.redis.port', 6379);
33  $prefix = $config->getString('store.redis.prefix', 'SimpleSAMLphp');
34 
35  $redis = new \Predis\Client(
36  array(
37  'scheme' => 'tcp',
38  'host' => $host,
39  'port' => $port,
40  ),
41  array(
42  'prefix' => $prefix,
43  )
44  );
45  }
46 
47  $this->redis = $redis;
48  }
49 
53  public function __destruct()
54  {
55  if (method_exists($this->redis, 'disconnect')) {
56  $this->redis->disconnect();
57  }
58  }
59 
68  public function get($type, $key)
69  {
70  assert(is_string($type));
71  assert(is_string($key));
72 
73  $result = $this->redis->get("{$type}.{$key}");
74 
75  if ($result === false || $result === null) {
76  return null;
77  }
78 
79  return unserialize($result);
80  }
81 
90  public function set($type, $key, $value, $expire = null)
91  {
92  assert(is_string($type));
93  assert(is_string($key));
94  assert($expire === null || (is_int($expire) && $expire > 2592000));
95 
96  $serialized = serialize($value);
97 
98  if ($expire === null) {
99  $this->redis->set("{$type}.{$key}", $serialized);
100  } else {
101  // setex expire time is in seconds (not unix timestamp)
102  $this->redis->setex("{$type}.{$key}", $expire - time(), $serialized);
103  }
104  }
105 
112  public function delete($type, $key)
113  {
114  assert(is_string($type));
115  assert(is_string($key));
116 
117  $this->redis->del("{$type}.{$key}");
118  }
119 }
$expire
Definition: saml2-acs.php:140
$config
Definition: bootstrap.php:15
$result
$type
__destruct()
Deconstruct the Redis data store.
Definition: Redis.php:53
__construct($redis=null)
Initialize the Redis data store.
Definition: Redis.php:20
$key
Definition: croninfo.php:18