public function add($word, $score = 0) { $word = str_replace(' ', '', trim($word)); $len = mb_strlen($word, 'UTF-8'); if (!$len) { return false; } echo '=====Word:' . $word . '=====' . PHP_EOL; for ($i = 1; $i <= $len; $i++) { $key = mb_substr($word, 0, $i, 'UTF-8'); echo 'key=' . $key . PHP_EOL; if (!$this->redis->zAdd($this->key_prefix . $key, 0, $word)) { return false; } } return $this->redis->zAdd($this->word_prefix, intval($score), $word); }
/** * @inheritdoc */ public function logRequest($url, \DateTime $date = null) { if (null === $date) { $date = new \DateTime(); } $timestamp = $date->getTimestamp(); $hashKey = $this->getHashKey($timestamp, $url); $this->redis->zAdd($this->key, $timestamp, $hashKey); }
/** * @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)); } }
/** * 改变状态 * * @param $state * @return $this */ protected function state($state) { $setKey = $this->queue->name . ':' . self::JOBS_TAB; $this->client->zRem($setKey . ':' . $this->injectors['state'], $this->injectors['id']); $this->client->zRem($setKey . ':' . $this->injectors['tube'] . ':' . $this->injectors['state'], $this->injectors['id']); $this->set('state', $state); $score = $this->injectors['timing']; $this->client->zAdd($setKey . ':' . $state, $score, $this->injectors['id']); $this->client->zAdd($setKey . ':' . $this->injectors['tube'] . ':' . $state, $score, $this->injectors['id']); $this->set('updated_at', Util::now()); return $this; }
/** * @author lix * @todo 加入队列处理 * @param * @return */ public function add_queue($queue_name = 'test_queue', $val_arr = array(), $max_num = 0) { $this->config->load('redis', TRUE); // $this->debug_dump($this->config); $this->debug_dump($this->config->item('redis')); $cfg = $this->config->item('redis'); $rds = new Redis(); $rds->connect($cfg['host'], $cfg['port']); $rds->select(9); for ($i = 0; $i < $max_num; $i++) { $rds->zAdd($queue_name, $i, $val_arr[$i]); } return; }
} $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); } } // STRING for ($i = 0; $i <= rand(1000, 10000); $i++) { $incr = $redis->incr('test:incr:string'); for ($j = 0; $j <= rand(10, 100); $j++) { $redis->set('test:' . $incr . ':string', $j); } } // ZSET for ($i = 0; $i <= rand(1000, 10000); $i++) { $incr = $redis->incr('test:incr:zset'); for ($j = 0; $j <= rand(10, 100); $j++) { $redis->zAdd('test:' . $incr . ':zset', $j, md5($j)); } }
private function checkSerializer($mode) { $this->redis->delete('key'); $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === Redis::SERIALIZER_NONE); // default $this->assertTrue($this->redis->setOption(Redis::OPT_SERIALIZER, $mode) === TRUE); // set ok $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === $mode); // get ok // lPush, rPush $a = array('hello world', 42, TRUE, array('<tag>' => 1729)); $this->redis->delete('key'); $this->redis->lPush('key', $a[0]); $this->redis->rPush('key', $a[1]); $this->redis->rPush('key', $a[2]); $this->redis->rPush('key', $a[3]); // lGetRange $this->assertTrue($a === $this->redis->lGetRange('key', 0, -1)); // lGet $this->assertTrue($a[0] === $this->redis->lGet('key', 0)); $this->assertTrue($a[1] === $this->redis->lGet('key', 1)); $this->assertTrue($a[2] === $this->redis->lGet('key', 2)); $this->assertTrue($a[3] === $this->redis->lGet('key', 3)); // lRemove $this->assertTrue($this->redis->lRemove('key', $a[3]) === 1); $this->assertTrue(array_slice($a, 0, 3) === $this->redis->lGetRange('key', 0, -1)); // lSet $a[0] = array('k' => 'v'); // update $this->assertTrue(TRUE === $this->redis->lSet('key', 0, $a[0])); $this->assertTrue($a[0] === $this->redis->lGet('key', 0)); // lInsert $this->assertTrue($this->redis->lInsert('key', Redis::BEFORE, $a[0], array(1, 2, 3)) === 4); $this->assertTrue($this->redis->lInsert('key', Redis::AFTER, $a[0], array(4, 5, 6)) === 5); $a = array(array(1, 2, 3), $a[0], array(4, 5, 6), $a[1], $a[2]); $this->assertTrue($a === $this->redis->lGetRange('key', 0, -1)); // sAdd $this->redis->delete('key'); $s = array(1, 'a', array(1, 2, 3), array('k' => 'v')); $this->assertTrue(1 === $this->redis->sAdd('key', $s[0])); $this->assertTrue(1 === $this->redis->sAdd('key', $s[1])); $this->assertTrue(1 === $this->redis->sAdd('key', $s[2])); $this->assertTrue(1 === $this->redis->sAdd('key', $s[3])); // variadic sAdd $this->redis->delete('k'); $this->assertTrue(3 === $this->redis->sAdd('k', 'a', 'b', 'c')); $this->assertTrue(1 === $this->redis->sAdd('k', 'a', 'b', 'c', 'd')); // sRemove $this->assertTrue(1 === $this->redis->sRemove('key', $s[3])); $this->assertTrue(0 === $this->redis->sRemove('key', $s[3])); // variadic $this->redis->delete('k'); $this->redis->sAdd('k', 'a', 'b', 'c', 'd'); $this->assertTrue(2 === $this->redis->sRem('k', 'a', 'd')); $this->assertTrue(2 === $this->redis->sRem('k', 'b', 'c', 'e')); $this->assertTrue(FALSE === $this->redis->exists('k')); // sContains $this->assertTrue(TRUE === $this->redis->sContains('key', $s[0])); $this->assertTrue(TRUE === $this->redis->sContains('key', $s[1])); $this->assertTrue(TRUE === $this->redis->sContains('key', $s[2])); $this->assertTrue(FALSE === $this->redis->sContains('key', $s[3])); unset($s[3]); // sMove $this->redis->delete('tmp'); $this->redis->sMove('key', 'tmp', $s[0]); $this->assertTrue(FALSE === $this->redis->sContains('key', $s[0])); $this->assertTrue(TRUE === $this->redis->sContains('tmp', $s[0])); unset($s[0]); // sorted sets $z = array('z0', array('k' => 'v'), FALSE, NULL); $this->redis->delete('key'); // zAdd $this->assertTrue(1 === $this->redis->zAdd('key', 0, $z[0])); $this->assertTrue(1 === $this->redis->zAdd('key', 1, $z[1])); $this->assertTrue(1 === $this->redis->zAdd('key', 2, $z[2])); $this->assertTrue(1 === $this->redis->zAdd('key', 3, $z[3])); // zDelete $this->assertTrue(1 === $this->redis->zDelete('key', $z[3])); $this->assertTrue(0 === $this->redis->zDelete('key', $z[3])); unset($z[3]); // check that zDelete doesn't crash with a missing parameter (GitHub issue #102): $this->assertTrue(FALSE === @$this->redis->zDelete('key')); // variadic $this->redis->delete('k'); $this->redis->zAdd('k', 0, 'a'); $this->redis->zAdd('k', 1, 'b'); $this->redis->zAdd('k', 2, 'c'); $this->assertTrue(2 === $this->redis->zDelete('k', 'a', 'c')); $this->assertTrue(1.0 === $this->redis->zScore('k', 'b')); $this->assertTrue($this->redis->zRange('k', 0, -1, true) == array('b' => 1.0)); // zRange $this->assertTrue($z === $this->redis->zRange('key', 0, -1)); // zScore $this->assertTrue(0.0 === $this->redis->zScore('key', $z[0])); $this->assertTrue(1.0 === $this->redis->zScore('key', $z[1])); $this->assertTrue(2.0 === $this->redis->zScore('key', $z[2])); // zRank $this->assertTrue(0 === $this->redis->zRank('key', $z[0])); $this->assertTrue(1 === $this->redis->zRank('key', $z[1])); $this->assertTrue(2 === $this->redis->zRank('key', $z[2])); // zRevRank $this->assertTrue(2 === $this->redis->zRevRank('key', $z[0])); $this->assertTrue(1 === $this->redis->zRevRank('key', $z[1])); $this->assertTrue(0 === $this->redis->zRevRank('key', $z[2])); // zIncrBy $this->assertTrue(3.0 === $this->redis->zIncrBy('key', 1.0, $z[2])); $this->assertTrue(3.0 === $this->redis->zScore('key', $z[2])); $this->assertTrue(5.0 === $this->redis->zIncrBy('key', 2.0, $z[2])); $this->assertTrue(5.0 === $this->redis->zScore('key', $z[2])); $this->assertTrue(2.0 === $this->redis->zIncrBy('key', -3.0, $z[2])); $this->assertTrue(2.0 === $this->redis->zScore('key', $z[2])); // mset $a = array('k0' => 1, 'k1' => 42, 'k2' => NULL, 'k3' => FALSE, 'k4' => array('a' => 'b')); $this->assertTrue(TRUE === $this->redis->mset($a)); foreach ($a as $k => $v) { $this->assertTrue($this->redis->get($k) === $v); } $a = array('k0' => 1, 'k1' => 42, 'k2' => NULL, 'k3' => FALSE, 'k4' => array('a' => 'b')); // hSet $this->redis->delete('key'); foreach ($a as $k => $v) { $this->assertTrue(1 === $this->redis->hSet('key', $k, $v)); } // hGet foreach ($a as $k => $v) { $this->assertTrue($v === $this->redis->hGet('key', $k)); } // hGetAll $this->assertTrue($a === $this->redis->hGetAll('key')); $this->assertTrue(TRUE === $this->redis->hExists('key', 'k0')); $this->assertTrue(TRUE === $this->redis->hExists('key', 'k1')); $this->assertTrue(TRUE === $this->redis->hExists('key', 'k2')); $this->assertTrue(TRUE === $this->redis->hExists('key', 'k3')); $this->assertTrue(TRUE === $this->redis->hExists('key', 'k4')); // hMSet $this->redis->delete('key'); $this->redis->hMSet('key', $a); foreach ($a as $k => $v) { $this->assertTrue($v === $this->redis->hGet('key', $k)); } // hMget $hmget = $this->redis->hMget('key', array_keys($a)); foreach ($hmget as $k => $v) { $this->assertTrue($v === $a[$k]); } // getMultiple $this->redis->set('a', NULL); $this->redis->set('b', FALSE); $this->redis->set('c', 42); $this->redis->set('d', array('x' => 'y')); $this->assertTrue(array(NULL, FALSE, 42, array('x' => 'y')) === $this->redis->getMultiple(array('a', 'b', 'c', 'd'))); // pipeline $this->sequence(Redis::PIPELINE); // multi-exec $this->sequence(Redis::MULTI); // keys $this->assertTrue(is_array($this->redis->keys('*'))); // issue #62, hgetall $this->redis->del('hash1'); $this->redis->hSet('hash1', 'data', 'test 1'); $this->redis->hSet('hash1', 'session_id', 'test 2'); $data = $this->redis->hGetAll('hash1'); $this->assertTrue($data['data'] === 'test 1'); $this->assertTrue($data['session_id'] === 'test 2'); // issue #145, serializer with objects. $this->redis->set('x', array(new stdClass(), new stdClass())); $x = $this->redis->get('x'); $this->assertTrue(is_array($x)); $this->assertTrue(is_object($x[0]) && get_class($x[0]) === 'stdClass'); $this->assertTrue(is_object($x[1]) && get_class($x[1]) === 'stdClass'); // revert $this->assertTrue($this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE) === TRUE); // set ok $this->assertTrue($this->redis->getOption(Redis::OPT_SERIALIZER) === Redis::SERIALIZER_NONE); // get ok }
$guanyintea_keys = $redis->keys('guanyintea*'); foreach ($guanyintea_keys as $key) { if ($redis->exists($key)) { $redis->delete($key); echo $key . " delete success " . PHP_EOL; } else { die('delete table guanyintea key not exists'); } } $sql = 'select * from guanyintea'; $result = mysql_query($sql); $i = 0; while ($guanyintea_res = mysql_fetch_array($result)) { $redis->set('guanyintea:product_id:title:' . $guanyintea_res['product_id'], $guanyintea_res['title']); $redis->set('guanyintea:product_id:old_price:' . $guanyintea_res['product_id'], $guanyintea_res['old_price']); $redis->set('guanyintea:product_id:new_price:' . $guanyintea_res['product_id'], $guanyintea_res['new_price']); $redis->set('guanyintea:product_id:subhead:' . $guanyintea_res['product_id'], $guanyintea_res['subhead']); $redis->set('guanyintea:product_id:weight:' . $guanyintea_res['product_id'], $guanyintea_res['weight']); $redis->set('guanyintea:product_id:product_num:' . $guanyintea_res['product_id'], $guanyintea_res['product_num']); if ($guanyintea_res['is_show'] == 1) { $redis->zAdd('guanyintea:is_show:product_id:1:', $i, $guanyintea_res['product_id']); } else { $redis->zAdd('guanyintea:is_show:product_id:0:', $i, $guanyintea_res['product_id']); } echo 'guanyintea ' . $guanyintea_res['product_id'] . ' update success' . PHP_EOL; $i++; } mysql_free_result($result); echo 'table guanyintea update success ' . PHP_EOL; mysql_close($mysql_conn); echo 'update success';
<?php header("Content-type:text/html;charset='utf-8'"); //###################### $redis = new Redis(); $redis->connect('localhost', '6379'); #此处加上验证更安全 $wytypeid = $redis->zRangeByScore('zjseowytypeid', '-inf', '+inf', array('withscores' => false)); //如果为数组为0的话,组件一个0~19的数组,没执行一次sPop随机返回并删除名称为key的set中一个元素 //$redis->DEL('typeid'); if (count($wytypeid) == 0) { for ($i = 0; $i < 10; $i++) { $redis->zAdd('zjseowytypeid', $i, $i); } $wytypeid = $redis->zRangeByScore('zjseowytypeid', '-inf', '+inf', array('withscores' => false)); } $randwytypeid = array_rand($wytypeid); //取得key $lanmu = $wytypeid[$randwytypeid]; //根据key删除值 $redis->zRemRangeByScore('zjseowytypeid', $lanmu, $lanmu); //根据取得要删除的key选择一个相等的值去除 //var_dump($wytypeid);exit; $jkysid = array(47, 48, 49, 50, 51, 52, 53, 54, 55, 56); //这里是对应的织梦后台栏目 //对应10个栏目的id //var_dump($wyurl); $type = $jkysid[$lanmu]; //根据上面取到的得到要插入数据的栏目 //////////////////////////////////////////【获取关键词开始】////////////////////////////////////////////////////// //获取关键词从redis中[每获取一个就删除一个,知道关键词耗尽]
/** * @param string $key * @param float $score * @param string $value */ public function zAdd($key, $score, $value) { $this->_redis->zAdd($key, $score, $value); }
$len = count($userset); echo 'user count: ' . $len, PHP_EOL; //generate 1-dim ran $i = 0; $tb = microtime(true); foreach ($userset as $uid) { $pipeline->sCard($uid); if ($i % 10000 == 0) { echo '*'; } $i++; } $tempArray = $pipeline->exec(); $i = 0; foreach ($userset as $index => $uid) { $redis->zAdd('rank-1', $tempArray[$index], $uid); if ($i % 10000 == 0) { echo '.'; } $i++; } echo PHP_EOL; $te = microtime(true); $cost = $te - $tb; echo 'generate 1-dim rank cost: ' . $cost, PHP_EOL; //generate 2-dim rank $tb = microtime(true); $i = 0; foreach ($userset as $uid) { $pipeline->sMembers($uid); if ($i % 10000 == 0) {
function remakeRedis() { $total_cnt = 0; $redis = new \Redis(); try { $redis->connect('127.0.0.1', '6379', 2.5, NULL, 150); if ($redis->select(1) == false) { \CADB\RespondJson::ResultPage(array(-1, 'redis 데이터베이스에 연결할 수 없습니다.')); } $redis->flushDb(); if (is_array($this->autocomplete)) { foreach ($this->autocomplete as $k => $_data) { foreach ($_data as $data) { $redis->zAdd($k, $data['score'], $data['name']); $total_cnt++; } } } } catch (RedisException $e) { var_dump($e); } $redis->close(); \CADB\RespondJson::ResultPage(array(0, $total_cnt . '건의 자동완성문장을 입력했습니다.')); }
public function testZX() { $this->redis->delete('key'); $this->assertTrue(array() === $this->redis->zRange('key', 0, -1)); $this->assertTrue(array() === $this->redis->zRange('key', 0, -1, true)); $this->assertTrue(1 === $this->redis->zAdd('key', 0, 'val0')); $this->assertTrue(1 === $this->redis->zAdd('key', 2, 'val2')); $this->assertTrue(1 === $this->redis->zAdd('key', 1, 'val1')); $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3')); $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('key', 0, -1)); // withscores $ret = $this->redis->zRange('key', 0, -1, true); $this->assertTrue(count($ret) == 4); $this->assertTrue($ret['val0'] == 0); $this->assertTrue($ret['val1'] == 1); $this->assertTrue($ret['val2'] == 2); $this->assertTrue($ret['val3'] == 3); $this->assertTrue(0 === $this->redis->zDelete('key', 'valX')); $this->assertTrue(1 === $this->redis->zDelete('key', 'val3')); $this->assertTrue(array('val0', 'val1', 'val2') === $this->redis->zRange('key', 0, -1)); // zGetReverseRange $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3')); $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'aal3')); $zero_to_three = $this->redis->zRangeByScore('key', 0, 3); $this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three); $this->assertTrue(5 === $this->redis->zCount('key', 0, 3)); // withscores $this->redis->zRemove('key', 'aal3'); $zero_to_three = $this->redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE)); $this->assertTrue(array('val0' => 0, 'val1' => 1, 'val2' => 2, 'val3' => 3) == $zero_to_three); $this->assertTrue(4 === $this->redis->zCount('key', 0, 3)); // limit $this->assertTrue(array('val0') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 1)))); $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 2)))); $this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2)))); $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100)))); $this->assertTrue(4 === $this->redis->zSize('key')); $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1')); $this->assertFalse($this->redis->zScore('key', 'val')); $this->assertFalse($this->redis->zScore(3, 2)); // zincrby $this->redis->delete('key'); $this->assertTrue(1.0 === $this->redis->zIncrBy('key', 1, 'val1')); $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1')); $this->assertTrue(2.5 === $this->redis->zIncrBy('key', 1.5, 'val1')); $this->assertTrue(2.5 === $this->redis->zScore('key', 'val1')); //zUnion $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); $this->redis->delete('keyU'); $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key2', 2, 'val2'); $this->redis->zAdd('key2', 3, 'val3'); $this->redis->zAdd('key3', 4, 'val4'); $this->redis->zAdd('key3', 5, 'val5'); $this->assertTrue(4 === $this->redis->zUnion('keyU', array('key1', 'key3'))); $this->assertTrue(array('val0', 'val1', 'val4', 'val5') === $this->redis->zRange('keyU', 0, -1)); // Union on non existing keys $this->redis->delete('keyU'); $this->assertTrue(0 === $this->redis->zUnion('keyU', array('X', 'Y'))); $this->assertTrue(array() === $this->redis->zRange('keyU', 0, -1)); // !Exist U Exist $this->redis->delete('keyU'); $this->assertTrue(2 === $this->redis->zUnion('keyU', array('key1', 'X'))); $this->assertTrue($this->redis->zRange('key1', 0, -1) === $this->redis->zRange('keyU', 0, -1)); // test weighted zUnion $this->redis->delete('keyZ'); $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(1, 1))); $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('keyZ', 0, -1)); $this->redis->zDeleteRangeByScore('keyZ', 0, 10); $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(5, 1))); $this->assertTrue(array('val0', 'val2', 'val3', 'val1') === $this->redis->zRange('keyZ', 0, -1)); $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); // zInter $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key1', 3, 'val3'); $this->redis->zAdd('key2', 2, 'val1'); $this->redis->zAdd('key2', 3, 'val3'); $this->redis->zAdd('key3', 4, 'val3'); $this->redis->zAdd('key3', 5, 'val5'); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2'))); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); // Union on non existing keys $this->assertTrue(0 === $this->redis->zInter('keyX', array('X', 'Y'))); $this->assertTrue(array() === $this->redis->zRange('keyX', 0, -1)); // !Exist U Exist $this->assertTrue(0 === $this->redis->zInter('keyY', array('key1', 'X'))); $this->assertTrue(array() === $this->redis->zRange('keyY', 0, -1)); // test weighted zInter $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key1', 3, 'val3'); $this->redis->zAdd('key2', 2, 'val1'); $this->redis->zAdd('key2', 1, 'val3'); $this->redis->zAdd('key3', 7, 'val1'); $this->redis->zAdd('key3', 3, 'val3'); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2'), array(1, 1))); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'min')); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'max')); $this->assertTrue(array('val3', 'val1') === $this->redis->zRange('keyI', 0, -1)); // zrevrange $this->redis->delete('z'); $this->redis->zadd('z', 1, 'one'); $this->redis->zadd('z', 2, 'two'); $this->redis->zadd('z', 5, 'five'); $this->redis->zadd('z', 10, 'ten'); $this->assertTrue(array('ten') === $this->redis->zReverseRange('z', 0, 0)); $this->assertTrue(array('ten', 'five', 'two') === $this->redis->zReverseRange('z', 0, 2)); $this->assertTrue(array('five', 'two') === $this->redis->zReverseRange('z', 1, 2)); $this->assertTrue(array('five') === $this->redis->zReverseRange('z', 1, 1)); $this->assertTrue(array() === $this->redis->zReverseRange('z', 1, 0)); $this->assertTrue(array() === $this->redis->zReverseRange('z', -1, 0)); // zrank, zrevrank $this->redis->delete('z'); $this->redis->zadd('z', 1, 'one'); $this->redis->zadd('z', 2, 'two'); $this->redis->zadd('z', 5, 'five'); $this->assertTrue(0 === $this->redis->zRank('z', 'one')); $this->assertTrue(1 === $this->redis->zRank('z', 'two')); $this->assertTrue(2 === $this->redis->zRank('z', 'five')); $this->assertTrue(2 === $this->redis->zRevRank('z', 'one')); $this->assertTrue(1 === $this->redis->zRevRank('z', 'two')); $this->assertTrue(0 === $this->redis->zRevRank('z', 'five')); }
public function getredis() { die; $redis = new Redis(); $redis->connect('121.40.144.140', 6379); $result = $redis->set('test', "11111111111"); //var_dump($result); //结果:bool(true) $getinfo = $redis->get('test'); var_dump($getinfo); $redis->delete('test'); //写入排行榜 $redis->zAdd('readsort', 45, '1'); $redis->zAdd('readsort', 5, '2'); $redis->zAdd('readsort', 55, '3'); $redis->zAdd('readsort', 45, '4'); $redis->zAdd('readsort', 86, '5'); $redis->zAdd('readsort', 8, '6'); $redis->zDelete('readsort', '6'); $redis->zDelete('readsort', '5'); $redis->zDelete('readsort', '4'); $redis->zDelete('readsort', '3'); $redis->zDelete('readsort', '2'); $redis->zDelete('readsort', '1'); //$data=$redis->zRange('readsort',0,-1,true); //从大到小排列zRevRange $data = $redis->zRevRange('readsort', 0, -1, true); var_dump($data); }
/** * 将value写入set集合 如果value存在 不写入 返回false * 如果是有序集合则根据score值更新该元素的顺序 * @param $set string 集合名 * @param $value mixed 值 * @param $stype int 集合类型 0:无序集合 1:有序集和 默认0 * @param $score int 元素排序值 */ public static function setAdd($set, $value = null, $stype = 0, $score = null) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; if ($stype && $score !== null) { $return = $redis->zAdd($set, $score, $value); } else { $return = $redis->sAdd($set, $value); } $redis->close(); $redis = null; return $return; }
<?php /** 统一的唯一标识数据添加,包含内,外部 * */ /** $url_arr = array ('baidu.com/aa/bb', 'yahoo.com/cc/dd', '360.com/ee/ff', 'google.com/gg/hh' ); **/ $sessid_arr = array('li9t209jm7mc6m4vmn88o5a7j0', 'li9t209jm7mc769vmn88o5a345', 'l8hy209jm7mc769vmn88o5a999', 'li99j2m7mc769vmnss88o5a875', 'li9t27mskklc769vmn88o5appp'); $qudao_tags = array('baidu', 'yahoo', '360', 'google'); $ip_arr = array('10.10.10.29', '55.55.55.22', '87.56.45.59', '48.15.46.54', '48.15.46.78'); $sywurl_arr = array('www.xxxxxx.com/goods/gooddetail/1020', 'www.xxxxxx.com/bsq', 'www.xxxxxx.com/koubei', 'www.xxxxxx.com/help/999', 'www.xxxxxx.com/member/xxx', 'www.xxxxxx.com'); $rds = new Redis(); $rds->connect('127.0.0.1', 6379); $rds->select(1); for ($i = 0; $i < 100; $i++) { $t = microtime(true); //$rds->set( $sessid_arr[array_rand($sessid_arr)].':'.$t.':'.$ip_arr[array_rand($ip_arr)].':0', $t.':'.$url_arr[array_rand($url_arr)].':'.$ip_arr[array_rand($ip_arr)].':0:0:0:0' ); $key = $sessid_arr[array_rand($sessid_arr)] . ':' . $t . ':' . $ip_arr[array_rand($ip_arr)] . ':' . $qudao_tags[array_rand($qudao_tags)] . ':0'; //$rds->set( $sessid_arr[array_rand($sessid_arr)].':'.$t.':'.$ip_arr[array_rand($ip_arr)].':'.$qudao_tags[array_rand($qudao_tags)].':0', 'www.xxxxxx.com/goods/gooddetail/1020:'.microtime(true) ); //$rds->set( $key, 'www.xxxxxx.com/goods/gooddetail/1020:'.microtime(true) ); for ($j = 0; $j < 100; $j++) { $res = $rds->zAdd($key, microtime(true) . rand(1, 10), $sywurl_arr[array_rand($sywurl_arr)]); } //'sleep(1); }