本文整理汇总了PHP中Redis::setnx方法的典型用法代码示例。如果您正苦于以下问题:PHP Redis::setnx方法的具体用法?PHP Redis::setnx怎么用?PHP Redis::setnx使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Redis
的用法示例。
在下文中一共展示了Redis::setnx方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acquireLock
/**
* {@inheritdoc}
*/
public function acquireLock($lockId)
{
if ($this->conn->setnx($lockId, 1)) {
$this->conn->expire($lockId, $this->lockTtl);
return true;
}
return false;
}
示例2: lockSession
/**
* @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;
}
示例3: getLock
/**
* {@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;
}
示例4: getLock
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;
}
示例5: addValue
protected function addValue($key, $value, $expires = 0)
{
$r = $this->redis->setnx($key, $value);
if ($r && $expires) {
$this->redis->setTimeout($key, $expires);
}
return $r;
}
示例6: testSetNX
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');
}
示例7: add
/**
* {@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);
}
示例8: add
/**
* 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;
}
示例9: add
/**
* 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;
}
示例10: Redis
$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();
示例11: setnx
/**
* @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);
});
}
示例12: addnx
/**
* 添加一个新的缓存
*
* 如果这个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);
}
示例13: array
$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');
示例14: acquire_lock
/**
* 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);
}