/** * @param mixed $value * * @return RedisNoSQLList */ public function prepend($value) { $this->redis->lpush($this->key, $value); if ($this->timeout) { $this->redis->setTimeout($this->key, $this->timeout); } return $this; }
public function testObject() { $this->redis->del('key'); $this->assertTrue($this->redis->object('encoding', 'key') === FALSE); $this->assertTrue($this->redis->object('refcount', 'key') === FALSE); $this->assertTrue($this->redis->object('idletime', 'key') === FALSE); $this->redis->set('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "raw"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "ziplist"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "hashtable"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 42); $this->redis->sadd('key', 1729); $this->assertTrue($this->redis->object('encoding', 'key') === "intset"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', str_repeat('A', pow(10, 6))); // 1M elements, too big for a ziplist. $this->assertTrue($this->redis->object('encoding', 'key') === "linkedlist"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); }
public function update_weixin_user($openid, $broker_id) { $this->api_model->update_weixin_user($openid); $redis = new Redis(); $redis->connect(REDIS_HOST, REDIS_PORT); $redis->auth(REDIS_AUTH); $key = "map:" . $broker_id; $users = $redis->lrange($key, 0, -1); if (!in_array($openid, $users)) { $redis->lpush($key, $openid); } }
function my_onStart($serv) { $redis = new Redis(); $redis->pconnect('127.0.0.1', 6379); $redis->flushAll(); $work_arr = array(); for ($i = 0; $i < $serv->setting['task_worker_num']; $i++) { $redis->lpush('free', $i); $work_arr[] = $i; } echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n"; echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n"; }
/** * Change state * * @param $state * @return $this */ public function state($state) { $this->emit($state); $this->removeState(); // Keep "FIFO!" $score = $this->injectors['timing'] + $this->injectors['priority']; $this->set('state', $state); $this->client->zadd('q:jobs', $score, $this->injectors['id']); $this->client->zadd('q:jobs:' . $state, $score, $this->injectors['id']); $this->client->zadd('q:jobs:' . $this->injectors['type'] . ':' . $state, $score, $this->injectors['id']); // Set inactive job to waiting list if ($this->queue->originalMode() && 'inactive' == $state) { $this->client->lpush('q:' . $this->injectors['type'] . ':jobs', 1); } $this->set('updated_at', Util::now()); return $this; }
public function bind() { $redis = new Redis(); $redis->connect(REDIS_HOST, REDIS_PORT); $redis->auth(REDIS_AUTH); $results = $this->job_model->getWxUserKeys(); $keys = array_map(function ($v) { return 'map:' . $v['broker_id']; }, $results); $redis->delete($keys); $results = $this->job_model->getWxUser(); foreach ($results as $u) { $key = 'map:' . $u['broker_id']; $open_id = $u['open_id']; $redis->lpush($key, $open_id); } }
public function testObject() { /* Version 3.0.0 (represented as >= 2.9.0 in redis info) and moving * forward uses "embstr" instead of "raw" for small string values */ if (version_compare($this->version, "2.9.0", "lt")) { $str_small_encoding = "raw"; } else { $str_small_encoding = "embstr"; } $this->redis->del('key'); $this->assertTrue($this->redis->object('encoding', 'key') === FALSE); $this->assertTrue($this->redis->object('refcount', 'key') === FALSE); $this->assertTrue($this->redis->object('idletime', 'key') === FALSE); $this->redis->set('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === $str_small_encoding); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', 'value'); /* Newer versions of redis are going to encode lists as 'quicklists', * so 'quicklist' or 'ziplist' is valid here */ $str_encoding = $this->redis->object('encoding', 'key'); $this->assertTrue($str_encoding === "ziplist" || $str_encoding === 'quicklist'); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "hashtable"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 42); $this->redis->sadd('key', 1729); $this->assertTrue($this->redis->object('encoding', 'key') === "intset"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', str_repeat('A', pow(10, 6))); // 1M elements, too big for a ziplist. $str_encoding = $this->redis->object('encoding', 'key'); $this->assertTrue($str_encoding === "linkedlist" || $str_encoding == "quicklist"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); }
public function testRpopLpush() { // standard case. $this->redis->delete('x', 'y'); $this->redis->lpush('x', 'abc'); $this->redis->lpush('x', 'def'); // x = [def, abc] $this->redis->lpush('y', '123'); $this->redis->lpush('y', '456'); // y = [456, 123] $this->assertEquals($this->redis->rpoplpush('x', 'y'), 'abc'); // we RPOP x, yielding abc. $this->assertEquals($this->redis->lgetRange('x', 0, -1), array('def')); // only def remains in x. $this->assertEquals($this->redis->lgetRange('y', 0, -1), array('abc', '456', '123')); // abc has been lpushed to y. // with an empty source, expecting no change. $this->redis->delete('x', 'y'); $this->assertTrue(FALSE === $this->redis->rpoplpush('x', 'y')); $this->assertTrue(array() === $this->redis->lgetRange('x', 0, -1)); $this->assertTrue(array() === $this->redis->lgetRange('y', 0, -1)); }
public function redisProducerAction() { $content = $this->getpost('content'); $config = Yaf_Application::app()->getConfig(); $queue = 'test_queue'; $host = $config['redis_host']; $port = $config['redis_port']; $redis = new Redis(); $redis->connect($host, $port); $redis->lpush($queue, $content); echo 1; die; }
try { $serv = new swoole_websocket_server("127.0.0.1", 9503); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $serv->on('Open', function ($server, $req) { echo "connection open: " . $req->fd . "\n"; }); $serv->on('Message', function ($server, $frame) use($redis, $serv) { $request = json_decode($frame->data, true); print_r($request); $cmd = $request['cmd']; $data = $request['data']; switch ($cmd) { case 'login': $uid = 'uid_' . $frame->fd; $redis->lpush('uid_list', $frame->fd); $username = $data['username']; $redis->set($uid, $username); $user_list = get_online_users($redis); $response = ['cmd' => 'login', 'data' => ['user' => $username, 'user_list' => $user_list]]; $response_data = json_encode($response); //通知新用户登录 foreach ($serv->connections as $fd) { $serv->push($fd, $response_data); } break; case 'send_msg': $username = $data['from']; $send_to = $data['send_to']; $content = $data['content']; $color = $data['color'] ?? '';
public function lpush($key, $value) { return parent::lpush($this->generateUniqueKey($key), $value); }
if ($rpassword) { $redis_handle->auth($rpassword); } $redis_handle->select($rdb); $redis_bi_key = 'bi'; while (true) { if ($to_exit) { echo "[" . date('Y-m-d H:i:s') . "] track log quit for user end\n"; break; } if ($redis_handle->llen($redis_bi_key) > 0) { $url = $redis_handle->rpop($redis_bi_key); if (!empty($url)) { $result = curl($url); if ($result === false) { $redis_handle->lpush($redis_bi_key, $url); } } } //usleep(100000); usleep(1); } /** * * @param string $url * @param array $post_data * @param string $proxy * @param int $max_loop * @return mixed */ function curl($url, $post_data = '', $proxy = '', $max_loop = 1)
<?php header("Content-type:text/html;charset=utf-8"); $username = $_POST['username']; $phone = $_POST['phone']; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $begin_time = time(); for ($i = 0; $i < 5000; $i++) { $redis->set('username' . $i, $username); $redis->set('phone' . $i, $phone); $redis->lpush("tutorial-list", "Redis" . $i); } $arList = $redis->lrange("tutorial-list", 0, 1000); echo "Stored string in redis:: "; print_r($arList); $end_time = time(); echo $end_time - $begin_time;
<?php $redis = new Redis(); $redis->connect("127.0.0.1", "6379"); // string $redis->delete("KeyTime"); $redis->mset(array('key111' => "key111", "key222" => "key222")); echo (int) $redis->exists("key111"); $array = $redis->getMultiple(array("key111", "key222")); echo "<br>"; print_r($array); for ($i = 0; $i < 10; $i++) { $redis->lpush("list", $i); } $redis->lpop("list"); $redis->rpop("list"); echo $redis->lsize("list"); echo $redis->lget("list", 0); echo $redis->lset("list", 1, "new_value"); $data = $redis->lRange("list", 0, -1); echo "<pre>"; print_r($data); $bool = $redis->ltrim("list", 0, 5); echo $redis->lrem("list", "5"); $bool = $redis->rpoplpush("srcKey", "dstKey"); // SET for ($i = 0; $i < 10; $i++) { $redis->sadd("myset", $i + rand(10, 99)); } $bool = $redis->srem("myset", 16); echo (int) $bool;
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $channel = $argv[1]; // channel $msg = $argv[2]; // msg $redis->select(2); $redis->lpush('channel' . $channel, $msg);
/** * 消息编辑 */ public function message_edit() { // @写入redis if (IS_POST) { $post = I('post.'); $id = $post['id']; if ($post['start_time']) { $post['start_time'] = strtotime($post['start_time']); } if ($id) { $post['id'] = $id; $post['uptime'] = time(); $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); if ($post['to_qun']) { $qun_info = M('qun')->find($post['to_qun']); if ($qun_info['robot_id']) { $key = "robot_" . $qun_info['robot_id']; $msg = array('msg' => $post['content'], 'uid' => $qun_info['username']); $redis->lpush($key, json_encode($msg)); } } else { $qun_list = M('qun')->select(); foreach ($qun_list as $qun_info) { if ($qun_info['robot_id']) { $key = "robot_" . $qun_info['robot_id']; $msg = array('msg' => $post['content'], 'uid' => $qun_info['username']); $redis->lpush($key, json_encode($msg)); } } } $ok = M('qun_message')->save($post); } else { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); if ($post['to_qun']) { $qun_info = M('qun')->find($post['to_qun']); if ($qun_info['robot_id']) { $key = "robot_" . $qun_info['robot_id']; $msg = array('msg' => $post['content'], 'uid' => $qun_info['username']); $redis->lpush($key, json_encode($msg)); } } else { $qun_list = M('qun')->select(); foreach ($qun_list as $qun_info) { if ($qun_info['robot_id']) { $key = "robot_" . $qun_info['robot_id']; $msg = array('msg' => $post['content'], 'uid' => $qun_info['username']); $redis->lpush($key, json_encode($msg)); } } } $post['addtime'] = time(); $ok = M('qun_message')->add($post); } if ($ok) { $this->success('成功', $_SERVER['HTTP_REFERER']); } else { $this->error('失败'); } return; } $httpget = I('get.'); $id = $httpget['id']; if ($id) { $message = M('qun_message')->find($id); $this->assign('message', $message); } $qun_list = M('qun')->field('id,nickname')->select(); $this->assign('qun_list', $qun_list); $this->display(); }
function setRedis($key, $value) { $redis = new Redis(); $redis->connect(HOST_REDIS, 6379); $redis->lpush($key, $value); }
public function choose_broker($id, $o_bid) { $wx_user = $this->house_model->choose_broker($id); if (!empty($wx_user)) { $open_id = $wx_user['open_id']; $broker_id = $wx_user['broker_id']; $this->api_model->update_weixin_user($open_id); $this->session->set_userdata('rel_name', $wx_user['rel_name']); $this->session->set_userdata('wx_broker_id', $broker_id); if ($broker_id != $o_bid) { $redis = new Redis(); $redis->connect(REDIS_HOST, REDIS_PORT); $redis->auth(REDIS_AUTH); if (!empty($o_bid)) { $o_key = "map:" . $o_bid; $redis->lrem($o_key, $open_id, 0); } $key = "map:" . $broker_id; $users = $redis->lrange($key, 0, -1); if (!in_array($open_id, $users)) { $redis->lpush($key, $open_id); } } } $this->view_list(1); }
/** * 从购物车删除 */ public function cartDel() { if (Auth::check()) { $cartkey = Auth::user()->front_uid; } else { $cartkey = $this->getIP(); } $key = 'laravel:user:'******':cart'; $good_id = Input::get('good_id'); $shop_id = Redis::lrange($key, 0, 0); if (Redis::lrem($key, 0, $good_id)) { if ($shop_id[0] == $good_id) { Redis::lpush($key, $shop_id); } if (Redis::llen($key) == 1) { Redis::del($key); } return Response::json(array('success' => 'true')); } }
$redis->set('demo3', $int); $redis->set('demo4', $string); $result1 = $redis->getMultiple(array('demo2', 'demo3', 'demo4')); echo '<pre>'; var_dump($result1) . '<br>'; /** lpush 将一个或多个值 value 插入到列表 key 的表头 描述:由列表头部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回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) /** 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)