Example #1
0
 /**
  * {@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}\".");
 }
Example #2
0
 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.
 }
Example #3
0
 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);
 }
 /**
  * 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;
 }
Example #5
0
 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.
 }
Example #6
0
 protected function tryLock()
 {
     $this->redis->watch('lock');
     $result = $this->redis->blPop(array('lock'), static::LOCK_TIMEOUT);
     return !empty($result);
 }
Example #7
0
$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();
Example #8
0
<?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');
}
Example #9
0
 /**
  * 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);
 }