00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class SharedMemorySegmentHandler implements SegmentHandler
00017 {
00018 const SEGMENT_SIZE = 2097152;
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
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
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
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 ?>