# SharedMemorySegmentHandler.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2006-2008 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
    00015     final class SharedMemorySegmentHandler implements SegmentHandler
    00016     {
    00017         const SEGMENT_SIZE = 2097152; // 2 ^ 21
    00018
    00019         private $id = null;
    00020
    00021         public function __construct($segmentId)
    00022         {
    00023             $this->id = $segmentId;
    00024         }
    00025
    00026         public function touch($key)
    00027         {
    00028             try {
    00029                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
    00030             } catch (BaseException $e) {
    00031                 return false;
    00032             }
    00033
    00034             try {
    00035                 $result = shm_put_var($shm, $key, true);
    00036                 shm_detach($shm);
    00037             } catch (BaseException $e) {
    00038                 // not enough shared memory left, rotate it.
    00039                 shm_detach($shm);
    00040                 return $this->drop();
    00041             }
    00042
    00043             return $result;
    00044         }
    00045
    00046         public function unlink($key)
    00047         {
    00048             try {
    00049                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
    00050             } catch (BaseException $e) {
    00051                 return false;
    00052             }
    00053
    00054             try {
    00055                 $result = shm_remove_var($shm, $key);
    00056             } catch (BaseException $e) {
    00057                 // non existent key
    00058                 $result = false;
    00059             }
    00060
    00061             shm_detach($shm);
    00062
    00063             return $result;
    00064         }
    00065
    00066         public function ping($key)
    00067         {
    00068             try {
    00069                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
    00070             } catch (BaseException $e) {
    00071                 return false;
    00072             }
    00073
    00074             try {
    00075                 $result = shm_get_var($shm, $key);
    00076             } catch (BaseException $e) {
    00077                 // variable key N doesn't exist, bleh
    00078                 $result = false;
    00079             }
    00080
    00081             shm_detach($shm);
    00082
    00083             return $result;
    00084         }
    00085
    00086         public function drop()
    00087         {
    00088             try {
    00089                 $shm = shm_attach($this->id, self::SEGMENT_SIZE, ONPHP_IPC_PERMS);
    00090             } catch (BaseException $e) {
    00091                 return false;
    00092             }
    00093
    00094             $result = shm_remove($shm);
    00095
    00096             shm_detach($shm);
    00097
    00098             return $result;
    00099         }
    00100     }
    00101 ?>