1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BlueCache; |
4
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemInterface; |
6
|
|
|
|
7
|
|
|
class CacheItem implements CacheItemInterface |
8
|
|
|
{ |
9
|
|
|
const ALLOWED_KEY_CHARS = '#^[\w_-]+$#'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $key; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
protected $data; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $expire; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $config = [ |
30
|
|
|
'expire' => 86400, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CacheItem constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $key |
37
|
|
|
* @param array $config |
38
|
|
|
* @throws \BlueCache\CacheException |
39
|
|
|
*/ |
40
|
36 |
|
public function __construct($key, array $config = []) |
41
|
|
|
{ |
42
|
36 |
|
if ($this->isKeyInvalid($key)) { |
43
|
1 |
|
throw new CacheException('Invalid key. Should us only chars, numbers and _. Use: ' . $key); |
44
|
|
|
} |
45
|
|
|
|
46
|
35 |
|
$this->key = $key; |
47
|
35 |
|
$this->config = array_merge($this->config, $config); |
48
|
35 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
36 |
|
protected function isKeyInvalid($key) |
55
|
|
|
{ |
56
|
36 |
|
return !preg_match(self::ALLOWED_KEY_CHARS, $key); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
31 |
|
public function getKey() |
63
|
|
|
{ |
64
|
31 |
|
return $this->key; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return mixed|null |
69
|
|
|
*/ |
70
|
8 |
|
public function get() |
71
|
|
|
{ |
72
|
8 |
|
return $this->isHit() ? $this->data : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* check that cashed data exists |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
30 |
|
public function isHit() |
81
|
|
|
{ |
82
|
30 |
|
if (is_null($this->data)) { |
83
|
2 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
29 |
|
if (is_null($this->expire)) { |
|
|
|
|
87
|
28 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
return !($this->expire <= time()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
31 |
|
public function set($value) |
98
|
|
|
{ |
99
|
31 |
|
$this->data = $value; |
100
|
31 |
|
$this->expire = null; |
101
|
|
|
|
102
|
31 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \DateTimeInterface|null $expiration |
107
|
|
|
* @return $this |
108
|
|
|
* @throws \BlueCache\CacheException |
109
|
|
|
*/ |
110
|
1 |
|
public function expiresAt($expiration = null) |
111
|
|
|
{ |
112
|
1 |
|
return $this->setExpiration($expiration); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \DateTimeInterface|null|int $expire |
117
|
|
|
* @return $this |
118
|
|
|
* @throws \BlueCache\CacheException |
119
|
|
|
*/ |
120
|
6 |
|
protected function setExpiration($expire) |
121
|
|
|
{ |
122
|
6 |
|
switch (true) { |
123
|
6 |
|
case is_null($expire): |
124
|
1 |
|
$this->expire = $this->config['expire'] + time(); |
125
|
1 |
|
break; |
126
|
|
|
|
127
|
5 |
|
case is_int($expire): |
128
|
4 |
|
$this->expire = time() + $expire; |
129
|
4 |
|
break; |
130
|
|
|
|
131
|
3 |
|
case $expire instanceof \DateTimeInterface: |
132
|
1 |
|
$this->expire = $expire->getTimestamp(); |
133
|
1 |
|
break; |
134
|
|
|
|
135
|
2 |
|
case $expire instanceof \DateInterval: |
136
|
1 |
|
$this->expire = (new \DateTime) |
137
|
1 |
|
->add($expire) |
138
|
1 |
|
->getTimestamp(); |
139
|
1 |
|
break; |
140
|
|
|
|
141
|
1 |
|
default: |
142
|
1 |
|
throw new CacheException('Invalid expire type.'); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
5 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \DateInterval|int|null $time |
150
|
|
|
* @return $this |
151
|
|
|
* @throws \BlueCache\CacheException |
152
|
|
|
*/ |
153
|
5 |
|
public function expiresAfter($time = null) |
154
|
|
|
{ |
155
|
5 |
|
return $this->setExpiration($time); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|