-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshmop.php
More file actions
193 lines (168 loc) · 4.29 KB
/
shmop.php
File metadata and controls
193 lines (168 loc) · 4.29 KB
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
187
188
189
190
191
192
193
<?php declare(strict_types=1);
namespace SHMCache;
/**
* Abstract and scalable shared memory operation
* Class shmop
* @package SHMCache
*/
abstract class shmop
{
/**
* System's id for the shared memory block.
* @var int
*/
protected $id;
/**
* The permissions that you wish to assign to your memory segment, those
* are the same as permission for a file. Permissions need to be passed
* in octal form, like for example 0644
* @var int
*/
protected $mode = 0644;
/**
* Get system's id for the shared memory block.
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get the permissions that you wish to assign to your memory segment
* @return int
*/
public function getMode()
{
return $this->mode;
}
/**
* shmop constructor.
* @param int $id [optional]
* @param int $mode [optional]
*/
public function __construct(int $id = 0, int $mode = 0)
{
if (empty($id)) {
$id = ftok(__FILE__, "b");
}
if ($mode > 0) {
$this->mode = $mode;
}
$this->id = $id;
}
/**
* Whether there is a system's id
* @param int $id [optional]
* @return bool
*/
protected function exists(int $id = 0): bool
{
return (bool)@shmop_open($id ? $id : $this->id, 'a', 0, 0);
}
/**
* Get the microsecond from microtime() and offset $microseconds
* @param int $microseconds [optional]
* @return int
*/
public function microtime(int $microseconds = 0): int
{
return intval(round(microtime(true) * 1000)) + $microseconds;
}
/**
* Hook to package the mixed data
* @param mixed $data
* @return mixed
*/
abstract protected function toPack($data);
/**
* Package to an array and serialize to a string
* @param mixed $data
* @param int $seconds [optional]
* @return string
*/
protected function pack($data, int $seconds = 0): string
{
return serialize(
array(
'data' => $this->toPack($data),
'timeout' => $seconds ? $this->microtime($seconds * 1000) : 0,
)
);
}
/**
* Hook to unpacking the mixed data
* @param mixed $data
* @return mixed
*/
abstract protected function toUnpack($data);
/**
* Unpacking a string and parse no timeout data from array
* @param string $data
* @return mixed|bool
*/
protected function unpack(string $data)
{
if ($data) {
$data = unserialize($data);
if (is_array($data) && isset($data['data']) && isset($data['timeout'])) {
$timeout = intval($data['timeout']);
if (!$timeout || $timeout >= $this->microtime()) {
return $this->toUnpack($data['data']);
}
}
}
return false;
}
/**
* Write data into shared memory block
* @param mixed $data
* @param int $seconds [optional]
* @return bool
*/
protected function write($data, int $seconds = 0): bool
{
if (!$data) {
return false;
}
$this->clean();
$data = $this->pack($data, $seconds);
$id = shmop_open($this->id, "n", $this->mode, strlen($data));
if (!$id) {
return false;
}
$size = shmop_write($id, $data, 0);
return !empty($size);
}
/**
* Read data from shared memory block
* @return bool|mixed
*/
protected function read()
{
if ($this->exists()) {
$id = shmop_open($this->id, "a", 0, 0);
if (!$id) {
return false;
}
$data = shmop_read($id, 0, shmop_size($id));
if (!$data) {
return false;
}
$data = $this->unpack($data);
shmop_close($id);
return $data;
}
return false;
}
/**
* Clean data from shared memory block
*/
protected function clean()
{
if ($this->exists()) {
$id = shmop_open($this->id, "a", 0, 0);
shmop_delete($id);
shmop_close($id);
}
}
}