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 = ['type']; |
16
|
|
|
public static $container; |
17
|
|
|
protected $cacheTypes = [ |
18
|
|
|
'memcache' => 'Suricate\Suricate::CacheMemcache', |
19
|
|
|
'memcached' => 'Suricate\Suricate::CacheMemcached', |
20
|
|
|
'apc' => 'Suricate\Suricate::CacheApc', |
21
|
|
|
'file' => 'Suricate\Suricate::CacheFile', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected function init() |
25
|
|
|
{ |
26
|
|
|
if (static::$container === null) { |
27
|
|
|
if (isset($this->cacheTypes[$this->type])) { |
28
|
|
|
static::$container = $this->cacheTypes[$this->type](true); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
throw new \Exception("Unknown cache type " . $this->type); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getInstance() |
36
|
|
|
{ |
37
|
|
|
$this->init(); |
38
|
|
|
return static::$container; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Setter |
43
|
|
|
* Put a variable in cache |
44
|
|
|
* @param string $variable The key that will be associated with the item. |
45
|
|
|
* @param mixed $value The variable to store. |
46
|
|
|
* @param int $expiry Expiration time of the item. If it's equal to zero, the item will never expire. |
47
|
|
|
* You can also use Unix timestamp or a number of seconds starting from current time, |
48
|
|
|
* but in the latter case the number of seconds may not exceed 2592000 (30 days). |
49
|
|
|
*/ |
50
|
|
|
public function set(string $variable, $value, $expiry = null) |
51
|
|
|
{ |
52
|
|
|
$this->init(); |
53
|
|
|
return static::$container->set($variable, $value, $expiry); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a variable from cache |
58
|
|
|
* @param string $variable The key to fetch |
59
|
|
|
* @return mixed Data fetched from cache, false if not found |
60
|
|
|
*/ |
61
|
|
|
public function get(string $variable) |
62
|
|
|
{ |
63
|
|
|
$this->init(); |
64
|
|
|
return static::$container->get($variable); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function delete(string $variable) |
68
|
|
|
{ |
69
|
|
|
$this->init(); |
70
|
|
|
return static::$container->delete($variable); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|