1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Cache\Storage\Adapter;
use Zend\Cache\Exception;
use Zend\Cache\Storage\AvailableSpaceCapableInterface;
use Zend\Cache\Storage\ClearByNamespaceInterface;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\TotalSpaceCapableInterface;
use Zend\Stdlib\ErrorHandler;
class ZendServerDisk extends AbstractZendServer implements
AvailableSpaceCapableInterface,
ClearByNamespaceInterface,
FlushableInterface,
TotalSpaceCapableInterface
{
/**
* Buffered total space in bytes
*
* @var null|int|float
*/
protected $totalSpace;
/**
* Constructor
*
* @param null|array|\Traversable|AdapterOptions $options
* @throws Exception\ExtensionNotLoadedException
*/
public function __construct($options = [])
{
if (! function_exists('zend_disk_cache_store')) {
throw new Exception\ExtensionNotLoadedException("Missing 'zend_disk_cache_*' functions");
} elseif (PHP_SAPI == 'cli') {
throw new Exception\ExtensionNotLoadedException("Zend server data cache isn't available on cli");
}
parent::__construct($options);
}
/* FlushableInterface */
/**
* Flush the whole storage
*
* @return bool
*/
public function flush()
{
return zend_disk_cache_clear();
}
/* ClearByNamespaceInterface */
/**
* Remove items of given namespace
*
* @param string $namespace
* @return bool
*/
public function clearByNamespace($namespace)
{
$namespace = (string) $namespace;
if ($namespace === '') {
throw new Exception\InvalidArgumentException('No namespace given');
}
return zend_disk_cache_clear($namespace);
}
/* TotalSpaceCapableInterface */
/**
* Get total space in bytes
*
* @throws Exception\RuntimeException
* @return int|float
*/
public function getTotalSpace()
{
if ($this->totalSpace === null) {
$path = ini_get('zend_datacache.disk.save_path');
ErrorHandler::start();
$total = disk_total_space($path);
$error = ErrorHandler::stop();
if ($total === false) {
throw new Exception\RuntimeException("Can't detect total space of '{$path}'", 0, $error);
}
$this->totalSpace = $total;
}
return $this->totalSpace;
}
/* AvailableSpaceCapableInterface */
/**
* Get available space in bytes
*
* @throws Exception\RuntimeException
* @return int|float
*/
public function getAvailableSpace()
{
$path = ini_get('zend_datacache.disk.save_path');
ErrorHandler::start();
$avail = disk_free_space($path);
$error = ErrorHandler::stop();
if ($avail === false) {
throw new Exception\RuntimeException("Can't detect free space of '{$path}'", 0, $error);
}
return $avail;
}
/* internal */
/**
* Store data into Zend Data Disk Cache
*
* @param string $internalKey
* @param mixed $value
* @param int $ttl
* @return void
* @throws Exception\RuntimeException
*/
protected function zdcStore($internalKey, $value, $ttl)
{
if (! zend_disk_cache_store($internalKey, $value, $ttl)) {
$valueType = gettype($value);
throw new Exception\RuntimeException(
"zend_disk_cache_store($internalKey, <{$valueType}>, {$ttl}) failed"
);
}
}
/**
* Fetch a single item from Zend Data Disk Cache
*
* @param string $internalKey
* @return mixed The stored value or FALSE if item wasn't found
* @throws Exception\RuntimeException
*/
protected function zdcFetch($internalKey)
{
return zend_disk_cache_fetch((string) $internalKey);
}
/**
* Fetch multiple items from Zend Data Disk Cache
*
* @param array $internalKeys
* @return array All found items
* @throws Exception\RuntimeException
*/
protected function zdcFetchMulti(array $internalKeys)
{
$items = zend_disk_cache_fetch($internalKeys);
if ($items === false) {
throw new Exception\RuntimeException("zend_disk_cache_fetch(<array>) failed");
}
return $items;
}
/**
* Delete data from Zend Data Disk Cache
*
* @param string $internalKey
* @return bool
* @throws Exception\RuntimeException
*/
protected function zdcDelete($internalKey)
{
return zend_disk_cache_delete($internalKey);
}
}