Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <?php
  2.  
  3. class MemoryManager
  4. {
  5. public function __construct($maxSize, $maxClients)
  6. {
  7.  
  8. // Validation
  9. $shm_key = ftok(__FILE__, 't');
  10. $this->_memoryPointer = shmop_open($shm_key, "c", 0644, $maxSize * $maxClients);
  11. $this->_maxSize = $maxSize;
  12. $this->_maxClient = $maxSize;
  13. }
  14.  
  15. public function writeClientData($clientId, $data)
  16. {
  17. $paddedData = str_pad($data, $this->_maxSize, $this->_padSymbol);
  18. shmop_write(
  19. $this->_memoryPointer,
  20. $paddedData,
  21. ($clientId - 1) * $this->_maxSize
  22. );
  23. }
  24.  
  25. public function readClientData($clientId)
  26. {
  27. $data = shmop_read(
  28. $this->_memoryPointer,
  29. ($clientId - 1) * $this->_maxSize,
  30. $this->_maxSize
  31. );
  32.  
  33. return trim($data, $this->_padSymbol);
  34. }
  35.  
  36. public function close()
  37. {
  38. shmop_delete($this->_memoryPointer);
  39. shmop_close($this->_memoryPointer);
  40. }
  41.  
  42.  
  43. private $_padSymbol = "|";
  44. }
  45.  
  46.  
  47. $mem = new MemoryManager(100, 100);
  48.  
  49. $mem->writeClientData(1, 'aaaaa');
  50. $mem->writeClientData(2, 'bbbbbbbb');
  51. var_dump($mem->readClientData(1));
  52. var_dump($mem->readClientData(2));
  53.  
  54.  
  55. $mem->writeClientData(2, 'cc');
  56. var_dump($mem->readClientData(2));
  57.  
  58.  
  59. $mem->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement