|
1
|
|
|
<?php |
|
2
|
|
|
namespace NilPortugues\Cache; |
|
3
|
|
|
|
|
4
|
|
|
use NilPortugues\Cache\Adapter\InMemoryAdapter; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
final class Cache implements CacheAdapter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var CacheAdapter |
|
13
|
|
|
*/ |
|
14
|
|
|
private $cache; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $namespace; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param CacheAdapter $cache |
|
24
|
|
|
* @param string $namespace |
|
25
|
|
|
* @param int $expires |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(CacheAdapter $cache = null, $namespace = '', $expires = 0) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->cache = (null === $cache) ? InMemoryAdapter::getInstance() : $cache; |
|
30
|
|
|
$this->namespace = (empty($namespace)) ? '' : $namespace."."; |
|
31
|
|
|
|
|
32
|
|
|
if (0 !== $expires) { |
|
33
|
|
|
$this->defaultTtl($expires); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $key |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
public function get($key) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->cache->get($this->namespace.$key); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $key |
|
48
|
|
|
* @param int $value |
|
49
|
|
|
* @param null $ttl |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function set($key, $value, $ttl = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->cache->set($this->namespace.$key, $value, $ttl); |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Allows to set a default ttl value if none is provided for set() |
|
60
|
|
|
* |
|
61
|
|
|
* @param int $ttl |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool|mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function defaultTtl($ttl) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->cache->defaultTtl($ttl); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Delete a value identified by $key. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
*/ |
|
75
|
|
|
public function delete($key) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->cache->delete($this->namespace.$key); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Checks the availability of the cache service. |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function isAvailable() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->cache->isAvailable(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check if value was found in the cache or not. |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function isHit() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->cache->isHit(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Clears all expired values from cache. |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function clear() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->cache->clear(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Clears all values from the cache. |
|
112
|
|
|
* |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
public function drop() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->cache->drop(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|