1
|
|
|
<?php |
2
|
|
|
namespace FMUP; |
3
|
|
|
|
4
|
|
|
use FMUP\Cache\Factory; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Get the Cache instance |
8
|
|
|
* @package FMUP |
9
|
|
|
* @example |
10
|
|
|
*/ |
11
|
|
|
class Cache |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Cache instance |
15
|
|
|
* @var Cache |
16
|
|
|
*/ |
17
|
|
|
private static $instance = array(); |
18
|
|
|
|
19
|
|
|
private $driver = Factory::DRIVER_RAM; |
20
|
|
|
private $cacheInstance = null; |
21
|
|
|
private $params = array(); |
22
|
|
|
private $factory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Multiton - private construct |
26
|
|
|
*/ |
27
|
3 |
|
private function __construct() |
28
|
|
|
{ |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $instanceKey |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
3 |
|
final public static function getInstance($instanceKey) |
36
|
|
|
{ |
37
|
3 |
|
if (!isset(self::$instance[$instanceKey])) { |
38
|
3 |
|
$class = get_called_class(); |
39
|
|
|
/* @var $instance $this */ |
|
|
|
|
40
|
3 |
|
$instance = new $class; |
41
|
3 |
|
self::$instance[$instanceKey] = $instance->setDriver($instanceKey); |
42
|
|
|
} |
43
|
3 |
|
return self::$instance[$instanceKey]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Cache\CacheInterface |
48
|
|
|
* @throws Cache\Exception |
49
|
|
|
*/ |
50
|
6 |
|
final public function getCacheInstance() |
51
|
|
|
{ |
52
|
6 |
|
if (!is_null($this->cacheInstance)) { |
53
|
4 |
|
return $this->cacheInstance; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$this->cacheInstance = $this->getFactory()->create($this->driver, $this->params); |
57
|
|
|
|
58
|
1 |
|
return $this->cacheInstance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Define a factory |
63
|
|
|
* @param Factory $factory |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
1 |
|
public function setFactory(Factory $factory) |
67
|
|
|
{ |
68
|
1 |
|
$this->factory = $factory; |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get factory instance |
74
|
|
|
* @return Factory |
75
|
|
|
*/ |
76
|
3 |
|
public function getFactory() |
77
|
|
|
{ |
78
|
3 |
|
if (!$this->factory) { |
79
|
2 |
|
$this->factory = Factory::getInstance(); |
80
|
|
|
} |
81
|
3 |
|
return $this->factory; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* set cacheInstance |
86
|
|
|
* @param \FMUP\Cache\CacheInterface $instance |
87
|
|
|
* @return \FMUP\Cache |
88
|
|
|
*/ |
89
|
2 |
|
public function setCacheInstance(Cache\CacheInterface $instance) |
90
|
|
|
{ |
91
|
2 |
|
$this->cacheInstance = $instance; |
92
|
2 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* set driver |
97
|
|
|
* @param string $driver |
98
|
|
|
* @return \FMUP\Cache |
99
|
|
|
*/ |
100
|
4 |
|
public function setDriver($driver) |
101
|
|
|
{ |
102
|
4 |
|
$this->driver = $driver; |
103
|
4 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get defined driver |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
2 |
|
public function getDriver() |
111
|
|
|
{ |
112
|
2 |
|
return $this->driver; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* set params for construct \FMUP\Cache\CacheInterface |
117
|
|
|
* @param array $params |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function setParams(array $params) |
121
|
|
|
{ |
122
|
1 |
|
$this->params = (array)$params; |
123
|
1 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Driver settings |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
2 |
|
public function getParams() |
131
|
|
|
{ |
132
|
2 |
|
return $this->params; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* set a param in cache |
137
|
|
|
* @param string $key |
138
|
|
|
* @param mixed $value |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
1 |
|
public function set($key, $value) |
142
|
|
|
{ |
143
|
1 |
|
$this->getCacheInstance()->set((string)$key, $value); |
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* return value of a param |
149
|
|
|
* @param string $key |
150
|
|
|
* @return mixed|null |
151
|
|
|
*/ |
152
|
1 |
|
public function get($key) |
153
|
|
|
{ |
154
|
1 |
|
return $this->getCacheInstance()->get((string)$key); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* check param is set |
159
|
|
|
* @param string $key |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
2 |
|
public function has($key) |
163
|
|
|
{ |
164
|
2 |
|
return $this->getCacheInstance()->has((string)$key); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* remove param |
169
|
|
|
* @param string $key |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
1 |
|
public function remove($key) |
173
|
|
|
{ |
174
|
1 |
|
$this->getCacheInstance()->remove((string)$key); |
175
|
1 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.