mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
update shared memory pool api
This commit is contained in:
parent
3ff79cb0be
commit
b18b9bb99e
3 changed files with 117 additions and 42 deletions
|
|
@ -17,34 +17,34 @@ abstract class Connection implements \Yggverse\Yoda\Interface\Model\Connection
|
||||||
$this->_pool = $pool ? $pool : new Pool;
|
$this->_pool = $pool ? $pool : new Pool;
|
||||||
|
|
||||||
// Set defaults
|
// Set defaults
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'completed'
|
'completed'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'title'
|
'title'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'subtitle'
|
'subtitle'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'tooltip'
|
'tooltip'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'mime'
|
'mime'
|
||||||
);
|
);
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'data'
|
'data'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'redirect'
|
'redirect'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_pool->set(
|
$this->_pool->init(
|
||||||
'request'
|
'request'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,18 +14,37 @@ interface Pool
|
||||||
?string $namespace = null
|
?string $namespace = null
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public function init(
|
||||||
|
string $key,
|
||||||
|
int $size = 0xfffff,
|
||||||
|
string $flags = 'c',
|
||||||
|
int $mode = 0644,
|
||||||
|
): ?\Shmop;
|
||||||
|
|
||||||
|
public function read(
|
||||||
|
string $key,
|
||||||
|
int $start = 0,
|
||||||
|
int $count = 0
|
||||||
|
): ?string;
|
||||||
|
|
||||||
|
public function write(
|
||||||
|
string $key,
|
||||||
|
string $value,
|
||||||
|
int $offset = 0
|
||||||
|
): int;
|
||||||
|
|
||||||
|
public function delete(
|
||||||
|
string $key
|
||||||
|
): bool;
|
||||||
|
|
||||||
public function get(
|
public function get(
|
||||||
string $key
|
string $key
|
||||||
): ?string;
|
): string;
|
||||||
|
|
||||||
public function set(
|
public function set(
|
||||||
string $key,
|
string $key,
|
||||||
?string $value = null,
|
?string $value = null
|
||||||
string $flags = 'c',
|
): void;
|
||||||
int $offset = 0,
|
|
||||||
int $mode = 0644,
|
|
||||||
?string $encoding = null
|
|
||||||
): int;
|
|
||||||
|
|
||||||
public function reset(): void;
|
public function reset(): void;
|
||||||
}
|
}
|
||||||
|
|
@ -18,12 +18,39 @@ class Pool implements \Yggverse\Yoda\Interface\Model\Pool
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(
|
public function init(
|
||||||
|
string $key,
|
||||||
|
int $size = 0xfffff,
|
||||||
|
string $flags = 'c',
|
||||||
|
int $mode = 0644,
|
||||||
|
): ?\Shmop
|
||||||
|
{
|
||||||
|
if (isset($this->_data[$key]))
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->_data[$key] = shmop_open(
|
||||||
|
crc32(
|
||||||
|
$this->_namespace . $key
|
||||||
|
),
|
||||||
|
$flags,
|
||||||
|
$mode,
|
||||||
|
$size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function read(
|
||||||
string $key,
|
string $key,
|
||||||
int $start = 0,
|
int $start = 0,
|
||||||
int $count = 0
|
int $count = 0
|
||||||
): ?string
|
): ?string
|
||||||
{
|
{
|
||||||
|
if (!isset($this->_data[$key]))
|
||||||
|
{
|
||||||
|
throw new \Exception;
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($this->_data[$key]))
|
if (empty($this->_data[$key]))
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -38,46 +65,75 @@ class Pool implements \Yggverse\Yoda\Interface\Model\Pool
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(
|
public function write(
|
||||||
string $key,
|
string $key,
|
||||||
?string $value = null,
|
string $value,
|
||||||
string $flags = 'c',
|
int $offset = 0
|
||||||
int $offset = 0,
|
|
||||||
int $mode = 0644,
|
|
||||||
?string $encoding = null
|
|
||||||
): int
|
): int
|
||||||
{
|
{
|
||||||
if (empty($value))
|
if (!isset($this->_data[$key]))
|
||||||
{
|
{
|
||||||
// @TODO delete from memory
|
throw new \Exception;
|
||||||
|
|
||||||
$this->_data[$key] = null;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->_data[$key] = shmop_open(crc32($this->_namespace . $key), $flags, $mode, mb_strlen($value, $encoding)))
|
return shmop_write(
|
||||||
|
$this->_data[$key],
|
||||||
|
$value,
|
||||||
|
$offset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(
|
||||||
|
string $key
|
||||||
|
): bool
|
||||||
|
{
|
||||||
|
if (!isset($this->_data[$key]))
|
||||||
{
|
{
|
||||||
return shmop_write(
|
throw new \Exception;
|
||||||
$this->_data[$key],
|
|
||||||
$value,
|
|
||||||
$offset
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \Exception;
|
$result = shmop_delete(
|
||||||
|
$this->_data[$key]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->_data[$key] = null;
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(
|
||||||
|
string $key
|
||||||
|
): string
|
||||||
|
{
|
||||||
|
return trim(
|
||||||
|
strval(
|
||||||
|
$this->read(
|
||||||
|
$key
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(
|
||||||
|
string $key,
|
||||||
|
?string $value = null
|
||||||
|
): void
|
||||||
|
{
|
||||||
|
$this->write(
|
||||||
|
$key,
|
||||||
|
strval(
|
||||||
|
$value
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reset(): void
|
public function reset(): void
|
||||||
{
|
{
|
||||||
foreach ($this->_data as $data)
|
foreach ($this->_data as $key => $shmop)
|
||||||
{
|
{
|
||||||
if ($data)
|
$this->delete(
|
||||||
{
|
$key
|
||||||
shmop_delete(
|
);
|
||||||
$data
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_data = [];
|
$this->_data = [];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue