function doQueue() { global $statusArr; $pid = posix_getpid(); $statusArr[$pid]++; $redis = new Redis(); $redis->connect('127.0.0.1', '6379'); // 这个中间可以是处理逻辑 $data = "you are so beautiful!"; $key = "test:lo"; $redis->rpush($key, $data); $data = $redis->lpop($key); $redis->close(); }
/** * @param mixed $value * * @return RedisNoSQLList */ public function append($value) { $this->redis->rpush($this->key, $value); if ($this->timeout) { $this->redis->setTimeout($this->key, $this->timeout); } return $this; }
/** * Write job log * * @param string $str * @return $this */ public function log($str) { $this->emit('log', $str); $this->client->rpush('q:job:' . $this->injectors['id'] . ':log', $str); $this->set('updated_at', Util::now()); return $this; }
public function testRawCommand() { $this->redis->set('mykey', 'some-value'); $str_result = $this->redis->rawCommand('get', 'mykey'); $this->assertEquals($str_result, 'some-value'); $this->redis->del('mylist'); $this->redis->rpush('mylist', 'A', 'B', 'C', 'D'); $this->assertEquals($this->redis->lrange('mylist', 0, -1), array('A', 'B', 'C', 'D')); }
/** * {@inheritdoc} */ public function queueMessage(\Swift_Mime_Message $message) { $this->redis->rpush($this->key, serialize($message)); return true; }
public function testEval() { if (version_compare($this->version, "2.5.0", "lt")) { $this->markTestSkipped(); } // Basic single line response tests $this->assertTrue(1 == $this->redis->eval('return 1')); $this->assertTrue(1.55 == $this->redis->eval("return '1.55'")); $this->assertTrue("hello, world" == $this->redis->eval("return 'hello, world'")); /* * Keys to be incorporated into lua results */ // Make a list $this->redis->del('mylist'); $this->redis->rpush('mylist', 'a'); $this->redis->rpush('mylist', 'b'); $this->redis->rpush('mylist', 'c'); // Make a set $this->redis->del('myset'); $this->redis->sadd('myset', 'd'); $this->redis->sadd('myset', 'e'); $this->redis->sadd('myset', 'f'); // Basic keys $this->redis->set('key1', 'hello, world'); $this->redis->set('key2', 'hello again!'); // Use a script to return our list, and verify its response $list = $this->redis->eval("return redis.call('lrange', 'mylist', 0, -1)"); $this->assertTrue($list === array('a', 'b', 'c')); // Use a script to return our set $set = $this->redis->eval("return redis.call('smembers', 'myset')"); $this->assertTrue($set == array('d', 'e', 'f')); // Test an empty MULTI BULK response $this->redis->del('not-any-kind-of-list'); $empty_resp = $this->redis->eval("return redis.call('lrange', 'not-any-kind-of-list', 0, -1)"); $this->assertTrue(is_array($empty_resp) && empty($empty_resp)); // Now test a nested reply $nested_script = "\n\t\t\treturn {\n\t\t\t\t1,2,3, {\n\t\t\t\t\tredis.call('get', 'key1'),\n\t\t\t\t\tredis.call('get', 'key2'),\n\t\t\t\t\tredis.call('lrange', 'not-any-kind-of-list', 0, -1),\n\t\t\t\t\t{\n\t\t\t\t\t\tredis.call('smembers','myset'),\n\t\t\t\t\t\tredis.call('lrange', 'mylist', 0, -1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t"; $expected = array(1, 2, 3, array('hello, world', 'hello again!', array(), array(array('d', 'e', 'f'), array('a', 'b', 'c')))); // Now run our script, and check our values against each other $eval_result = $this->redis->eval($nested_script); $this->assertTrue(is_array($eval_result) && count($this->array_diff_recursive($eval_result, $expected)) == 0); /* * Nested reply wihin a multi/pipeline block */ $num_scripts = 10; foreach (array(Redis::PIPELINE, Redis::MULTI) as $mode) { $this->redis->multi($mode); for ($i = 0; $i < $num_scripts; $i++) { $this->redis->eval($nested_script); } $replies = $this->redis->exec(); foreach ($replies as $reply) { $this->assertTrue(is_array($reply) && count($this->array_diff_recursive($reply, $expected)) == 0); } } /* * KEYS/ARGV */ $args_script = "return {KEYS[1],KEYS[2],KEYS[3],ARGV[1],ARGV[2],ARGV[3]}"; $args_args = array('k1', 'k2', 'k3', 'v1', 'v2', 'v3'); $args_result = $this->redis->eval($args_script, $args_args, 3); $this->assertTrue($args_result === $args_args); // turn on key prefixing $this->redis->setOption(Redis::OPT_PREFIX, 'prefix:'); $args_result = $this->redis->eval($args_script, $args_args, 3); // Make sure our first three are prefixed for ($i = 0; $i < count($args_result); $i++) { if ($i < 3) { // Should be prefixed $this->assertTrue($args_result[$i] == 'prefix:' . $args_args[$i]); } else { // Should not be prefixed $this->assertTrue($args_result[$i] == $args_args[$i]); } } }
public function rpush($key, $value) { return parent::rpush($this->generateUniqueKey($key), $value); }
<?php define('EMONCMS_EXEC', 1); require "route.php"; $route = new Route(get('q')); $redis = new Redis(); $redis->connect("127.0.0.1"); if ($route->format == 'json') { if ($route->controller == 'input') { if ($route->action == 'post') { $array = array("time" => time(), "apikey" => get('apikey'), "nodeid" => (int) get('node'), "csv" => get('csv')); $msg = json_encode($array); $buflength = $redis->llen('buffer'); if ($buflength < 1000) { $redis->rpush('buffer', $msg); } } } } function get($index) { $val = null; if (isset($_GET[$index])) { $val = $_GET[$index]; } return $val; }
$redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */ $redis->delete('s'); $redis->sadd('s', 5); $redis->sadd('s', 4); $redis->sadd('s', 2); $redis->sadd('s', 1); $redis->sadd('s', 3); var_dump($redis->sort('s')); // 1,2,3,4,5 var_dump($redis->sort('s', array('sort' => 'desc'))); // 5,4,3,2,1 var_dump($redis->sort('s', array('sort' => 'desc', 'store' => 'out'))); // (int)5 $redis->persist('key'); function psubscribe($redis, $pattern, $chan, $msg) { echo "Pattern: {$pattern}\n"; echo "Channel: {$chan}\n"; echo "Payload: {$msg}\n"; } $redis->eval("return 1"); // Returns an integer: 1 $redis->eval("return {1,2,3}"); // Returns Array(1,2,3) $redis->del('mylist'); $redis->rpush('mylist', 'a'); $redis->rpush('mylist', 'b'); $redis->rpush('mylist', 'c'); // Nested response: Array(1,2,3,Array('a','b','c')); $redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}}");
<?php // redis publisher via list (we could redis pub/sub) $redis = new Redis(); $redis->connect("127.0.0.1"); $i = 0; while (true) { if (time() - $ltime > 1) { $ltime = time(); print $i . "\n"; $i = 0; } $redis->rpush('myqueue', "hello"); $i++; }
<?php $id = trim($_GET['id'], '"'); $dtime = $_GET['dtime']; $itime = $_GET['itime']; if (strlen($id) < 16) { exit("error id " . $id); } $dtime = substr($dtime, 0, 19); $itime = substr($itime, 0, 19); $dtime = strtotime($dtime); $itime = strtotime($itime); $redis = new Redis(); $redis->connect('127.0.0.1', '6379'); $redis->select(9); $redis->rpush('news_id', $id); $mysqli = new mysqli(); $mysqli->connect('localhost', 'root', 'ps!seecjkshitsaym'); $mysqli->select_db('shcms'); $sql = "update news SET input_time='{$itime}',display_time='{$dtime}' where news_id='{$id}'"; echo $sql; $res = $mysqli->query($sql); var_dump($res); $mysqli->close();
//结果:int(1) var_dump($redis->lpush("test", "222")) . '<br>'; //结果:int(2) /** rpush 描述:由列表尾部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。 参数:key,value 返回值:成功返回数组长度,失败false */ $redis->delete('test'); var_dump($redis->lpush("test", "111")) . '<br>'; //结果:int(1) var_dump($redis->lpush("test", "222")) . '<br>'; //结果:int(2) var_dump($redis->rpush("test", "333")) . '<br>'; //结果:int(3) var_dump($redis->rpush("test", "444")) . '<br>'; //结果:int(4) /** lpop 描述:返回和移除列表的第一个元素 参数:key 返回值:成功返回第一个元素的值 ,失败返回false */ $redis->delete('test'); $redis->lpush("test", "111"); $redis->lpush("test", "222"); $redis->rpush("test", "333"); $redis->rpush("test", "444");
/** * 设置某个商品在购物车里的数量 * 此项操作必须是购物车至少有一件的情况 */ public function cartSetCount() { if (Auth::check()) { $cartkey = Auth::user()->front_uid; } else { $cartkey = $this->getIP(); } $key = 'laravel:user:'******':cart'; $good_id = Input::get('good_id'); $shop_id = Input::get('shop_id'); // 不用 $count = Input::get('count'); $ids = array_count_values(Redis::lrange($key, 1, -1)); $num = $count - $ids[$good_id]; if ($num > 0) { for ($i = $num; $i > 0; $i--) { Redis::rpush($key, $good_id); } } elseif ($num < 0) { Redis::lrem($key, $num, $good_id); if (Redis::llen($key) == 1) { Redis::del($key); } } // 相等就不作处理了s //var_dump(Redis::lrange($key, 0, -1)); return Response::json(array('success' => 'true')); }