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