1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 4/3/15 |
5
|
|
|
* Time: 1:17 PM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Cache\Adapter; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Cache\CacheAdapter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Adapter |
17
|
|
|
* @package NilPortugues\Cache\Adapter |
18
|
|
|
*/ |
19
|
|
|
abstract class Adapter implements CacheAdapter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $hit = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|int |
28
|
|
|
*/ |
29
|
|
|
protected $ttl; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CacheAdapter|null |
33
|
|
|
*/ |
34
|
|
|
protected $nextAdapter; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if value was found in the cache or not. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function isHit() |
43
|
|
|
{ |
44
|
|
|
return $this->hit; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Allows to set a default ttl value if none is provided for set() |
49
|
|
|
* |
50
|
|
|
* @param int $ttl |
51
|
|
|
* |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @return bool|mixed |
54
|
|
|
*/ |
55
|
|
|
public function defaultTtl($ttl) |
56
|
|
|
{ |
57
|
|
|
if (false === \is_numeric($ttl) || $ttl<0) { |
58
|
|
|
throw new \InvalidArgumentException('A TTL value must be a positive integer value'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->ttl = (int)$ttl; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $ttl |
67
|
|
|
* |
68
|
|
|
* @return int|null |
69
|
|
|
*/ |
70
|
|
|
protected function fromDefaultTtl($ttl) |
71
|
|
|
{ |
72
|
|
|
$ttl = (int) $ttl; |
73
|
|
|
|
74
|
|
|
if (0 == $ttl && null !== $this->ttl) { |
75
|
|
|
$ttl = $this->ttl; |
76
|
|
|
} |
77
|
|
|
return $ttl; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $value |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function storageDataStructure($value) |
86
|
|
|
{ |
87
|
|
|
return \serialize($value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $value |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
protected function restoreDataStructure($value) |
96
|
|
|
{ |
97
|
|
|
return \unserialize($value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
protected function clearChain() |
104
|
|
|
{ |
105
|
|
|
InMemoryAdapter::getInstance()->clear(); |
106
|
|
|
|
107
|
|
|
if (null !== $this->nextAdapter) { |
108
|
|
|
$this->nextAdapter->clear(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $key |
114
|
|
|
* @param $value |
115
|
|
|
* @param $ttl |
116
|
|
|
*/ |
117
|
|
|
protected function setChain($key, $value, $ttl) |
118
|
|
|
{ |
119
|
|
|
InMemoryAdapter::getInstance()->set($key, $value, $ttl); |
120
|
|
|
|
121
|
|
|
if (null !== $this->nextAdapter) { |
122
|
|
|
$this->nextAdapter->set($key, $value, $ttl); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
*/ |
129
|
|
|
protected function dropChain() |
130
|
|
|
{ |
131
|
|
|
InMemoryAdapter::getInstance()->drop(); |
132
|
|
|
|
133
|
|
|
if (null !== $this->nextAdapter) { |
134
|
|
|
$this->nextAdapter->drop(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $key |
141
|
|
|
*/ |
142
|
|
|
protected function deleteChain($key) |
143
|
|
|
{ |
144
|
|
|
InMemoryAdapter::getInstance()->delete($key); |
145
|
|
|
|
146
|
|
|
if (null !== $this->nextAdapter) { |
147
|
|
|
$this->nextAdapter->delete($key); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|