|
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
|
|
View Code Duplication |
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 'apc': |
|
26
|
|
|
static::$container = Suricate::CacheApc(true); |
|
27
|
|
|
break; |
|
28
|
|
|
case 'file': |
|
29
|
|
|
static::$container = Suricate::CacheFile(true); |
|
30
|
|
|
break; |
|
31
|
|
|
default: |
|
32
|
|
|
throw new \Exception("Unknown cache type " . $this->type); |
|
33
|
|
|
break; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getInstance() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->init(); |
|
41
|
|
|
return static::$container; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Setter |
|
46
|
|
|
* Put a variable in cache |
|
47
|
|
|
* @param string $variable The key that will be associated with the item. |
|
48
|
|
|
* @param mixed $value The variable to store. |
|
49
|
|
|
* @param int $expiry Expiration time of the item. If it's equal to zero, the item will never expire. |
|
50
|
|
|
* You can also use Unix timestamp or a number of seconds starting from current time, |
|
51
|
|
|
* but in the latter case the number of seconds may not exceed 2592000 (30 days). |
|
52
|
|
|
*/ |
|
53
|
|
|
public function set($variable, $value, $expiry = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->init(); |
|
56
|
|
|
return static::$container->set($variable, $value, $expiry); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get a variable from cache |
|
61
|
|
|
* @param string $variable The key to fetch |
|
62
|
|
|
* @return mixed Data fetched from cache, false if not found |
|
63
|
|
|
*/ |
|
64
|
|
|
public function get($variable) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->init(); |
|
67
|
|
|
return static::$container->get($variable); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.