1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Lucas Brucksch <[email protected]> |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpFastCache\Drivers\Zendshm; |
15
|
|
|
|
16
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
17
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
18
|
|
|
use phpFastCache\Entities\driverStatistic; |
19
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
20
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
22
|
|
|
use Psr\Cache\CacheItemInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Driver (zend memory cache) |
26
|
|
|
* Requires Zend Data Cache Functions from ZendServer |
27
|
|
|
* @package phpFastCache\Drivers |
28
|
|
|
*/ |
29
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
30
|
|
|
{ |
31
|
|
|
use DriverBaseTrait; |
32
|
|
|
/** |
33
|
|
|
* Driver constructor. |
34
|
|
|
* @param array $config |
35
|
|
|
* @throws phpFastCacheDriverException |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $config = []) |
38
|
|
|
{ |
39
|
|
|
$this->setup($config); |
40
|
|
|
|
41
|
|
|
if (!$this->driverCheck()) { |
42
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function driverCheck() |
50
|
|
|
{ |
51
|
|
|
if (extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store')) { |
52
|
|
|
return true; |
53
|
|
|
} else { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
60
|
|
|
* @return mixed |
61
|
|
|
* @throws phpFastCacheInvalidArgumentException |
62
|
|
|
*/ |
63
|
|
|
protected function driverWrite(CacheItemInterface $item) |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* Check for Cross-Driver type confusion |
67
|
|
|
*/ |
68
|
|
|
if ($item instanceof Item) { |
69
|
|
|
$ttl = $item->getExpirationDate()->getTimestamp() - time(); |
70
|
|
|
|
71
|
|
|
return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
72
|
|
|
} else { |
73
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
protected function driverRead(CacheItemInterface $item) |
82
|
|
|
{ |
83
|
|
|
$data = zend_shm_cache_fetch($item->getKey()); |
84
|
|
|
if ($data === false) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
93
|
|
|
* @return bool |
94
|
|
|
* @throws phpFastCacheInvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
protected function driverDelete(CacheItemInterface $item) |
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* Check for Cross-Driver type confusion |
100
|
|
|
*/ |
101
|
|
|
if ($item instanceof Item) { |
102
|
|
|
return zend_shm_cache_delete($item->getKey()); |
103
|
|
|
} else { |
104
|
|
|
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
protected function driverClear() |
112
|
|
|
{ |
113
|
|
|
return @zend_shm_cache_clear(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
protected function driverConnect() |
120
|
|
|
{ |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/******************** |
125
|
|
|
* |
126
|
|
|
* PSR-6 Extended Methods |
127
|
|
|
* |
128
|
|
|
*******************/ |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public static function getHelp() |
134
|
|
|
{ |
135
|
|
|
return <<<HELP |
136
|
|
|
<p> |
137
|
|
|
This driver rely on Zend Server 8.5+, see: http://www.zend.com/en/products/zend_server |
138
|
|
|
</p> |
139
|
|
|
HELP; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return driverStatistic |
144
|
|
|
*/ |
145
|
|
|
public function getStats() |
146
|
|
|
{ |
147
|
|
|
$stats = (array) zend_shm_cache_info(); |
148
|
|
|
return (new driverStatistic()) |
149
|
|
|
->setData(implode(', ', array_keys($this->namespaces))) |
|
|
|
|
150
|
|
|
->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.",$stats[ 'items_total' ])) |
151
|
|
|
->setRawData($stats) |
152
|
|
|
->setSize($stats[ 'memory_total' ]); |
153
|
|
|
} |
154
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: