Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function acquireLock($lockId)
 {
     if ($this->conn->setnx($lockId, 1)) {
         $this->conn->expire($lockId, $this->lockTtl);
         return true;
     }
     return false;
 }
 /**
  * @param string $sessionId
  */
 private function lockSession($sessionId)
 {
     $attempts = 1000000 / $this->spinLockWait * $this->lockMaxWait;
     $this->lockKey = $sessionId . '.lock';
     for ($i = 0; $i < $attempts; $i++) {
         $success = $this->redis->setnx($this->prefix . $this->lockKey, '1');
         if ($success) {
             $this->locked = true;
             $this->redis->expire($this->prefix . $this->lockKey, $this->lockMaxWait + 1);
             return true;
         }
         usleep($this->spinLockWait);
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 protected function getLock($name, $blocking)
 {
     $content = serialize($this->getLockInformation());
     if ($this->expiration > 0) {
         if (!$this->redis->setex($name, $this->expiration, $content)) {
             return false;
         }
         $this->ttl[$name] = $this->expiration;
     } else {
         if (!$this->redis->setnx($name, $content)) {
             return false;
         }
         unset($this->ttl[$name]);
     }
     return true;
 }
Exemplo n.º 4
0
 private function getLock($expireMs)
 {
     // $microtime = sprintf('%d', microtime(true)*1000);
     try {
         $microtime = time() * 1000;
         $locktime = $microtime + $expireMs;
         $lock = $this->rd->setnx($this->k, $locktime);
         if (!$lock) {
             $lockV = $this->rd->getSet($this->k, $locktime);
             if ($lockV <= $microtime) {
                 $lock = 1;
                 $this->rd->pexpireAt($this->k, $locktime);
                 // 设置过期时间
             }
         }
         return $lock;
     } catch (\RedisException $e) {
         if ($this->reconnRedis()) {
             $microtime = time() * 1000;
             $locktime = $microtime + $expireMs;
             $lock = $this->rd->setnx($this->k, $locktime);
             if (!$lock) {
                 $lockV = $this->rd->getSet($this->k, $locktime);
                 if ($lockV <= $microtime) {
                     $lock = 1;
                     $this->rd->pexpireAt($this->k, $locktime);
                     // 设置过期时间
                 }
             }
             return $lock;
         }
     }
     return FALSE;
 }
Exemplo n.º 5
0
 protected function addValue($key, $value, $expires = 0)
 {
     $r = $this->redis->setnx($key, $value);
     if ($r && $expires) {
         $this->redis->setTimeout($key, $expires);
     }
     return $r;
 }
Exemplo n.º 6
0
 public function testSetNX()
 {
     $this->redis->set('key', 42);
     $this->assertTrue($this->redis->setnx('key', 'err') === FALSE);
     $this->assertTrue($this->redis->get('key') === '42');
     $this->redis->delete('key');
     $this->assertTrue($this->redis->setnx('key', '42') === TRUE);
     $this->assertTrue($this->redis->get('key') === '42');
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function add($key, $data, $ttl = 0)
 {
     if ($ttl > 0) {
         $uid = uniqid('temp_key', true);
         $result = $this->multi($key)->setex($uid, (int) $ttl, $data)->renameNx($uid, $key)->delete($uid)->exec();
         return !empty($result[1]);
     }
     return $this->redis->setnx($key, $data);
 }
 /**
  * Write data for key into cache if it doesn't exist already.
  * If it already exists, it fails and returns false.
  *
  * @param string $key Identifier for the data.
  * @param mixed $value Data to be cached.
  * @param int $duration How long to cache the data, in seconds.
  * @return bool True if the data was successfully cached, false on failure.
  * @link https://github.com/phpredis/phpredis#setnx
  */
 public function add($key, $value, $duration)
 {
     if (!is_int($value)) {
         $value = serialize($value);
     }
     $result = $this->_Redis->setnx($key, $value);
     // setnx() doesn't have an expiry option, so overwrite the key with one
     if ($result) {
         return $this->_Redis->setex($key, $duration, $value);
     }
     return false;
 }
Exemplo n.º 9
0
 /**
  * Write data for key into cache if it doesn't exist already.
  * If it already exists, it fails and returns false.
  *
  * @param string $key Identifier for the data.
  * @param mixed $value Data to be cached.
  * @return bool True if the data was successfully cached, false on failure.
  * @link https://github.com/phpredis/phpredis#setnx
  */
 public function add($key, $value)
 {
     $duration = $this->_config['duration'];
     $key = $this->_key($key);
     if (!is_int($value)) {
         $value = serialize($value);
     }
     // setnx() doesn't have an expiry option, so overwrite the key with one
     if ($this->_Redis->setnx($key, $value)) {
         return $this->_Redis->setex($key, $duration, $value);
     }
     return false;
 }
Exemplo n.º 10
0
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//锁配置
$lock_timeout = 0.1;
//超时时间
$lock_flag = 0;
//是否获取到锁
$lock_key = 'lock.foo';
//锁的key值
//获取锁
while ($lock_flag != 1) {
    $now = time() + microtime(true);
    //当前时间戳
    $lock_time = $now + $lock_timeout;
    //设置lock.foo对应的值
    $lock_flag = $redis->setnx($lock_key, $lock_time);
    //获取锁
    //获取lock.foo对应的值
    $gettime = $redis->get($lock_key);
    //判断是否获得锁
    //setnx('key','value')返回true就获得锁了或者,如果超时了,time()>getSet(key,value)那么此线程也获得锁了
    if ($lock_flag or $now > $gettime and $now > $redis->getSet($lock_key, $lock_time)) {
        $lock_flag = 1;
    } else {
        sleep(1);
    }
}
if ($lock_flag) {
    $rs = $db->query("select * from ab_table limit 0,1");
    $rs->setFetchMode(PDO::FETCH_ASSOC);
    $row = $rs->fetch();
Exemplo n.º 11
0
 /**
  * @param string $key
  * @param string $value
  * @return bool
  */
 public function setnx($key, $value)
 {
     $this->_useCnt++;
     return $this->_wrapBoolReturn(function () use($key, $value) {
         return parent::setnx($key, $value);
     });
 }
Exemplo n.º 12
0
 /**
  * 添加一个新的缓存
  *
  * 如果这个key已经存在返回FALSE
  *
  * @param string $key 缓存键
  * @param mix $value 缓存值
  * @param int $expire 过期时间,0永不过期
  */
 public function addnx($key, $value, $expire = 0)
 {
     return $this->connect->setnx($key, $value, $expire);
 }
Exemplo n.º 13
0
$redis->info("COMMANDSTATS");
$redis->ifno("CPU");
$redis->lastSave();
$redis->resetStat();
$redis->save();
$redis->slaveof('10.0.1.7', 6379);
$redis->slowlog('get', 10);
$redis->slowlog('reset');
$redis->get('key');
$redis->get('key');
$redis->set('key', 'value', 10);
$redis->set('key', 'value', array('nx', 'ex' => 10));
$redis->set('key', 'value', array('xx', 'px' => 1000));
$redis->setex('key', 3600, 'value');
$redis->psetex('key', 100, 'value');
$redis->setnx('key', 'value');
$redis->set('key1', 'val1');
$redis->set('key2', 'val2');
$redis->set('key3', 'val3');
$redis->set('key4', 'val4');
$redis->delete('key1', 'key2');
/* return 2 */
$redis->delete(array('key3', 'key4'));
/* return 2 */
$redis->set('key', 'value');
$redis->exists('NonExistingKey');
$redis->incr('key1');
/* 4 */
$redis->incrBy('key1', 10);
/* 14 */
$redis->decr('key1');
Exemplo n.º 14
0
 /**
  * Tries to acquire a lock with a given name.
  *
  * @see cache_is_lockable
  * @param string $key Name of the lock to acquire.
  * @param string $ownerid Information to identify owner of lock if acquired.
  * @return bool True if the lock was acquired, false if it was not.
  */
 public function acquire_lock($key, $ownerid)
 {
     return $this->redis->setnx($key, $ownerid);
 }