|
1
|
|
|
<?php |
|
2
|
|
|
namespace Suricate; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Cache |
|
6
|
|
|
* |
|
7
|
|
|
* @package Suricate |
|
8
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @property string $type |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Cache extends Service implements Interfaces\ICache |
|
14
|
|
|
{ |
|
15
|
|
|
protected $parametersList = array('type'); |
|
16
|
|
|
public static $container; |
|
17
|
|
|
|
|
18
|
|
|
protected function init() |
|
19
|
|
|
{ |
|
20
|
|
|
if (static::$container === null) { |
|
21
|
|
|
switch ($this->type) { |
|
22
|
|
|
case 'memcache': |
|
23
|
|
|
static::$container = Suricate::CacheMemcache(true); |
|
24
|
|
|
break; |
|
25
|
|
|
case 'memcached': |
|
26
|
|
|
static::$container = Suricate::CacheMemcached(true); |
|
27
|
|
|
break; |
|
28
|
|
|
case 'apc': |
|
29
|
|
|
static::$container = Suricate::CacheApc(true); |
|
30
|
|
|
break; |
|
31
|
|
|
case 'file': |
|
32
|
|
|
static::$container = Suricate::CacheFile(true); |
|
33
|
|
|
break; |
|
34
|
|
|
default: |
|
35
|
|
|
throw new \Exception("Unknown cache type " . $this->type); |
|
36
|
|
|
break; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getInstance() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->init(); |
|
44
|
|
|
return static::$container; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Setter |
|
49
|
|
|
* Put a variable in cache |
|
50
|
|
|
* @param string $variable The key that will be associated with the item. |
|
51
|
|
|
* @param mixed $value The variable to store. |
|
52
|
|
|
* @param int $expiry Expiration time of the item. If it's equal to zero, the item will never expire. |
|
53
|
|
|
* You can also use Unix timestamp or a number of seconds starting from current time, |
|
54
|
|
|
* but in the latter case the number of seconds may not exceed 2592000 (30 days). |
|
55
|
|
|
*/ |
|
56
|
|
|
public function set(string $variable, $value, $expiry = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->init(); |
|
59
|
|
|
return static::$container->set($variable, $value, $expiry); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a variable from cache |
|
64
|
|
|
* @param string $variable The key to fetch |
|
65
|
|
|
* @return mixed Data fetched from cache, false if not found |
|
66
|
|
|
*/ |
|
67
|
|
|
public function get(string $variable) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->init(); |
|
70
|
|
|
return static::$container->get($variable); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.