diff --git a/src/Interface/Model/Pool.php b/src/Interface/Model/Pool.php index 5839cb64..39d46bc0 100644 --- a/src/Interface/Model/Pool.php +++ b/src/Interface/Model/Pool.php @@ -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; } \ No newline at end of file diff --git a/src/Model/Pool.php b/src/Model/Pool.php new file mode 100644 index 00000000..b330bbb8 --- /dev/null +++ b/src/Model/Pool.php @@ -0,0 +1,73 @@ +_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 = []; + } +} \ No newline at end of file