本文整理汇总了PHP中Redis::watch方法的典型用法代码示例。如果您正苦于以下问题:PHP Redis::watch方法的具体用法?PHP Redis::watch怎么用?PHP Redis::watch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Redis
的用法示例。
在下文中一共展示了Redis::watch方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update
/**
* {@inheritdoc}
*/
public function update($key, callable $modifier, $ttl = 0)
{
$this->redis->watch($key);
$data = $this->redis->get($key);
if ($data === false) {
$this->redis->unwatch();
return false;
} else {
$modifier($data);
if ($this->setInTransaction($key, $data, (int) $ttl)) {
return $data;
}
}
throw new AtomicViolationException("Atomic violation occurred when updating key \"{$key}\".");
}
示例2: testMultiExec
public function testMultiExec()
{
$this->sequence(Redis::MULTI);
$this->differentType(Redis::MULTI);
// with prefix as well
$this->redis->setOption(Redis::OPT_PREFIX, "test:");
$this->sequence(Redis::MULTI);
$this->differentType(Redis::MULTI);
$this->redis->setOption(Redis::OPT_PREFIX, "");
$this->redis->set('x', '42');
$this->assertTrue(TRUE === $this->redis->watch('x'));
$ret = $this->redis->multi()->get('x')->exec();
// successful transaction
$this->assertTrue($ret === array('42'));
// failed transaction
$this->redis->watch('x');
$r = $this->newInstance();
// new instance, modifying `x'.
$r->incr('x');
$ret = $this->redis->multi()->get('x')->exec();
$this->assertTrue($ret === FALSE);
// failed because another client changed our watched key between WATCH and EXEC.
// watch and unwatch
$this->redis->watch('x');
$r->incr('x');
// other instance
$this->redis->unwatch();
// cancel transaction watch
$ret = $this->redis->multi()->get('x')->exec();
$this->assertTrue($ret === array('44'));
// succeeded since we've cancel the WATCH command.
}
示例3: mget
protected function mget()
{
$this->db->watch([$this->count]);
$error = $this->db->getLastError();
if ($error) {
return $error;
}
$replies = $this->db->mGet([$this->count, $this->limit, $this->reset]);
$error = $this->db->getLastError();
if ($error) {
return $error;
}
if (!$replies[0] && $replies[0] !== '0') {
return $this->create();
}
return $this->decr($replies);
}
示例4: freeze
/**
* Freezes this cache backend.
*
* All data in a frozen backend remains unchanged and methods which try to add
* or modify data result in an exception thrown. Possible expiry times of
* individual cache entries are ignored.
*
* A frozen backend can only be thawn by calling the flush() method.
*
* @throws \RuntimeException
* @return void
*/
public function freeze()
{
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1323344192);
}
do {
$entriesKey = $this->buildKey('entries');
$this->redis->watch($entriesKey);
$entries = $this->redis->lRange($entriesKey, 0, -1);
$this->redis->multi();
foreach ($entries as $entryIdentifier) {
$this->redis->persist($this->buildKey('entry:' . $entryIdentifier));
}
$this->redis->set($this->buildKey('frozen'), 1);
$result = $this->redis->exec();
} while ($result === false);
$this->frozen = true;
}
示例5: testFailedTransactions
public function testFailedTransactions()
{
$this->redis->set('x', 42);
// failed transaction
$this->redis->watch('x');
$r = $this->newInstance();
// new instance, modifying `x'.
$r->incr('x');
$ret = $this->redis->multi()->get('x')->exec();
$this->assertTrue($ret === FALSE);
// failed because another client changed our watched key between WATCH and EXEC.
// watch and unwatch
$this->redis->watch('x');
$r->incr('x');
// other instance
$this->redis->unwatch();
// cancel transaction watch
$ret = $this->redis->multi()->get('x')->exec();
$this->assertTrue($ret === array('44'));
// succeeded since we've cancel the WATCH command.
}
示例6: tryLock
protected function tryLock()
{
$this->redis->watch('lock');
$result = $this->redis->blPop(array('lock'), static::LOCK_TIMEOUT);
return !empty($result);
}
示例7:
$key = 'test-expire-key';
$redis->expire($key, 60);
//使用秒为单位
$redis->pExpire($key, 60000);
//使用毫秒作为单位
$redis->expireAt($key, 1476868380);
//使用Unix timestamp,指定时间过期
$redis->pExpireAt($key, 1476868380000.0);
//使用Unix timestamp在指定时间过期,区别是毫秒作为单位
$redis->persist($key);
//移除给定key的生存时间
$redis->ttl($key);
//返回key剩余的过期时间,使用秒为单位
$redis->pttl($key);
//返回key剩余的过期时间,使用毫秒作为单位
$redis->watch($key2);
$ret = $redis->multi(Redis::MULTI)->get($key)->incr($key1)->del($key2)->exec();
$pip->incr('a');
$pip->get('test-num2');
$pip->incr('b');
$pip->del('a');
$ret = $pip->exec();
var_dump($ret);
exit;
$ret = $redis->incr('test-num2')->incr('test-num2')->exec();
exit('aa' . $redis->get('test-num2'));
$redis->multi(Redis::PIPELINE);
for ($i = 0; $i < 10000; $i++) {
$redis->incr('test-num')->include('test-num2');
}
$redis->exec();
示例8: PDO
<?php
/*
$dsn = "mysql:host=localhost;dbname=demo";
$db = new PDO($dsn, 'root', 'root');
*/
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->watch('myk');
$vn = $redis->get('myk');
$vn = $vn + 1;
sleep(1);
$redis->multi();
$redis->set('myk', $vn);
$rs = $redis->exec();
if (!$rs) {
error_log("eeee\n", 3, 'F:/logdata/eee.log');
}
示例9: removeExpiredJobs
/**
* Remove the expired jobs from a given queue.
*
* @param \Redis $transaction
* @param string $from
* @param int $time
* @return void
*/
protected function removeExpiredJobs($transaction, $from, $time)
{
$transaction->watch($from);
$transaction->multi();
$transaction->zRemRangeByScore($from, '-inf', $time);
}