1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of Phpfastcache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phpfastcache\Drivers\Memstatic; |
18
|
|
|
|
19
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait; |
21
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
22
|
|
|
use Phpfastcache\Entities\DriverStatistic; |
23
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
24
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
25
|
|
|
use Psr\Cache\CacheItemInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Driver |
29
|
|
|
* @method Config getConfig() |
30
|
|
|
*/ |
31
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
32
|
|
|
{ |
33
|
|
|
use TaggableCacheItemPoolTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected array $staticStack = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function driverCheck(): bool |
44
|
|
|
{ |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function driverConnect(): bool |
52
|
|
|
{ |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ExtendedCacheItemInterface $item |
58
|
|
|
* @return null|array |
59
|
|
|
*/ |
60
|
|
|
protected function driverRead(ExtendedCacheItemInterface $item): ?array |
61
|
|
|
{ |
62
|
|
|
return $this->staticStack[$item->getKey()] ?? null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ExtendedCacheItemInterface $item |
67
|
|
|
* @return bool |
68
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
69
|
|
|
* @throws PhpfastcacheLogicException |
70
|
|
|
*/ |
71
|
|
|
protected function driverWrite(ExtendedCacheItemInterface $item): bool |
72
|
|
|
{ |
73
|
|
|
$this->assertCacheItemType($item, Item::class); |
74
|
|
|
|
75
|
|
|
$this->staticStack[$item->getKey()] = $this->driverPreWrap($item); |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ExtendedCacheItemInterface $item |
81
|
|
|
* @return bool |
82
|
|
|
* @throws PhpfastcacheInvalidArgumentException |
83
|
|
|
*/ |
84
|
|
|
protected function driverDelete(ExtendedCacheItemInterface $item): bool |
85
|
|
|
{ |
86
|
|
|
$this->assertCacheItemType($item, Item::class); |
87
|
|
|
|
88
|
|
|
$key = $item->getKey(); |
89
|
|
|
if (isset($this->staticStack[$key])) { |
90
|
|
|
unset($this->staticStack[$key]); |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
protected function driverClear(): bool |
100
|
|
|
{ |
101
|
|
|
unset($this->staticStack); |
102
|
|
|
$this->staticStack = []; |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return DriverStatistic |
108
|
|
|
*/ |
109
|
|
|
public function getStats(): DriverStatistic |
110
|
|
|
{ |
111
|
|
|
$stat = new DriverStatistic(); |
112
|
|
|
$stat->setInfo('[Memstatic] A memory static driver') |
113
|
|
|
->setSize(mb_strlen(serialize($this->staticStack))) |
114
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
115
|
|
|
->setRawData($this->staticStack); |
116
|
|
|
|
117
|
|
|
return $stat; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|