|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Ion |
|
4
|
|
|
* |
|
5
|
|
|
* Building blocks for web development |
|
6
|
|
|
* |
|
7
|
|
|
* @package Ion |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Aviat\Ion\Cache\Driver; |
|
14
|
|
|
|
|
15
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class RedisDriver implements \Aviat\Ion\Cache\CacheDriverInterface { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The redis extension class instance |
|
21
|
|
|
* #var Redis |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $redis; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create the Redis cache driver |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(ContainerInterface $container) |
|
29
|
|
|
{ |
|
30
|
|
|
$config = $container->get('config'); |
|
31
|
|
|
$redisConfig = $config->get('redis'); |
|
32
|
|
|
|
|
33
|
|
|
$this->redis = new \Redis(); |
|
34
|
|
|
|
|
35
|
|
|
(array_key_exists('port', $redisConfig)) |
|
36
|
|
|
? $this->redis->pconnect($redisConfig['host'], $redisConfig['port']) |
|
37
|
|
|
: $this->redis->pconnect($redisConfig['host']); |
|
38
|
|
|
|
|
39
|
|
|
// If there is a password, authorize |
|
40
|
|
|
if (array_key_exists('password', $redisConfig)) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->redis->auth($redisConfig['password']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// If there is a database selected, connect to the specified database |
|
46
|
|
|
if (array_key_exists('database', $redisConfig)) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->redis->select($redisConfig['database']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Destructor to disconnect from redis |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __destruct() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->redis->close(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Retreive a value from the cache backend |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function get($key) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->redis->get($key); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set a cached value |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $key |
|
77
|
|
|
* @param mixed $value |
|
78
|
|
|
* @return CacheDriverInterface |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
public function set($key, $value) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->redis->set($key, $value); |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Invalidate a cached value |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* @return CacheDriverInterface |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function invalidate($key) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->redis->del($key); |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Clear the contents of the cache |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function invalidateAll() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->redis->flushDB(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
// End of RedisDriver.php |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.