1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Cache |
9
|
|
|
* |
10
|
|
|
* @package Suricate |
11
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @property string $type |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class Cache extends Service implements Interfaces\ICache |
17
|
|
|
{ |
18
|
|
|
protected $parametersList = ['type']; |
19
|
|
|
public static $container; |
20
|
|
|
protected $cacheTypes = [ |
21
|
|
|
'memcache' => 'Suricate\Suricate::CacheMemcache', |
22
|
|
|
'memcached' => 'Suricate\Suricate::CacheMemcached', |
23
|
|
|
'apc' => 'Suricate\Suricate::CacheApc', |
24
|
|
|
'file' => 'Suricate\Suricate::CacheFile' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected function init() |
28
|
|
|
{ |
29
|
|
|
if (static::$container === null) { |
30
|
|
|
if (isset($this->cacheTypes[$this->type])) { |
31
|
|
|
static::$container = $this->cacheTypes[$this->type](true); |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
throw new \Exception("Unknown cache type " . $this->type); |
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(string $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(string $variable) |
65
|
|
|
{ |
66
|
|
|
$this->init(); |
67
|
|
|
return static::$container->get($variable); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Delete a variable from cache |
72
|
|
|
* |
73
|
|
|
* @param string $variable The key to delete |
74
|
|
|
* @return boolean |
75
|
|
|
*/ |
76
|
|
|
public function delete(string $variable) |
77
|
|
|
{ |
78
|
|
|
$this->init(); |
79
|
|
|
return static::$container->delete($variable); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|