mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
implement shared memory pool model
This commit is contained in:
parent
1d2641dceb
commit
8b28265204
2 changed files with 86 additions and 3 deletions
|
|
@ -10,12 +10,22 @@ namespace Yggverse\Yoda\Interface\Model;
|
|||
*/
|
||||
interface Pool
|
||||
{
|
||||
public function __construct(
|
||||
?string $namespace = null
|
||||
);
|
||||
|
||||
public function get(
|
||||
string $key
|
||||
): mixed;
|
||||
): ?string;
|
||||
|
||||
public function set(
|
||||
string $key,
|
||||
mixed $value
|
||||
): void;
|
||||
string $value,
|
||||
string $flags = 'c',
|
||||
int $offset = 0,
|
||||
int $mode = 0644,
|
||||
?string $encoding = null
|
||||
): int;
|
||||
|
||||
public function reset(): void;
|
||||
}
|
||||
73
src/Model/Pool.php
Normal file
73
src/Model/Pool.php
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Yoda\Model;
|
||||
|
||||
class Pool implements \Yggverse\Yoda\Interface\Model\Pool
|
||||
{
|
||||
private string $_namespace;
|
||||
|
||||
private array $_data = [];
|
||||
|
||||
public function __construct(
|
||||
?string $namespace = null
|
||||
) {
|
||||
$this->_namespace = __FILE__ . (
|
||||
$namespace ? $namespace : uniqid()
|
||||
);
|
||||
}
|
||||
|
||||
public function get(
|
||||
string $key,
|
||||
int $start = 0,
|
||||
int $count = 0
|
||||
): ?string
|
||||
{
|
||||
if (isset($this->_data[$key]))
|
||||
{
|
||||
return shmop_read(
|
||||
$this->_data[$key],
|
||||
$start,
|
||||
$count ? $count : shmop_size(
|
||||
$this->_data[$key]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function set(
|
||||
string $key,
|
||||
string $value,
|
||||
string $flags = 'c',
|
||||
int $offset = 0,
|
||||
int $mode = 0644,
|
||||
?string $encoding = null
|
||||
): int
|
||||
{
|
||||
if ($this->_data[$key] = shmop_open(crc32($this->_namespace . $key), $flags, $mode, mb_strlen($value, $encoding)))
|
||||
{
|
||||
return shmop_write(
|
||||
$this->_data[$key],
|
||||
$value,
|
||||
$offset
|
||||
);
|
||||
}
|
||||
|
||||
throw new \Exception;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
foreach ($this->_data as $data)
|
||||
{
|
||||
shmop_delete(
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
$this->_data = [];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue