/**
  * @param integer $key_
  *
  * @return mixed
  */
 public function get($key_)
 {
     if (false === shm_has_var($this->m_segment, $key_)) {
         return null;
     }
     return shm_get_var($this->m_segment, $key_);
 }
Ejemplo n.º 2
0
 /**
  * Fetches an entry from the cache.
  *
  * @param string $id cache id The id of the cache entry to fetch.
  * @return mixed The cached data or FALSE, if no cache entry exists for the given id.
  */
 public function fetch($id)
 {
     if ($this->contains($id)) {
         return shm_get_var($this->shmId, $this->forgeKey($id));
     }
     return false;
 }
 public function read($key)
 {
     if ($this->shmId) {
         return @shm_get_var($this->shmId, $key);
     }
     return false;
 }
Ejemplo n.º 4
0
 /**
  * @return IdValue
  */
 public function generate()
 {
     $timestamp = $this->generateTimestamp();
     // Acquire semaphore
     $semaphore = sem_get($this->semaphoreId);
     sem_acquire($semaphore);
     // Attach shared memory
     $memory = shm_attach(self::SHM_KEY);
     $sequence = 0;
     if (!is_null($this->lastTimestamp) && $timestamp->equals($this->lastTimestamp)) {
         // Get
         $sequence = shm_get_var($memory, self::SHM_SEQUENCE) + 1 & $this->config->getSequenceMask();
         // Increment sequence
         shm_put_var($memory, self::SHM_SEQUENCE, $sequence);
         if ($sequence === 0) {
             usleep(1000);
             $timestamp = $this->generateTimestamp();
         }
     } else {
         // Reset sequence if timestamp is different from last one.
         $sequence = 0;
         shm_put_var($memory, self::SHM_SEQUENCE, $sequence);
     }
     // Detach shared memory
     shm_detach($memory);
     // Release semaphore
     sem_release($semaphore);
     // Update lastTimestamp
     $this->lastTimestamp = $timestamp;
     return new IdValue($timestamp, $this->regionId, $this->serverId, $sequence, $this->calculate($timestamp, $this->regionId, $this->serverId, $sequence));
 }
Ejemplo n.º 5
0
 /**
  * Private helper function
  *
  * @return  var data
  */
 protected function _get()
 {
     $h = shm_attach($this->spot);
     $data = shm_get_var($h, $this->name);
     shm_detach($h);
     return is_array($data) ? $data : false;
 }
Ejemplo n.º 6
0
 public function set($key, $value, $no_cas = false)
 {
     if (!$this->isOpen()) {
         $this->open();
     }
     $this->enterCriticalSection($this->ipckey);
     $this->debug("SHM set: {$key} = {$value}");
     $key = strtolower($key);
     $idx = $this->props[$key];
     if (!$no_cas && shm_has_var($this->shm, $idx) && !empty($this->hashes[$key])) {
         $var = shm_get_var($this->shm, $idx);
         $check = md5($var);
         if ($this->hashes[$key] == $check) {
             $this->debug("CAS check: Key not modified: {$key}");
             shm_put_var($this->shm, $idx, $value);
             $ok = true;
         } else {
             $this->debug("CAS check: Key modified, write blocked: {$key}");
             $ok = false;
         }
     } else {
         $this->debug("CAS check: Check disabled for set: {$key}");
         $ok = true;
         shm_put_var($this->shm, $idx, $value);
     }
     if ($ok) {
         $hash = md5($value);
         $this->hashes[$key] = $hash;
         $this->debug("CAS hash for {$key} is now {$hash}");
     }
     $this->leaveCriticalSection();
     return $ok;
 }
Ejemplo n.º 7
0
 public static function retrieve($key)
 {
     self::getHandle();
     $data = shm_get_var(self::$handle, self::getVarKey($key));
     self::release();
     return $data;
 }
Ejemplo n.º 8
0
/**
 * 通过本机共享内存件来生成一个auto_increment序列
 *
 * 序列类似MySQL的auto_increment
 *
 * @access private
 * @param  void
 * @return mixed
 */
function getNextValueByShareMemory()
{
    $addr = '127.0.0.1';
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $addr = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (!empty($_SERVER['SERVER_ADDR'])) {
        $addr = $_SERVER['SERVER_ADDR'];
    }
    $skey = 'global_serial_generator_seed_' . $addr;
    $ikey = crc32($skey);
    $sem = $shm = null;
    $retry_times = 1;
    do {
        $sem = sem_get($ikey, 1, 0777);
        $shm = shm_attach($ikey, 128, 0777);
        if (is_resource($sem) && is_resource($shm)) {
            break;
        }
        $cmd = "ipcrm -M 0x00000000; ipcrm -S 0x00000000; ipcrm -M {$ikey} ; ipcrm -S {$ikey}";
        $last_line = exec($cmd, $output, $retval);
    } while ($retry_times-- > 0);
    if (!sem_acquire($sem)) {
        return false;
    }
    $next_value = false;
    if (shm_has_var($shm, $ikey)) {
        shm_put_var($shm, $ikey, $next_value = shm_get_var($shm, $ikey) + 1);
    } else {
        shm_put_var($shm, $ikey, $next_value = 1);
    }
    $shm && shm_detach($shm);
    $sem && sem_release($sem);
    return $next_value;
}
Ejemplo n.º 9
0
function Map2GE($x, $y)
{
    if (0) {
        $SHM_KEY = ftok(__FILE__, chr(4));
        $data = shm_attach($SHM_KEY, 102400, 0666);
        $result = shm_get_var($data, 1);
        $r = $result[$x][$y];
        if (isset($r)) {
            shm_detach($data);
            return $r;
        }
    }
    // ­×¥¿
    $r = t67to97($x, $y);
    $x = $r[0];
    $y = $r[1];
    $proj = "proj -I +proj=tmerc +ellps=aust_SA +lon_0=121 +x_0=250000 +k=0.9999";
    $ret = shell_exec("echo {$x} {$y} | {$proj}");
    if (preg_match("/(\\d+)d(\\d+)'([\\d.]+)\"E\\s+(\\d+)d(\\d+)'([\\d.]+)\"N/", $ret, $matches)) {
        list($junk, $ed, $em, $es, $nd, $nm, $ns) = $matches;
        $r[0] = $ed + $em / 60 + $es / 3600;
        $r[1] = $nd + $nm / 60 + $ns / 3600;
        if (0) {
            $result[$x][$y] = $r;
            shm_put_var($data, 1, $result);
            shm_detach($data);
        }
        return $r;
    }
    return FALSE;
    // exit;
}
Ejemplo n.º 10
0
 public function DUMP()
 {
     if (!$this->ok) {
         return array();
     }
     $data = @shm_get_var($this->shmid, $this->index);
     return $data;
 }
Ejemplo n.º 11
0
 public function __get($k)
 {
     $key = crc32($k);
     if (!shm_has_var($this->sharedMemoryId, $key)) {
         return null;
     }
     return shm_get_var($this->sharedMemoryId, $key);
 }
Ejemplo n.º 12
0
 public function __get($var)
 {
     if (shm_has_var($this->fsh, $var)) {
         return shm_get_var($this->fsh, $var);
     } else {
         return null;
     }
 }
Ejemplo n.º 13
0
 /**
  * Execution commands.
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (is_null(Config::$sharedId) == false && @shm_has_var(Config::$sharedId, Config::STATUS)) {
         $output->writeln('<info>' . shm_get_var(Config::$sharedId, Config::STATUS) . '</info>');
     } else {
         $output->writeln('<comment>' . Config::NAME . ' not running</comment>');
     }
 }
Ejemplo n.º 14
0
 /**
  * @param $key
  * @param int $size
  * @return mixed
  */
 public function get($key, $size = 10000)
 {
     $result = $this->attach($key, $size);
     sem_acquire($result['mutex']);
     $value = @shm_get_var($result['shm'], $key);
     sem_release($result['mutex']);
     return $value;
 }
Ejemplo n.º 15
0
 public function __get($name)
 {
     $name = $this->intkey($name);
     if (shm_has_var($this->ipc, $name)) {
         return shm_get_var($this->ipc, $name);
     }
     return NULL;
 }
Ejemplo n.º 16
0
Archivo: Shm.php Proyecto: millken/ypf
 public function get($key)
 {
     if ($this->has($key)) {
         return shm_get_var($this->shm, $this->shm_key($key));
     } else {
         return false;
     }
 }
Ejemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function get()
 {
     if (shm_has_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID)) {
         $data = shm_get_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID);
         shm_remove_var($this->shared_memory_segment, self::$SEGMENT_VAR_ID);
         return $data;
     }
 }
Ejemplo n.º 18
0
 /**
  * Gets the value of the semaphore
  * @author Yohann Marillet
  */
 public function get($key = 1)
 {
     $return = false;
     if ($this->has($key, true)) {
         $return = shm_get_var($this->segment, $key)[0];
     }
     return $return;
 }
Ejemplo n.º 19
0
 /**
  * Execution commands.
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (is_null(Config::$sharedId) == false && @shm_has_var(Config::$sharedId, Config::PID)) {
         $process = new Process('ps o pid --ppid ' . shm_get_var(Config::$sharedId, Config::PID));
         $process->run();
     } else {
         $output->writeln('<comment>' . Config::NAME . ' not running</comment>');
     }
 }
Ejemplo n.º 20
0
 function containsKey($key)
 {
     if (!is_numeric($key)) {
         throw new Scalr_System_Exception(sprintf("key must be numeric. '%s' is given", $key));
     }
     // `shm_get_var` generates PHP warning if key doesn't exists
     $value = @shm_get_var($this->shm, $key);
     return $value !== false;
 }
Ejemplo n.º 21
0
 public function set($index, $value, $overwrite = false)
 {
     if ($overwrite || !shm_has_var($this->shm, $index) || $this->data[$index] == shm_get_var($this->shm, $index)) {
         shm_put_var($this->shm, $index, $value);
         return true;
     } else {
         return false;
     }
 }
Ejemplo n.º 22
0
 public function get(string $key)
 {
     $key = crc32($key);
     if (shm_has_var($this->_shmid, $key)) {
         return shm_get_var($this->_shmid, $key);
     } else {
         return null;
     }
 }
Ejemplo n.º 23
0
 public function run()
 {
     $this->synchronized(function ($thread) {
         $thread->wait();
     }, $this);
     $counter = shm_get_var($this->shmid, 1);
     $counter++;
     shm_put_var($this->shmid, 1, $counter);
     printf("Thread #%lu says: %s\n", $this->getThreadId(), $counter);
 }
Ejemplo n.º 24
0
 /**
  * Read shared memory var
  * @return mixed|null
  */
 public function get()
 {
     sem_acquire($this->__mutex);
     //block until released
     $var = @shm_get_var($this->__shm, $this->__key);
     //read var
     sem_release($this->__mutex);
     //release mutex
     return !empty($var) ? $var : NULL;
 }
Ejemplo n.º 25
0
 public function __construct($id, $startSize = 10240)
 {
     $this->id = $id;
     $this->res = shm_attach($id, $startSize);
     if (!shm_has_var($this->res, self::DEFAULT_VAR_ID)) {
         $this->data = array();
         $this->changed = true;
     } else {
         $this->data = shm_get_var($this->res, self::DEFAULT_VAR_ID);
     }
 }
Ejemplo n.º 26
0
 /**
  * Read datas with $uid key
  * @param mixed $uid
  * @return mixed
  */
 public function read($uid)
 {
     if (!is_int($uid) && !is_numeric($uid)) {
         throw new Exception\RuntimeException('Segment type key must integer or numeric.');
     }
     $this->alloc();
     $strShm = shm_get_var($this->memory, $uid);
     $str = trim($strShm);
     if (!$str) {
         return false;
     }
     return $str;
 }
Ejemplo n.º 27
0
 /**
  * @param int $pid
  *
  * @return int
  */
 private function readInc($pid)
 {
     $res = shm_attach($pid);
     if (!shm_has_var($res, 0)) {
         shm_put_var($res, 0, 0);
     }
     $inc = shm_get_var($res, 0);
     if ($inc === 16777215) {
         $inc = 0;
     }
     ++$inc;
     shm_put_var($res, 0, $inc);
     return $inc;
 }
 function squabble()
 {
     global $handle;
     global $buffer;
     $i = 0;
     $pid = getmypid();
     while (true) {
         if (preg_match("/^{$pid}\\b/", shm_get_var($buffer, 1))) {
             continue;
         }
         sem_acquire($handle);
         $i++;
         shm_put_var($buffer, 1, "{$pid} {$i}");
         sem_release($handle);
     }
 }
Ejemplo n.º 29
0
 public function run()
 {
     if ($this->mutex) {
         $locked = Mutex::lock($this->mutex);
     }
     $counter = shm_get_var($this->shmid, 1);
     $counter++;
     shm_put_var($this->shmid, 1, $counter);
     printf("Thread #%lu lock: %s says: %s\n", $this->getThreadId(), !empty($locked) ? "Y" : "N", $counter);
     //$this->lock();
     //$this->unlock();
     if ($this->mutex) {
         Mutex::unlock($this->mutex);
     }
     return true;
 }
 public function ping($key)
 {
     try {
         $shm = shm_attach($this->id, self::SEGMENT_SIZE, HESPER_IPC_PERMS);
     } catch (BaseException $e) {
         return false;
     }
     try {
         $result = shm_get_var($shm, $key);
     } catch (BaseException $e) {
         // variable key N doesn't exist, bleh
         $result = false;
     }
     shm_detach($shm);
     return $result;
 }