Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MemoryManager
- {
- public function __construct($maxSize, $maxClients)
- {
- // Validation
- $shm_key = ftok(__FILE__, 't');
- $this->_memoryPointer = shmop_open($shm_key, "c", 0644, $maxSize * $maxClients);
- $this->_maxSize = $maxSize;
- $this->_maxClient = $maxSize;
- }
- public function writeClientData($clientId, $data)
- {
- $paddedData = str_pad($data, $this->_maxSize, $this->_padSymbol);
- shmop_write(
- $this->_memoryPointer,
- $paddedData,
- ($clientId - 1) * $this->_maxSize
- );
- }
- public function readClientData($clientId)
- {
- $data = shmop_read(
- $this->_memoryPointer,
- ($clientId - 1) * $this->_maxSize,
- $this->_maxSize
- );
- return trim($data, $this->_padSymbol);
- }
- public function close()
- {
- shmop_delete($this->_memoryPointer);
- shmop_close($this->_memoryPointer);
- }
- private $_padSymbol = "|";
- }
- $mem = new MemoryManager(100, 100);
- $mem->writeClientData(1, 'aaaaa');
- $mem->writeClientData(2, 'bbbbbbbb');
- var_dump($mem->readClientData(1));
- var_dump($mem->readClientData(2));
- $mem->writeClientData(2, 'cc');
- var_dump($mem->readClientData(2));
- $mem->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement