1 | <?php |
||
17 | class SharedMemory implements CacheInterface |
||
18 | { |
||
19 | /** |
||
20 | * holds shared memory resource |
||
21 | * @var resource |
||
22 | */ |
||
23 | protected $shm; |
||
24 | |||
25 | /** |
||
26 | * shared memory ipc key |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $client_count_key = 'system_client_count'; |
||
30 | |||
31 | /** |
||
32 | * memory size |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $size; |
||
36 | |||
37 | /** |
||
38 | * @param int $size memory size |
||
39 | * @param string $file |
||
40 | */ |
||
41 | 9 | public function __construct($size = 33554432, $file = __FILE__) |
|
54 | |||
55 | /** |
||
56 | * connect shared memory |
||
57 | * |
||
58 | * @param string $file |
||
59 | */ |
||
60 | 9 | public function attach($file = __FILE__) |
|
61 | { |
||
62 | 9 | if (!file_exists($file)) { |
|
63 | $touch = touch($file); |
||
64 | if (!$touch) { |
||
65 | throw new \RuntimeException("file is not exists and it can not be created. file: {$file}"); |
||
66 | } |
||
67 | } |
||
68 | 9 | $key = ftok($file, 'a'); |
|
69 | 9 | $this->shm = shm_attach($key, $this->size); //allocate shared memory |
|
70 | 9 | } |
|
71 | |||
72 | /** |
||
73 | * remove shared memory. |
||
74 | * you should know that it maybe does not work. |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function remove() |
||
79 | { |
||
80 | //dallocate shared memory |
||
81 | if (!shm_remove($this->shm)) { |
||
82 | return false; |
||
83 | } |
||
84 | $this->dettach(); |
||
85 | // shm_remove maybe not working. it likes a php bug. |
||
86 | unset($this->shm); |
||
87 | |||
88 | return true; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function dettach() |
||
95 | { |
||
96 | return shm_detach($this->shm); //allocate shared memory |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * set var |
||
101 | * |
||
102 | * @param $key |
||
103 | * @param $value |
||
104 | * @return bool |
||
105 | */ |
||
106 | 6 | public function set($key, $value) |
|
107 | { |
||
108 | 6 | return shm_put_var($this->shm, $this->shm_key($key), $value); //store var |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * generate shm key |
||
113 | * |
||
114 | * @param $val |
||
115 | * @return mixed |
||
116 | */ |
||
117 | 9 | public function shm_key($val) |
|
118 | { // enable all world langs and chars ! |
||
119 | // text to number system. |
||
120 | 9 | return preg_replace("/[^0-9]/", "", (preg_replace("/[^0-9]/", "", md5($val)) / 35676248) / 619876); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * get var |
||
125 | * |
||
126 | * @param $key |
||
127 | * @param null $default |
||
128 | * @return bool|mixed |
||
129 | */ |
||
130 | 9 | public function get($key, $default = null) |
|
131 | { |
||
132 | 9 | if ($this->has($key)) { |
|
133 | 9 | return shm_get_var($this->shm, $this->shm_key($key)); |
|
134 | } else { |
||
135 | return $default; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * has var ? |
||
141 | * |
||
142 | * @param $key |
||
143 | * @return bool |
||
144 | */ |
||
145 | 9 | public function has($key) |
|
146 | { |
||
147 | 9 | if (shm_has_var($this->shm, $this->shm_key($key))) { // check is isset |
|
148 | 9 | return true; |
|
149 | } else { |
||
150 | 3 | return false; |
|
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * delete var |
||
156 | * |
||
157 | * @param $key |
||
158 | * @return bool |
||
159 | */ |
||
160 | 3 | public function delete($key) |
|
161 | { |
||
162 | 3 | if ($this->has($key)) { |
|
163 | 3 | return shm_remove_var($this->shm, $this->shm_key($key)); |
|
164 | } else { |
||
165 | return false; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * init when wakeup |
||
171 | */ |
||
172 | public function __wakeup() |
||
176 | } |