Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: PHP  |  size: 3.55 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class CacheSharedMemory {
  2.        
  3.         protected function encode($value) {
  4.                 return serialize($value);
  5.         }
  6.         protected function decode($value){
  7.                 return unserialize($value);
  8.         }
  9.        
  10.         protected function deleteSMB($ID) {
  11.                 $shm_id = @shmop_open($ID, "w", 0, 0);
  12.                 @shmop_delete($shm_id);
  13.                 @shmop_close($shm_id);
  14.         }
  15.         protected function writeSMB($ID, $value, $perm = 0644) {
  16.                 $this->deleteSMB($ID);
  17.                 $value = $this->encode($value);
  18.                 $shm_id = shmop_open($ID, "c", $perm, strlen($value));
  19.                 shmop_write($shm_id, $value, 0);
  20.                 shmop_close($shm_id);
  21.         }
  22.         protected function readSMB($ID) {
  23.                 $shm_id = @shmop_open($ID, "a", 0, 0);
  24.                 $value = $this->decode(@shmop_read($shm_id, 0, @shmop_size($shm_id)));
  25.                 @shmop_close($shm_id);
  26.                 return $value;
  27.         }
  28.                
  29.         protected function getRegistry() {
  30.                 $reg = $this->readSMB(1);
  31.                 if(is_array($reg)) {
  32.                         return $reg;
  33.                 } else {
  34.                         $this->writeSMB(1, array());
  35.                         return $this->getRegistry();
  36.                 }
  37.         }
  38.         protected function handleRegistered($handle) {
  39.                 $reg = $this->getRegistry();
  40.                 if(in_array($handle, $reg))
  41.                         return array_search($handle, $reg);
  42.                 else
  43.                         return FALSE;
  44.         }
  45.         protected function registerHandle($handle) {
  46.                 if(is_string($handle)) {
  47.                         $key = $this->handleRegistered($handle);
  48.                         if(is_int($key)) {
  49.                                 return $key;
  50.                         } else {
  51.                                 $array = $this->getRegistry();
  52.                                 array_push($array, "".$handle);
  53.                                 $this->writeSMB(1, $array);
  54.                                 return $this->handleRegistered($handle);
  55.                         }
  56.                 }
  57.         }
  58.         protected function unregisterByKey($key) {
  59.                 if(is_int($key)) {
  60.                         $array = $this->getRegistry();
  61.                         if($array[$key]) {
  62.                                 unset($array[$key]);
  63.                                 $this->writeSMB(1, $array);
  64.                                 $this->deleteSMB($key + 2);
  65.                                 return TRUE;
  66.                         } else {
  67.                                 return FALSE;
  68.                         }
  69.                 } else {
  70.                         return FALSE;
  71.                 }
  72.         }
  73.         protected function unregisterByHandle($handle) {
  74.                 return $this->unregisterByKey($this->handleRegistered($handle));
  75.         }
  76.        
  77.         public function set($handle, $value) {
  78.                 $this->writeSMB($this->registerHandle($handle) + 2, $value);
  79.         }
  80.         public function get($handle) {
  81.                 if($key = $this->handleRegistered($handle)) {
  82.                         return $this->readSMB($key + 2);
  83.                 } else {
  84.                         return FALSE;
  85.                 }
  86.         }
  87.        
  88.         public function is_set($handle) {
  89.                 return is_int($this->handleRegistered($handle));
  90.         }
  91.        
  92.         public function clear($handle) {
  93.                 $key = $this->handleRegistered($handle);
  94.                 if(is_int($key)) {
  95.                         $this->unregisterByKey($key);
  96.                         return TRUE;
  97.                 } else {
  98.                         return FALSE;
  99.                 }
  100.         }
  101.        
  102.         //Debugging tools      
  103.         public function debug($tool = NULL) {
  104.                 switch($tool) {
  105.                         case "clear":
  106.                                 $this->clearCache();
  107.                                 break;
  108.                         case "size":
  109.                                 print $this->getTotalCacheSize();
  110.                                 break;
  111.                         case "registry":
  112.                                 return $this->getRegistry();
  113.                         case "all":
  114.                                 foreach($this->getRegistry() as $handle) {
  115.                                         $array[$handle] = $this->get($handle);
  116.                                 }
  117.                                 return $array;
  118.                         case "random":
  119.                                 $array = $this->getRegistry();
  120.                                 $return->id = array_rand($array);
  121.                                 $return->value = $this->get($array[$return->id]);
  122.                                 return $return;
  123.                 }
  124.         }
  125.         private function clearCache() {
  126.                 $registry = $this->getRegistry();
  127.                 foreach($registry as $key => $handle) {
  128.                         $this->clear($handle);
  129.                 }
  130.                 $this->deleteSMB(1);
  131.         }
  132.         private function getCacheSize($key) {
  133.                 return shmop_size(shmop_open($key, "a", 0, 0));
  134.         }
  135.         private function getTotalCacheSize() {
  136.                 $si = ' KMGTPEZY';
  137.                 $bytes = 0;
  138.                 foreach($this->getRegistry() as $key => $handle) {
  139.                         $bytes += $this->getCacheSize($key + 2);
  140.                 }
  141.                 for($x = 0; $bytes >= 1000 && $x < (strlen($si) - 1); $bytes /= 1000, $x++);
  142.                 return round($bytes, 2) . $si{$x} . 'B';
  143.         }
  144.  
  145. }