SharedMemorySegmentHandler.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: SharedMemorySegmentHandler.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class SharedMemorySegmentHandler implements SegmentHandler
00017     {
00018         const SEGMENT_SIZE = 2097152; // 2 ^ 21
00019         
00020         private $id = null;
00021         
00022         public function __construct($segmentId)
00023         {
00024             $this->id = $segmentId;
00025         }
00026         
00027         public function touch($key)
00028         {
00029             try {
00030                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
00031             } catch (BaseException $e) {
00032                 return false;
00033             }
00034 
00035             try {
00036                 $result = shm_put_var($shm, $key, true);
00037                 shm_detach($shm);
00038             } catch (BaseException $e) {
00039                 // not enough shared memory left, rotate it.
00040                 shm_detach($shm);
00041                 return $this->drop();
00042             }
00043             
00044             return $result;
00045         }
00046         
00047         public function unlink($key)
00048         {
00049             try {
00050                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
00051             } catch (BaseException $e) {
00052                 return false;
00053             }
00054             
00055             try {
00056                 $result = shm_remove_var($shm, $key);
00057                 shm_detach($shm);
00058                 return $result;
00059             } catch (BaseException $e) {
00060                 // non existent key
00061                 shm_detach($shm);
00062                 return false;
00063             }
00064             
00065             Assert::isUnreachable();
00066         }
00067         
00068         public function ping($key)
00069         {
00070             try {
00071                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
00072             } catch (BaseException $e) {
00073                 return false;
00074             }
00075             
00076             try {
00077                 $result = shm_get_var($shm, $key);
00078             } catch (BaseException $e) {
00079                 // variable key N doesn't exist, bleh
00080                 shm_detach($shm);
00081                 return false;
00082             }
00083             
00084             shm_detach($shm);
00085             
00086             return $result;
00087         }
00088         
00089         public function drop()
00090         {
00091             try {
00092                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
00093             } catch (BaseException $e) {
00094                 return false;
00095             }
00096             
00097             $result = shm_remove($shm);
00098             
00099             shm_detach($shm);
00100             
00101             return $result;
00102         }
00103     }
00104 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4