Predis? PHP? Redis? ???? ?? ?? ???????, PHP5.3 ??? ????? ??? ??? ?? 25,000????. C ??? ??? PHP ??(?: C ?? phpredis https://github.com/owlient/phpredis ??)?? ???? ??? ?? ??? ???? ????.
?? ???? redis? ???? ?? ?? ?????. session.save_handler = redis session.save_path = “tcp://127.0.0.1:6379″
??? ?? ??? ??? ??? ????? ????? ?????.
- //?? ??? ???? ?? ?????? ?????. ??? ??? $file;
- spl_autoload_register(function($class) {
- $file = __DIR__.'? ???? ????. / lib/Predis/'.$class.'.php';
- if (file_exists($file)) {
- require $file;
- return true;
- }
- });
-
- //?? IP, ?? ? ?? ?????? ??
- $server = array(
- 'host' => '127.0.0.1',
- 'port' => ; 6379,
- '??????' => 15
- );
- $redis = new Client($server);
-
- //?? ??/???? ??
- $ redis- >set('library', 'predis');
- $retval = $redis->get('library');
- echo $retval; //'predis' ??
-
- //setex ?? ???? ??
- $redis->setex('str', 10, 'bar') //?? ????? 10??? ?????
-
- //setnx/ msetnx? ?????. ?? ??? ?? ?? ?? ???? ????.
- $redis->setnx('foo',12) //true
- $redis->setnx('foo',34) ); //false
-
- //set? ??? getset ??, ??? ?? ?? ?? ?????
- $redis->getset('foo',56);//returns 34
-
- // incrby/incr/decrby/decr ?? ???? ????.
- $redis->incr('foo') //foo? 57
- $redis->incrby(' foo',2); //foo? 59???
-
- //exists? ?? ?? ????? ?????
- $redis->exists('foo');//true
-
- //del delete
- $redis->del('foo');//true
-
- //?? ??, ??? ?? ???, ?? ?? ??, ?? ??? ?? ??/zset, ?? ???? ??? ?????
- $redis->type('foo');//???? ??? ??? ?????
- $redis->set('str','test');
- $redis ->type('str '); //???, ?? ???
-
- //append? ?? ???? ?????.
- $redis->append('str','_123'); //??? ?? ?????. ??? ??? 8??, ??? str? 'test_123'???
-
- //setrange ?? ?? ??
- $redis->setrange('str',0,'abc' ); //return 3, ???? 2? 0?? ?? ??
- $redis->setrange('str',2,'cd');//?? ??? ???? 4? ?????. ? ?? ?? ???? 'str'? 'abcd'???
-
- //substr ?? ?? ??
- $redis->substr('str',0,2);// 0?? ??, ? ?? ?? ????, ? 3?, 'abc' ??
-
- //strlen ??? ?? ????
- $redis->strlen('str') //Return 4
-
- //setbit/ getbit ?? ?? ? ??
- $redis->setbit('binary',31,1); //31?? ??? 1? ???? ?????. ??? ??? ??? ?????? ??? ??? ?? ????. getbit??? ??? ?????
- $redis->getbit('binary',31); //return 1
-
- //keys ?? ?? ??, * ?? ? ? ?? ??(? ??? ??)
- $redis->set('foo1',123);
- $redis->set('foo2',456);
- $redis->keys('foo*'); // foo1? foo2? ??? ?????.
- $redis->keys('f?o?') //?? ????
-
- //randomkey? ???? ?? ?????
- $redis->randomkey( ); //'foo1' ?? 'foo2' ? redis? ???? ?? ?? ??? ? ????
-
- //rename/ renamenx? ? ??? ????. ???? renamenx? ?? ??? ??? ???? ???? ????. key
- $redis->rename('str','str2') //?? ??? 'str'? ?? ' str2'
-
- //expire ? ??- ?? ???, ttl? ?? ?? ??? ???? ??? ?? ???? ??????
- $redis->expire('foo', 1) // ????? 1?? ??
- $redis->ttl( 'foo'); //???? ?? 1?? ??
- $redis->expire('foo'); //?? ?? ??
-
- //dbsize ?? Redis ??????? ? ??? ?? ?????.
- $redis->dbsize();
-
- /*
- ? ??
- * /
-
- //rpush/rpushx ??? ??? ?? ??, ????? ?? ??
- //lpush/lpushx? rpush/rpushx? ???? ???? ??? ????? ????. ?? ?? 'x'? ?? ???? ????? ?????.
- $redis->rpush('fooList', ' bar1') //??? ??? ?????. 1
- $redis->lpush( 'fooList', 'bar0'); //??? ??? ?????. 2
- $redis->rpushx(' fooList', 'bar2') //3? ?????. rpushx? ?? ????? ?????. ??? ??? 0? ?????.
- //llen? ?? ?? ??? ?????.
- $redis->llen('fooList') ;//3
-
- //lrange? ???? ?? ??? ?????
- $redis->lrange('fooList',0,1); //0~1?? ??? ???? ??? ?????. ? 2?? ??
- $redis->lrange('fooList',0, -1);//0?? ??? ? ?? ??? ???? ?? ?? ??? ?????? ?? ?????. redis?? ?? ??? ????, ??? ?????.
-
- //lindex? ?? ??? ?? ??? ?????. ??? ??? ??
- $redis->lindex('fooList',1); //'bar1'? ?????
-
- //lset? ???? ??? ??? ?? ?? ?????
- $ redis->lset('fooList',1,'123');//?? 1? ??? ???? true? ?????
-
- //lrem ???? ???? ??? ?? ??? ?????
- $redis->lrem('fooList',1,'_'); //?(?? ??)? ???? '_' ?? 1?? ?????(????? -1 ??).
-
- //lpop/rpop? ?? ??? ??? ?? ?? ?? ?? ??? ??? ?(? ??)???.
- $redis->lpop('fooList') //' bar0'
- $redis-> rpop('fooList'); //'bar2'
-
- //ltrim ??? ??, ???? ? ?? ?? ??, ??? ??
- $redis-> ,1); //???? 0???? ? ?? ?? ??
-
- //rpoplpush ? ???? ??? ??? ?? ???? ??
- $ redis->rpush('list1','ab0 ');
- $redis->rpush('list1','ab1');
- $redis->rpush('list2','ab2 ');
- $redis->rpush ('list2','ab3');
- $redis->rpoplpush('list1','list2');//?? list1 =>array('ab0'),list2 =>array('ab1','ab2','ab3')
- $redis->rpoplpush('list2','list2');//?? ??? ???? ???? ??? ??? ??? ?????. list2 =>array('ab3','ab1','ab2 ' )
-
- //linsert? ??? ??? ??? ?? ??? ?? ??? ?????.
- $redis->linsert('list2', 'before','ab1','123') ; / /'ab1' ?? ?? '123'? ???? ?????.
- $redis->linsert('list2', 'after','ab1','456') //'ab1' ?? ?? '? ???? ?????. 456'
-
- //blpop/brpop? ?? ?? ?? ?? ??? ???? ??? ?? ?? ?? ?? ?? ??? ??? ??????(? ??? PHP ???? ?? ????? ? ? ????)
- / / brpoplpush? ??? ???? ???? ??? rpoplpush
- $redis->blpop('list3',10); //list3? ?? ??? ? ?? ??? ?? ??? ?????. ?? ?? ????. 10? ? ?? ??
-
- /**
- ??? ?? ??
- */
-
- //sadd? ??? ????, true? ????, false? ????? ?????.
- $redis->sadd ('set1' ,'ab');
- $redis->sadd('set1','cd');
- $redis->sadd('set1','ef');
-
- //srem? ??? ??? ?????.
- $redis->srem('set1','cd') //'cd' ??? ?????.
-
- //spop? ? ?? ??
- $redis ->spop('set1');
-
- //smove? ?? ?? ???? ??? ??? ?? ?? ???? ?????
- $redis->sadd(' set2','123');
- $redis->smove('set1','set2','ab');//'set1'? 'ab'? 'set2'? ???? true ?? false? ?????.
-
- / /scard? ?? ?? ???? ?? ?? ?????.
- $redis->scard('set2');//2
-
- //sismember? ??? ???? ??? ?????. ?? ???? ??
- $redis-> sismember('set2','123'); //true ?? false
-
- //smembers? ?? ???? ?? ??? ???????
- $redis ->smembers('set2'); //array( '123','ab');
-
- //sinter/sunion/sdiff ? ???? ?? ??? ???/???/??? ?????.
- $redis->sadd('set1', 'ab');
- $redis->sinter('set2','set1') //?? ??('ab')
-
- //sinterstore/sunionstore/sdiffstore ? ??? ?? ???/???/?? ??? ? ?? ???? ??
- $redis->set('foo',0);
- $redis-> ;sinterstore('foo','set1') ; //'set1'? ??? 'foo'? ???? 'foo'? ?? ???? ???? ?? ????
- $redis->sinterstore('foo ',array('set1',' set2')); //'set1'? 'set2'? ??? ??? 'foo' ???? ???? 'foo'? ?? ??? ?????
-
- / /srandmember? ???? ?? ??? ?????.
- $redis->srandmember('set1');
-
- /**
- ??? ?? ??? ??
- */
-
- //sadd? ??? ???? ?? ??? ????, true? ????, ???? false? ?????.
- $redis->zadd('zset1',1,'ab');
- $redis->zadd('zset1',2,' cd');
- $redis-> zadd('zset1',3,'ef');
-
- //zincrby? ??? ??? ??? ?? ???? ??? ??? ?????. elements
- $redis->zincrby('zset1',10 ,'ab');//Return 11
-
- //zrem? ??? ??? ?????
- $redis->zrem(' zset1','ef'); //true ?? false
-
- //zrange? ???? ??? ??? ?? ??? ?? ??? ?????.
- $redis->zrange('zset1',0 ,1); //?? 0? 1(2) ??? ??? ?????.
- $redis->zrange('zset1',0,-1);//?? 0? ? ?? ?? ??? ??? ?????. ??? (?? ??? ??)
-
- //zrevrange ?? ??, ???? ??? ??? ?? ??? ???? ??
- $redis->zrevrange('zset1',0 ,-1); //?? ??? zrange? ?????
-
- //zrangebyscore /zrevrangebyscore? ???? ??? ??? ??? ?? ??? ??/?????? ?????
- $redis-> zadd('zset1',3,'ef');
- $redis->zadd('zset1',5 ,'gh');
- $redis->zrangebyscore('zset1',2, 9); //??? ? 2~9 ??? ?? array('ef','gh')
- ?? //???? ??
- $redis->zrangebyscore('zset1',2,9, 'withscores'); //??? ? 2-9 ??? ??? ???? ??? ?? ?????. array(array('ef' ,3),array('gh',5))
- $redis-> zrangebyscore('zset1',2,9,array('withscores' =>true,'limit'=>array(1 , 2))) //??? ? 2-9 ??? ?? ??, 'withscores ' =>true? ??? ?? ????? ?????. 'limit'=>array(1, 2)? ?? 2? ??? ????? ?????. ??? array(array('ef',3),array('gh'???. ,5))
-
- //zunionstore/zinterstore ?? ???? ???/???? ?? ???? ??
- $redis->zunionstore('zset3',array('zset1','zset2', 'zset0')); //'zset1','zset2','zset0'? ???? 'zset3'? ??
- //?? ????
- $redis->zunionstore('zset3',array( 'zset1','zset2'),array('weights' => array(5,0)));/ /weights ????? ???? ?????. ?, ??? ? 5?? ? ?? ?? ??? ??? ?????. ??, 0?? ? ??? ????? ??? ?????.
- $redis->zunionstore('zset3',array('zset1','zset2') ,array('aggregate' => 'max'));/ /'aggregate' => 'max' ?? 'min'? ??? ? ??? ??? ? ?? ???? ?? ?? ???? ?????.
-
- //zcount? ??? ??? ?? ?? ?????
- $redis->zcount('zset1',3,5);//2
- $redis->zcount('zset1',' (3',5)) //'(3'? ??? ?????. ??? ?? 3-5 ????? 3? ???? ????. ????? '(5'? ???? ??? 5??? 5? ???? ??? ??? ?? ????.
-
- //zcard? ??? ?????. elements
- $redis->zcard('zset1');//4
-
- //zscore? ??? ???? ?????
- $redis->zscore('zset1','ef');//3
-
- //zremrangebyscore? ??? ??? ??? ?????
- $redis->zremrangebyscore('zset1',0, 2); //0-2('ab', 'cd') ??? ???? ?? ??? ????, ??? ??? ?? ?????. 2
-
- //zrank/zrevrank? ???? ??/????? ?????. ??? ?? ?? ??(??? ??)
- $redis->zrank('zset1','ef');//? ?? ????? 0? ?????. zrevrank? 1(??? ??)? ?????. >
- //zremrangebyrank? ???? ??? ?? ??? ?? ??? ?????
- $redis->zremrangebyrank('zset1',0,10) //?? 0-10? ??? ???? ??? ?????. ??? ?? 2
-
- /**
- ?? ??? ??
- */
-
- //hset/hget ?? ??? ??? ???
- $redis->hset('hash1','key1', 'v1'); //'key1' ?? 'v1' ?? ?? ??? hash1 ???? ??
- $redis->hset('hash1','key2','v2');
- $redis ->hget('hash1','key1'); //'hash1' ????? 'key1' ?? ?? ???? 'v1'? ?????.
-
- //hexists? ??? ??? ?? ?????. table
- $redis->hexists ('hash1','key1'); //true ?? false
-
- //hdel ?? ????? ??? ?? ??? ?????
- $redis-> ;hdel('hash1','key2'); //true ?? false
-
- //hlen? ?? ??? ?? ?? ?????
- $redis->hlen('hash1 '); //1
-
- //hsetnx? ??? ????? ??? ?? ????.
- $redis->hsetnx('hash1','key1','v2') //false
- $redis-> hsetnx('hash1','key2','v2'); //true
-
- //hmset/hmget? ?? ???? ?? ?? ??? ??????
- $redis-> ;hmset('hash1' ,array('key3'=>'v3','key4'=>'v4'));
- $redis->hmget('hash1',array('key3' ,'key4')) ; //?? ?? ????? array('v3','v4')
-
- //hincrby? ??? ?? ?????
- $redis->hincrby('hash1', 'key5',3 ); //3? ??
- $redis->hincrby('hash1','key5',10); //13? ??
-
- //hkeys ?? ???
- $redis->hkeys('hash1'); //return array('key1','key2','key3','key4','key5')
-
- // hvals? ?? ???? ?????. ?? ?
- $redis->hvals('hash1') //return array('v1','v2','v3','v4',13)
-
- //hgetall ?? ?? ??? ??? ?????
- $redis->hgetall('hash1') //return array('key1'=>'v1','key2'=>'v2' ,'key3'=> ;'v3','key4'=>'v4','key5'=>13)
-
- /**
- ?? ??
- */
-
- / /sort sort
- $redis->rpush('tab',3);
- $redis->rpush('tab',2);
- $redis->rpush('tab' ,17);
- $redis->sort('tab'); //??(2,3,17) ??
- //???? ?? array('sort' => 'desc', 'limit'? ???? ??? ? ???? => array(1, 2))
- $redis->sort('tab',array('sort' => 'desc')); ????, return array(17,3, 2)
- $redis->sort('tab',array('limit' => array(1, 2))) //1? 2? ?? ?? ??? ??(??? 2? ??? ?? ??? ???)?? array(3,17)
- $redis->sort('tab',array('limit' => array('alpha' =)? ?????. > true))); // ? ?? ??? ???? ???? ??(17,2,3)? ?????. 17? ? ?? ??? '1'??? ? ?? ???
- $redis->sort???. ('tab',array('limit' => ; array('store' => 'ordered')))) //?? ??? ???? ?? ?? ?????
- $redis->sort(' tab',array('limit' => array ('get' => 'pre_*'))); //????? ?? '*'? ??? ????? ? ?????. ?, 'pre_? ???? ??? ??????. '? ?????.
-
- /**
- Redis ?? ??
- */
-
- //select? ??? ??????? ?????.
- $redis->select('mydb') //specify mydb, ???? ??? ??
-
- //flushdb ?? ????? ???
- $redis->flushdb();
-
- //move? ?? ?????? ??? ?????. ?? ??????
- $redis->set('foo', 'bar');
- $redis->move('foo', 'mydb2') //'mydb2' ?????? ???? ??
-
- //info? ??? ?? ??? ?????
- $redis->info( ; redis->slaveof(); //???? ?? ???
-
- //?? ??? ?? ????? ????
- $redis->save();
- //?? ???? ???? ?????? ??
- $redis->bgsave();
- //??
- $redis ->bgrewriteaof();
- //???? ????? ????? ??? ?????
- $redis-> lastsave();
-
- //?? ?-? ??/????
- $mkv = array(
- 'usr:0001' => '? ?? ???',
- 'usr: 0002' => '? ?? ???',
- 'usr:0003' => '? ?? ???'
- );
- $redis->mset($mkv); //?? ?? ?? ? ??
- $retval = $redis->mget(array_keys($mkv)); ?? ?? ???? ?
- print_r($retval);
-
- //?? ??
- $replies = $redis->pipeline(function($pipe) {
- $pipe-> ;ping();
- $pipe->flushdb();
- $pipe- >incrby('counter', 10) //?? ??
- $pipe->incrby('counter ', 30);
- $pipe->exists('counter');
- $pipe->get('counter');
- $pipe->mget('does_not_exist', ' ???');
- });
- print_r($replies);
-
- //CAS,???操?
-
- function zpop($client, $zsetKey) {
- $element = null;
- $options = array(
- 'cas' => true, // CAS ?? ???? ???
- 'watch' => $zsetKey, // ?? ??? ???? ?? ???? ?? ?
- 'retry' => 3, // ??? ????? ?? ??? ??,
- // ?????? ??? ??? ?.
- );
-
- $txReply = $client- >multiExec($options, function($tx)
- use ($zsetKey, &$element) {
- @list($element) = $tx->zrange($zsetKey, 0, 0);
- if (isset($element)) {
- $tx->multi(); // CAS? ???? MULTI? ????? ????? ???.
- $tx->zrem($zsetKey, $element);
- }
- });
- return $element;
- }
- $zpopped = zpop($redis, 'zset');
- echo isset($zpopped) ? "ZPOPed $zpopped": "ZPOP? ?? ?? ????!", "n";
-
- //對存取的key加前綴,如: 'nrk:'
- $redis->getProfile() ->setPreprocessor(new KeyPrefixPreprocessor('nrk:'));
-
- //分布式存儲? ?些??
- $multiple_servers = array(
- array(
- 'host' => ; '127.0.0.1',
- '??' => 6379,
- '??????' => 15,
- '??' => array(
- '???' => '127.0.0.1',
- '??' => 6380,
- '??????' => 15,
- '??' => '? ',
- ),
- );
-
- predistributionIDistributionStrategy ??;
-
- NaiveDistributionStrategy ???? IDistributionStrategy {
- private $_nodes, $_nodesCount;
-
- ?? ??? ?????. __constructor() {
- $this->_nodes = array();
- $this->_nodesCount = 0;
- }
-
- ?? ?? add($node, $weight = null) {
- $this->_nodes[] = $node;
- $this->_nodesCount ;
- }
-
- ?? ?? ??($node) {
- $ this->_nodes = array_filter($this->_nodes, function($n) use($node) {
- return $n !== $node;
- });
- $this- >_nodesCount = count($this->_nodes);
- }
-
- ?? ?? get($key) {
- $count = $this->_nodesCount;
- if ( $count === 0) {
- throw new RuntimeException('?? ??');
- }
- return $this->_nodes[$count > 1? abs(crc32($key) % $count) : 0];
- }
-
- ?? ?? generateKey($value) {
- return crc32($value);
- }
- }
-
- //配置鍵分布策略
- $options = array(
- 'key_distribution' => new NaiveDistributionStrategy(),
- );
-
- $redis = new PredisClient($multiple_servers, $options);
-
- for ($i = 0; $i set("key:$i", str_pad($i, 4, '0', 0));
- $redis->get("key:$i");
- }
-
- $server1 = $redis->getClientFor('first')->info();
- $server2 = $redis->getClientFor('second')->info();
-
- printf("?? '%s'?? %d?? ?? ?? ?? '%s'?? %d?? ?? ????. n",
- '? ??', $server1['db15']['keys'], '? ??', $server2['db15']['keys']
- );
-
-
???碼
|
PHP, ???, ????