1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Process Library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\Process\SystemV; |
7
|
|
|
|
8
|
|
|
class SharedMemory |
9
|
|
|
{ |
10
|
|
|
use IpcKeyTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The resource that be generated after call "shm_attach" |
14
|
|
|
* @var resource |
15
|
|
|
*/ |
16
|
|
|
protected $shmId; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The size of the shared memory |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $size; |
23
|
|
|
|
24
|
|
|
public function __construct($pathname = __FILE__, $size = null, $permission = 0666) |
25
|
|
|
{ |
26
|
|
|
if (!is_null($size)) { |
27
|
|
|
$this->size = static::humanReadableToBytes($size); |
28
|
|
|
} else { |
29
|
|
|
$this->size = (int)ini_get('sysvshm.init_mem') ?: 10000; |
30
|
|
|
} |
31
|
|
|
$ipcKey = $this->generateIpcKey($pathname); |
32
|
|
|
$this->shmId = shm_attach($ipcKey, $this->size, $permission); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Gets a value from the shared memory |
37
|
|
|
* @param $key |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function get($key) |
41
|
|
|
{ |
42
|
|
|
return shm_get_var($this->shmId, $this->generateShmKey($key)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Persists data in the shared memory |
47
|
|
|
* @param string $key |
48
|
|
|
* @param mixed $value |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function set($key, $value) |
52
|
|
|
{ |
53
|
|
|
return shm_put_var($this->shmId, $this->generateShmKey($key), $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Delete an item from the shared memory by its key |
58
|
|
|
* @param string $key |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function delete($key) |
62
|
|
|
{ |
63
|
|
|
return shm_remove_var($this->shmId, $this->generateShmKey($key)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether an item exists in the shared memory |
68
|
|
|
* @param string $key |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function has($key) |
72
|
|
|
{ |
73
|
|
|
return shm_has_var($this->shmId, $this->generateShmKey($key)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Deletes all items |
78
|
|
|
*/ |
79
|
|
|
public function clear() |
80
|
|
|
{ |
81
|
|
|
return shm_remove($this->shmId); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Checks whether the memory is enabled |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function isEnabled() |
89
|
|
|
{ |
90
|
|
|
return is_resource($this->shmId); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Disconnects from shared memory |
95
|
|
|
*/ |
96
|
|
|
public function close() |
97
|
|
|
{ |
98
|
|
|
is_resource($this->shmId) && shm_detach($this->shmId); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Removes all items and disconnects from shared memory |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function destroy() |
107
|
|
|
{ |
108
|
|
|
if (is_resource($this->shmId)) { |
109
|
|
|
shm_remove($this->shmId); |
110
|
|
|
shm_detach($this->shmId); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Generate the variable key |
116
|
|
|
* @param string $val |
117
|
|
|
* @return string int |
118
|
|
|
*/ |
119
|
|
|
protected function generateShmKey($val) |
120
|
|
|
{ |
121
|
|
|
// enable all world langs and chars ! |
122
|
|
|
return preg_replace("/[^0-9]/", "", (preg_replace("/[^0-9]/", "", md5($val))/35676248)/619876); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert human readable file size (e.g. "10K" or "3M") into bytes |
127
|
|
|
* @link https://github.com/brandonsavage/Upload/blob/master/src/Upload/File.php#L446 |
128
|
|
|
* @param string $input |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public static function humanReadableToBytes($input) |
132
|
|
|
{ |
133
|
|
|
$number = (int)$input; |
134
|
|
|
$units = array( |
135
|
|
|
'b' => 1, |
136
|
|
|
'k' => 1024, |
137
|
|
|
'm' => 1048576, |
138
|
|
|
'g' => 1073741824 |
139
|
|
|
); |
140
|
|
|
$unit = strtolower(substr($input, -1)); |
141
|
|
|
if (isset($units[$unit])) { |
142
|
|
|
$number = $number * $units[$unit]; |
143
|
|
|
} |
144
|
|
|
return $number; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|