예제 #1
0
 public function testIncr()
 {
     $this->redis->set('key', 0);
     $this->redis->incr('key');
     $this->assertEquals(1, $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertEquals(2, $this->redis->get('key'));
     $this->redis->incr('key', 3);
     $this->assertEquals(5, $this->redis->get('key'));
 }
예제 #2
0
 /**
  * @see QueueInterface::push()
  */
 public function push(TaskInterface $task)
 {
     $eta = $task->getEta() ?: new \DateTime();
     $score = $eta->getTimestamp();
     $unique = $this->redis->incr('sequence');
     $member = $unique . '@' . $this->serializer->serialize($task);
     $result = $this->redis->zAdd('tasks', $score, $member);
     if (!$result) {
         throw new \RuntimeException(sprintf('Unable to push the task %s.', $task));
     }
 }
예제 #3
0
 /**
  * @param InputInterface  $input  Input
  * @param OutputInterface $output Output
  *
  * @return null
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $redisHost = $input->getOption('host');
     $redisPort = $input->getOption('port');
     $redisDb = $input->getOption('db');
     try {
         $config = Yaml::parse(file_get_contents($_SERVER['HOME'] . '/.hooks.yml'));
     } catch (\Exception $e) {
         $config = ['daemon' => ['host' => '127.0.0.1', 'port' => 6379, 'db' => 0]];
     }
     if ($redisHost) {
         $config['daemon']['host'] = $redisHost;
     }
     if ($redisPort) {
         $config['daemon']['port'] = $redisPort;
     }
     if ($redisDb) {
         $config['daemon']['db'] = $redisDb;
     }
     $redis = new \Redis();
     $redis->connect($config['daemon']['host'], $config['daemon']['port']);
     $redis->select($config['daemon']['db']);
     $version = $redis->incr('hooks.worker.version');
     $output->writeln('<info>Worker version incremented: ' . $version . '.</info>');
     return null;
 }
예제 #4
0
 /**
  * Increases the value
  *
  * @param   string $key
  * @param   int $value
  * @return  void
  */
 public function increment($key, $value = 1)
 {
     if ($value == 1) {
         $this->_redis->incr($this->_prefix . $key);
     } else {
         $this->_redis->incrBy($this->_prefix . $key, $value);
     }
 }
예제 #5
0
 /**
  * {@inheritdoc}
  */
 public function increment($key, $value = 1)
 {
     if ($value < 1) {
         throw new \InvalidArgumentException('Value of incrementation must be greater that nil.');
     }
     if ($value === 1) {
         return $this->redis->incr($key);
     }
     return $this->redis->incrBy($key, $value);
 }
예제 #6
0
파일: Wk_Redis.php 프로젝트: telander/waka
 /**
  * @param string $key
  * @return int
  */
 public function incr($key)
 {
     try {
         $this->_useCnt++;
         return parent::incr($key);
     } catch (Exception $e) {
         Wk::logger()->err($e->getMessage());
     }
     return 0;
 }
예제 #7
0
 public function testDifferentTypeHash()
 {
     $key = 'hash';
     $this->redis->del($key);
     $this->assertEquals(1, $this->redis->hSet($key, 'key', 'value'));
     // string I/F
     $this->assertEquals(FALSE, $this->redis->get($key));
     $this->assertEquals(FALSE, $this->redis->getset($key, 'value2'));
     $this->assertEquals(FALSE, $this->redis->append($key, 'append'));
     $this->assertEquals(FALSE, $this->redis->substr($key, 0, 8));
     $this->assertEquals(array(FALSE), $this->redis->mget(array($key)));
     $this->assertEquals(FALSE, $this->redis->incr($key));
     $this->assertEquals(FALSE, $this->redis->incrBy($key, 1));
     $this->assertEquals(FALSE, $this->redis->decr($key));
     $this->assertEquals(FALSE, $this->redis->decrBy($key, 1));
     // lists I/F
     $this->assertEquals(FALSE, $this->redis->rPush($key, 'lvalue'));
     $this->assertEquals(FALSE, $this->redis->lPush($key, 'lvalue'));
     $this->assertEquals(FALSE, $this->redis->lLen($key));
     $this->assertEquals(FALSE, $this->redis->lPop($key));
     $this->assertEquals(FALSE, $this->redis->lGetRange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->lTrim($key, 0, 1));
     $this->assertEquals(FALSE, $this->redis->lGet($key, 0));
     $this->assertEquals(FALSE, $this->redis->lSet($key, 0, "newValue"));
     $this->assertEquals(FALSE, $this->redis->lRemove($key, 'lvalue', 1));
     $this->assertEquals(FALSE, $this->redis->lPop($key));
     $this->assertEquals(FALSE, $this->redis->rPop($key));
     $this->assertEquals(FALSE, $this->redis->rPoplPush($key, __FUNCTION__ . 'lkey1'));
     // sets I/F
     $this->assertEquals(FALSE, $this->redis->sAdd($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sRemove($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sPop($key));
     $this->assertEquals(FALSE, $this->redis->sMove($key, __FUNCTION__ . 'skey1', 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sSize($key));
     $this->assertEquals(FALSE, $this->redis->sContains($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sInter($key, __FUNCTION__ . 'skey2'));
     $this->assertEquals(FALSE, $this->redis->sUnion($key, __FUNCTION__ . 'skey4'));
     $this->assertEquals(FALSE, $this->redis->sDiff($key, __FUNCTION__ . 'skey7'));
     $this->assertEquals(FALSE, $this->redis->sMembers($key));
     $this->assertEquals(FALSE, $this->redis->sRandMember($key));
     // sorted sets I/F
     $this->assertEquals(FALSE, $this->redis->zAdd($key, 1, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zDelete($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zIncrBy($key, 1, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRank($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRevRank($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zReverseRange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zRangeByScore($key, 1, 2));
     $this->assertEquals(FALSE, $this->redis->zCount($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zCard($key));
     $this->assertEquals(FALSE, $this->redis->zScore($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zDeleteRangeByRank($key, 1, 2));
     $this->assertEquals(FALSE, $this->redis->zDeleteRangeByScore($key, 1, 2));
 }
예제 #8
0
 public function testGetLastError()
 {
     // We shouldn't have any errors now
     $this->assertTrue($this->redis->getLastError() === NULL);
     // test getLastError with a regular command
     $this->redis->set('x', 'a');
     $this->assertFalse($this->redis->incr('x'));
     $incrError = $this->redis->getLastError();
     $this->assertTrue(strlen($incrError) > 0);
     // clear error
     $this->redis->clearLastError();
     $this->assertTrue($this->redis->getLastError() === NULL);
 }
예제 #9
0
파일: Redis.php 프로젝트: ehough/stash
 /**
  * {@inheritdoc}
  */
 public function clear($key = null)
 {
     if (is_null($key)) {
         $this->redis->flushDB();
         return true;
     }
     $keyString = $this->makeKeyString($key, true);
     $keyReal = $this->makeKeyString($key);
     $this->redis->incr($keyString);
     // increment index for children items
     $this->redis->delete($keyReal);
     // remove direct item.
     $this->keyCache = array();
     return true;
 }
예제 #10
0
 public function testIncr()
 {
     $this->redis->set('key', 0);
     $this->redis->incr('key');
     $this->assertEquals(1, $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertEquals(2, $this->redis->get('key'));
     $this->redis->incr('key', 3);
     $this->assertEquals(5, $this->redis->get('key'));
     $this->redis->delete('key');
     $this->redis->set('key', 'abc');
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
 }
예제 #11
0
 /**
  * 双重缓存,防止击穿 (如果key没有被初始化,仍有可能会导致击穿现象)
  * @param int  $key    Redis key
  * @return Mix
  */
 public function getByLock($key)
 {
     $sth = $this->redis->get($key);
     if ($sth === false) {
         return $sth;
     } else {
         $sth = json_decode($sth, true);
         if (intval($sth['expire']) <= time()) {
             $lock = $this->redis->incr($key . ".lock");
             if ($lock === 1) {
                 return false;
             }
             return $sth['data'];
         } else {
             return $sth['data'];
         }
     }
 }
/**
 * Places an email into our email queue, to be sent later (normally within a second or so).
 * @param array $email
 * @param string $priority an unsigned integer as a string, between 0 and 10. Lower has higher priority, so 0 is highest priority.
 */
function queue_email($email, $priority = '9')
{
    $redis = new Redis();
    $host = '127.0.0.1';
    $port = 6379;
    $redis_connected = $redis->pconnect($host, $port);
    echo "Redis Connected: {$redis_connected}\n";
    if ($redis_connected) {
        $email_job = [];
        $email_job['priority.created'] = $priority . '.' . time();
        $email_job['data'] = json_encode($email);
        // Increase job index
        $email_job_index = $redis->incr('mail_sender:email_job_index');
        $email_job['job_id'] = $email_job_index;
        var_dump($email_job);
        // Insert job as an atomic transaction
        $redis->multi()->hMset("mail_sender:email_job:{$email_job_index}", $email_job)->zAdd("mail_sender:email_jobs", 0, $email_job_index)->exec();
    }
}
예제 #13
0
 public function testGetLastError()
 {
     // We shouldn't have any errors now
     $this->assertTrue($this->redis->getLastError() === NULL);
     // Throw some invalid lua at redis
     $this->redis->eval("not-a-lua-script");
     // Now we should have an error
     $evalError = $this->redis->getLastError();
     $this->assertTrue(strlen($evalError) > 0);
     // test getLastError with a regular command
     $this->redis->set('x', 'a');
     $this->assertFalse($this->redis->incr('x'));
     $incrError = $this->redis->getLastError();
     $this->assertTrue($incrError !== $evalError);
     // error has changed
     $this->assertTrue(strlen($incrError) > 0);
     // clear error
     $this->redis->clearLastError();
     $this->assertTrue($this->redis->getLastError() === NULL);
 }
예제 #14
0
 public function testIncr()
 {
     $this->redis->set('key', 0);
     $this->redis->incr('key');
     $this->assertEquals(1, (int) $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertEquals(2, (int) $this->redis->get('key'));
     $this->redis->incr('key', 3);
     $this->assertEquals(5, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', 3);
     $this->assertEquals(8, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', 1);
     $this->assertEquals(9, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', -1);
     $this->assertEquals(8, (int) $this->redis->get('key'));
     $this->redis->delete('key');
     $this->redis->set('key', 'abc');
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
 }
예제 #15
0
파일: TestRedis.php 프로젝트: 0/phpredis
 public function testIncr()
 {
     $this->redis->set('key', 0);
     $this->redis->incr('key');
     $this->assertEquals(1, (int) $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertEquals(2, (int) $this->redis->get('key'));
     $this->redis->incr('key', 3);
     $this->assertEquals(5, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', 3);
     $this->assertEquals(8, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', 1);
     $this->assertEquals(9, (int) $this->redis->get('key'));
     $this->redis->incrBy('key', -1);
     $this->assertEquals(8, (int) $this->redis->get('key'));
     $this->redis->delete('key');
     $this->redis->set('key', 'abc');
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
     $this->redis->incr('key');
     $this->assertTrue("abc" === $this->redis->get('key'));
     // incrbyfloat
     $this->redis->delete('key');
     $this->redis->set('key', 0);
     $this->redis->incrbyfloat('key', 1.5);
     $this->assertEquals('1.5', $this->redis->get('key'));
     $this->redis->incrbyfloat('key', 2.25);
     $this->assertEquals('3.75', $this->redis->get('key'));
     $this->redis->incrbyfloat('key', -2.25);
     $this->assertEquals('1.5', $this->redis->get('key'));
     $this->redis->set('key', 'abc');
     $this->redis->incrbyfloat('key', 1.5);
     $this->assertTrue("abc" === $this->redis->get('key'));
     $this->redis->incrbyfloat('key', -1.5);
     $this->assertTrue("abc" === $this->redis->get('key'));
 }
예제 #16
0
<?php

if (extension_loaded('redis')) {
    $redis = new Redis();
    $redis->connect('localhost');
    $redis->incr('holidayapi:requests:alltime');
} else {
    $redis = false;
}
require '../lib/HolidayAPIv1.php';
$api = new \HolidayAPI\v1($redis);
header('Content-Type: application/json; charset=utf-8');
$flags = JSON_UNESCAPED_UNICODE;
if (isset($_REQUEST['pretty'])) {
    $flags |= JSON_PRETTY_PRINT;
}
echo json_encode($api->getHolidays(), $flags);
if ($redis) {
    $redis->close();
}
예제 #17
0
파일: test1.php 프로젝트: songliang89/my-ci
<?php

/**
 * Created by linzl
 * User: linzl<*****@*****.**>
 * Date: 15/7/21
 * Time: 下午3:00
 */
$redis = new Redis();
$redis->connect("127.0.0.1", "6379");
$vote_num = $redis->get("my_vote");
$flag = $redis->incr("my_vote");
// 例如204状态码,当浏览器收到204时,页面不跳转。
header("HTTP/1.1 204 No Content");
예제 #18
0
 /**
  * Increment a raw value
  *
  * @param    string $id Cache ID
  * @param    int $offset Step/value to add
  * @return    mixed    New value on success or FALSE on failure
  */
 public function increment($id, $offset = 1)
 {
     return $this->_redis->exists($id) ? $this->_redis->incr($id, $offset) : FALSE;
 }
예제 #19
0
<?php

$redis = new Redis();
$r = $redis->connect('10.240.0.2', 6379, 1);
if ($r) {
    $key = $redis->incr('key');
    $redis->close();
    var_dump($key);
} else {
    header("HTTP/1.0 502 sys busy");
}
예제 #20
0
파일: Gateway.php 프로젝트: medz/thinksns-4
 public function increment($key)
 {
     return parent::incr($key);
 }
예제 #21
0
<?php

$redis_url_str = $_ENV["REDIS_URL"] ? $_ENV["REDIS_URL"] : "redis://localhost:6379";
$redis_url = parse_url($redis_url_str);
$redis = new Redis();
$redis->connect($redis_url['host'], $redis_url['port']);
$redis->auth($redis_url['pass']);
$redis->incr('counter');
$counter = $redis->get('counter') + "";
echo "Counter has been incremented to " . $counter . "\n";
예제 #22
0
파일: index.php 프로젝트: huycao/yodelivery
<?php

header('Content-Type: image/gif');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
define('REDIS_HOST', '192.168.100.17');
define('REDIS_PORT_6', 6395);
define('EXPIRE_TIME', 15);
$countList = array('ad_request', 'ad_impression', 'ad_click');
if (isset($_REQUEST['count'])) {
    $key = $_REQUEST['count'];
    if (in_array($key, $countList)) {
        $redis = new Redis();
        $redis->connect(REDIS_HOST, REDIS_PORT_6);
        $counter = $redis->get($key);
        $redis->incr($key);
    }
}
echo base64_decode("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==");
예제 #23
0
파일: gateway.php 프로젝트: eastlhu/fooking
     foreach ($keys as $k) {
         $data = $redis->hgetAll($k);
         if ($k == $key) {
             continue;
         }
         $data['type'] = 'join';
         $client->sendMsg($sid, json_encode($data));
         echo "sync-->" . $data['id'] . "\n";
     }
     //send all
     $client->sendAllMsg(json_encode($msg));
 } else {
     if ($msg['type'] == 'popo') {
         $redis = new Redis();
         $redis->connect('localhost', 6379);
         $popid = $redis->incr('popoid');
         $msg['id'] = 'p' . $popid;
         $client = new RouterClient('127.0.0.1', 9010);
         $client->sendAllMsg(json_encode($msg));
     } else {
         if ($msg['type'] == 'boom') {
             $client = new RouterClient('127.0.0.1', 9010);
             $client->sendAllMsg($data);
         } else {
             if ($msg['type'] == 'message') {
                 //$msg['text'] = mb_substr($msg['text'], 0, 10);
                 $client = new RouterClient('127.0.0.1', 9010);
                 $client->sendAllMsg($data);
             } else {
                 if ($msg['type'] == 'kill') {
                     $redis = new Redis();
예제 #24
0
파일: redis.php 프로젝트: xiehaowei/php
$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();
exit;
$it = null;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while ($ret = $redis->scan($it, 'test*', 3)) {
    foreach ($ret as $item) {
        echo $item . PHP_EOL;
    }
}
exit;
예제 #25
0
<?php

/**
 * Fill db mockup values
 */
$redis = new Redis();
$redis->connect('127.0.0.1');
$redis->select(2);
// HSET
for ($i = 0; $i <= rand(1000, 10000); $i++) {
    $incr = $redis->incr('test:incr:hset');
    $data = array();
    for ($j = 0; $j <= rand(10, 500); $j++) {
        $data['field' . $j] = md5($j);
    }
    $redis->hMset('test:' . $incr . ':hset', $data);
}
// SET
for ($i = 0; $i <= rand(1000, 10000); $i++) {
    $incr = $redis->incr('test:incr:set');
    for ($j = 0; $j <= rand(10, 100); $j++) {
        $redis->sAdd('test:' . $incr . ':set', $j);
    }
}
// LIST
for ($i = 0; $i <= rand(1000, 10000); $i++) {
    $incr = $redis->incr('test:incr:list');
    for ($j = 0; $j <= rand(10, 100); $j++) {
        $redis->rPush('test:' . $incr . ':list', $j);
    }
}
예제 #26
0
 function subscribe_run()
 {
     $this->load->config('redis');
     $redis_config = $this->config->item('redis');
     $redis = new Redis();
     $redis->pconnect($redis_config['write']['hostname'], $redis_config['write']['port']);
     while (true) {
         try {
             $data = $redis->lPop(Config::$subscribe_queue_redis_key);
             if ($data) {
                 $redis->incr("dy_lzl_success");
                 $path = __DIR__;
                 $app_path = FCPATH;
                 $cmd = "/bin/sh " . $path . "/push.sh '{$app_path}' '{$data}' ";
                 $redis->set("cmd", $cmd);
                 @system($cmd, $return);
             }
         } catch (Exception $e) {
             $redis->incr("dy_lzl_error");
         }
     }
 }
예제 #27
0
 /**
  * Increment a raw value
  *
  * @param	string	$id	Cache ID
  * @param	int	$offset	Step/value to add
  * @return	mixed	New value on success or FALSE on failure
  */
 public function increment($id, $offset = 1)
 {
     return $this->_redis->incr($id, $offset);
 }
예제 #28
0
 public function incr($key)
 {
     return $this->connection->incr($key);
 }
예제 #29
0
 /**
  * Increments the group value to simulate deletion of all keys under a group
  * old values will remain in storage until they expire.
  *
  * @param string $group The group name to clear.
  * @return bool success
  */
 public function clearGroup($group)
 {
     return (bool) $this->_Redis->incr($this->settings['prefix'] . $group);
 }
예제 #30
0
파일: time.php 프로젝트: ZSShang/mylearn
<?php

//$time = microtime(1) - 3600 * 24;
//echo date("Y-m-d H:i:s",intval($time))."\n";
//echo $time."\n";
//$new = round($time,3) * 1000;
//echo $new."\n";
//echo strtotime(date("Y-m-d",time()))."\n";
$redis = new Redis();
$redis->connect("127.0.0.1", "6379");
$redis->select(4);
$redis->psetex("secondTimes", 1500, 1);
var_dump($redis->get("secondTimes"));
if ($redis->get("secondTimes")) {
    for ($i = 0; $i < 10; $i++) {
        $redis->incr("secondTimes");
    }
    sleep(1);
    var_dump($redis->get("secondTimes"));
    if ($redis->get("secondTimes") > 10) {
        exit("超出限制!");
    } else {
        echo "发送请求!";
    }
} else {
    $redis->setex("secondTimes", 1, 0);
}