predis是php鏈接redis的操做庫,因爲它徹底使用php編寫,大量使用命名空間以及閉包等功能,只支持php5.3以上版本,使用方便,可是性能通常,若是追求這塊的性能,能夠改換c語言編寫的php擴展後性能會大幅提高(好比使用C擴展phpredis https://github.com/owlient/phpredis)。
spl_autoload_register ( function ( $class ) { $file = __DIR__ . '/lib/Predis/' . $class . '.php' ; if ( file_exists ( $file ) ) { require $file ; return true ; } } ) ;
$server = array ( 'host' => '127.0.0.1' , 'port' => 6379 , 'database' => 15 ) ; $redis = new Client ( $server ) ;
$redis -> set ( 'library' , 'predis' ) ;
$retval = $redis -> get ( 'library' ) ;
echo $retval ; //顯示 'predis'php
$redis -> setex ( 'str' , 10 , 'bar' ) ; //表示存儲有效期爲10秒node
$redis -> setnx ( 'foo' , 12 ) ; //true
$redis -> setnx ( 'foo' , 34 ) ; //falsegit
$redis -> getset ( 'foo' , 56 ) ; //返回34github
$redis -> incr ( 'foo' ) ; //foo爲57
$redis -> incrby ( 'foo' , 2 ) ; //foo爲59redis
$redis -> exists ( 'foo' ) ; //true數據庫
$redis -> del ( 'foo' ) ; //true數組
$redis -> type ( 'foo' ) ; //不存在,返回none
$redis -> set ( 'str' , 'test' ) ;
$redis -> type ( 'str' ) ; //字符串,返回string服務器
$redis -> append ( 'str' , '_123' ) ; //返回累加後的字符串長度8,此進str爲 'test_123'微信
$redis -> setrange ( 'str' , 0 , 'abc' ) ; //返回3,參數2爲0時等同於set操做
$redis -> setrange ( 'str' , 2 , 'cd' ) ; //返回4,表示從第2個字符後替換,這時'str'爲'abcd'閉包
$redis -> substr ( 'str' , 0 , 2 ) ; //表示從第0個起,取到第2個字符,共3個,返回'abc'
$redis -> strlen ( 'str' ) ; //返回4
$redis -> setbit ( 'binary' , 31 , 1 ) ; //表示在第31位存入1,這邊可能會有大小端問題?不過不要緊,getbit 應該不會有問題
$redis -> getbit ( 'binary' , 31 ) ; //返回1
$redis -> set ( 'foo1' , 123 ) ;
$redis -> set ( 'foo2' , 456 ) ;
$redis -> keys ( 'foo*' ) ; //返回foo1和foo2的array
$redis -> keys ( 'f?o?' ) ; //同上
$redis -> randomkey ( ) ; //多是返回 'foo1'或者是'foo2'及其它任何一存在redis的key
$redis -> rename ( 'str' , 'str2' ) ; //把原先命名爲'str'的key改爲了'str2'
$redis -> expire ( 'foo' , 1 ) ; //設置有效期爲1秒
$redis -> ttl ( 'foo' ) ; //返回有效期值1s
$redis -> expire ( 'foo' ) ; //取消expire行爲
$redis -> dbsize ( ) ;
$redis -> rpush ( 'fooList' , 'bar1' ) ; //返回一個列表的長度1
$redis -> lpush ( 'fooList' , 'bar0' ) ; //返回一個列表的長度2
$redis -> rpushx ( 'fooList' , 'bar2' ) ; //返回3,rpushx只對已存在的隊列作添加,不然返回0
$redis -> llen ( 'fooList' ) ; //3
$redis -> lrange ( 'fooList' , 0 , 1 ) ; //返回數組包含第0個至第1個共2個元素
$redis -> lrange ( 'fooList' , 0 ,- 1 ) ; //返回第0個至倒數第一個,至關於返回全部元素,注意redis中不少時候會用到負數,下同
$redis -> lindex ( 'fooList' , 1 ) ; //返回'bar1'
$redis -> lset ( 'fooList' , 1 , '123' ) ; //修改位置1的元素,返回true
$redis -> lrem ( 'fooList' , 1 , '_' ) ; //刪除隊列中左起(右起使用-1)1個字符'_'(如有)
$redis -> lpop ( 'fooList' ) ; //'bar0'
$redis -> rpop ( 'fooList' ) ; //'bar2'
$redis -> ltrim ( 'fooList' , 0 , 1 ) ; //保留左邊起第0個至第1個元素
$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')
$redis -> linsert ( 'list2' , 'before' , 'ab1' , '123' ) ; //表示在元素'ab1'以前插入'123'
$redis -> linsert ( 'list2' , 'after' , 'ab1' , '456' ) ; //表示在元素'ab1'以後插入'456'
$redis -> blpop ( 'list3' , 10 ) ;
若是list3爲空則一直等待,直到不爲空時將第一元素彈出,10秒後超時
$redis -> sadd ( 'set1' , 'ab' ) ;
$redis -> sadd ( 'set1' , 'cd' ) ;
$redis -> sadd ( 'set1' , 'ef' ) ;
$redis -> srem ( 'set1' , 'cd' ) ; //刪除'cd'元素
$redis -> spop ( 'set1' ) ;
$redis -> sadd ( 'set2' , '123' ) ;
$redis -> smove ( 'set1' , 'set2' , 'ab' ) ; //移動'set1'中的'ab'到'set2',返回true or false
$redis -> scard ( 'set2' ) ; //2
$redis -> sismember ( 'set2' , '123' ) ; //true or false
$redis -> smembers ( 'set2' ) ; //array('123','ab');
$redis -> sadd ( 'set1' , 'ab' ) ;
$redis -> sinter ( 'set2' , 'set1' ) ; //返回array('ab')
$redis -> set ( 'foo' , 0 ) ; $redis -> sinterstore ( 'foo' , 'set1' ) ; //這邊等同於將'set1'的內容copy到'foo'中,並將'foo'轉爲set表 $redis -> sinterstore ( 'foo' , array ( 'set1' , 'set2' ) ) ; //將'set1'和'set2'中相同的元素copy到'foo'表中,覆蓋'foo'原有內容
$redis -> srandmember ( 'set1' ) ;
$redis -> zadd ( 'zset1' , 1 , 'ab' ) ;
$redis -> zadd ( 'zset1' , 2 , 'cd' ) ;
$redis -> zadd ( 'zset1' , 3 , 'ef' ) ;
$redis -> zincrby ( 'zset1' , 10 , 'ab' ) ; //返回11
$redis -> zrem ( 'zset1' , 'ef' ) ; //true or false
$redis -> zrange ( 'zset1' , 0 , 1 ) ; //返回位置0和1之間(兩個)的元素
$redis -> zrange ( 'zset1' , 0 ,- 1 ) ; //返回位置0和倒數第一個元素之間的元素(至關於全部元素)
$redis -> zrevrange ( 'zset1' , 0 ,- 1 ) ; //元素順序和zrange相反
$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)) $redis -> zrevrangebyscore ( 'zset1' , 9 , 2 , array ( 'withscores' => true , 'limit' => array ( offset , limit ) ) ) ;
$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'表示並集後相同的元素是取大值或是取小值
$redis -> zcount ( 'zset1' , 3 , 5 ) ; //2
$redis -> zcount ( 'zset1' , '(3' , 5 ) ) ; //'(3'表示索引值在3-5之間但不含3,同理也可使用'(5'表示上限爲5但不含5
$redis -> zcard ( 'zset1' ) ; //4
$redis -> zscore ( 'zset1' , 'ef' ) ; //3
$redis -> zremrangebyscore ( 'zset1' , 0 , 2 ) ; //刪除索引在0-2之間的元素('ab','cd'),返回刪除元素個數2
$redis -> zrank ( 'zset1' , 'ef' ) ; //返回0,由於它是第一個元素;zrevrank則返回1(最後一個)
$redis -> zremrangebyrank ( 'zset1' , 0 , 10 ) ; //刪除位置爲0-10的元素,返回刪除的元素個數2
$redis -> hset ( 'hash1' , 'key1' , 'v1' ) ; //將key爲'key1' value爲'v1'的元素存入hash1表
$redis -> hset ( 'hash1' , 'key2' , 'v2' ) ;
$redis -> hget ( 'hash1' , 'key1' ) ; //取出表'hash1'中的key 'key1'的值,返回'v1'
$redis -> hexists ( 'hash1' , 'key1' ) ; //true or false
$redis -> hdel ( 'hash1' , 'key2' ) ; //true or false
$redis -> hlen ( 'hash1' ) ; //1
$redis -> hsetnx ( 'hash1' , 'key1' , 'v2' ) ; //false
$redis -> hsetnx ( 'hash1' , 'key2' , 'v2' ) ; //true
$redis -> hmset ( 'hash1' , array ( 'key3' => 'v3' , 'key4' => 'v4' ) ) ;
$redis -> hmget ( 'hash1' , array ( 'key3' , 'key4' ) ) ; //返回相應的值 array('v3','v4')
$redis -> hincrby ( 'hash1' , 'key5' , 3 ) ; //返回3
$redis -> hincrby ( 'hash1' , 'key5' , 10 ) ; //返回13
$redis -> hkeys ( 'hash1' ) ; //返回array('key1','key2','key3','key4','key5')
//hvals 返回hash表中的全部value
$redis -> hvals ( 'hash1' ) ; //返回array('v1','v2','v3','v4',13)
$redis -> hgetall ( 'hash1' ) ; //返回array('key1'=>'v1','key2'=>'v2','key3'=>'v3','key4'=>'v4','key5'=>13)
$redis -> rpush ( 'tab' , 3 ) ; $redis -> rpush ( 'tab' , 2 ) ; $redis -> rpush ( 'tab' , 17 ) ; $redis -> sort ( 'tab' ) ; //返回array(2,3,17) //使用參數,可組合使用 array('sort' => 'desc','limit' => array(1, 2)) $redis -> sort ( 'tab' , array ( 'sort' => 'desc' ) ) ; //降序排列,返回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 ) ) ) ; //按首字符排序返回array(17,2,3),由於17的首字符是'1'因此排首位置 $redis -> sort ( 'tab' , array ( 'limit' => array ( 'store' => 'ordered' ) ) ) ; //表示永久性排序,返回元素個數 $redis -> sort ( 'tab' , array ( 'limit' => array ( 'get' => 'pre_*' ) ) ) ; //使用了通配符'*'過濾元素,表示只返回以'pre_'開頭的元素
$redis -> select ( 'mydb' ) ; //指定爲mydb,不存在則建立
$redis -> flushdb ( ) ;
$redis -> set ( 'foo' , 'bar' ) ;
$redis -> move ( 'foo' , 'mydb2' ) ; //若'mydb2'庫存在
$redis -> info ( ) ;
$redis -> slaveof ( '127.0.0.1' , 80 ) ; //配置127.0.0.1端口80的服務器爲從服務器
$redis -> slaveof ( ) ; //清除從服務器
$redis -> save ( ) ;
$redis -> bgsave ( ) ;
$redis -> bgrewriteaof ( ) ;
$redis -> lastsave ( ) ;
$mkv = array ( 'usr:0001' => 'First user' , 'usr:0002' => 'Second user' , 'usr:0003' => 'Third user' ) ; $redis -> mset ( $mkv ) ; //存儲多個key對應的value $retval = $redis -> mget ( array_keys ( $mkv ) ) ; //獲取多個key對應的value 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' , 'counter' ) ; } ) ; print_r ( $replies ) ;
function zpop ( $client , $zsetKey ) { $element = null ; $options = array ( 'cas' => true , // Initialize with support for CAS operations 'watch' => $zsetKey , // Key that needs to be WATCHed to detect changes 'retry' => 3 , // Number of retries on aborted transactions, after // which the client bails out with an exception. ) ; $txReply = $client -> multiExec ( $options , function ( $tx ) use ( $zsetKey , & $element ) { @ list ( $element ) = $tx -> zrange ( $zsetKey , 0 , 0 ) ; if ( isset ( $element ) ) { $tx -> multi ( ) ; // With CAS, MULTI *must* be explicitly invoked. $tx -> zrem ( $zsetKey , $element ) ; } } ) ; return $element ; } $zpopped = zpop ( $redis , 'zset' ) ; echo isset ( $zpopped ) ? "ZPOPed $zpopped" : "Nothing to ZPOP!" , "\n" ;
$redis -> getProfile ( ) -> setPreprocessor ( new KeyPrefixPreprocessor ( 'nrk:' ) ) ;
$multiple_servers = array ( array ( 'host' => '127.0.0.1' , 'port' => 6379 , 'database' => 15 , 'alias' => 'first' , ) , array ( 'host' => '127.0.0.1' , 'port' => 6380 , 'database' => 15 , 'alias' => 'second' , ) , ) ; use Predis\Distribution\IDistributionStrategy ; class NaiveDistributionStrategy implements IDistributionStrategy { private $_nodes , $_nodesCount ; public function __constructor ( ) { $this ->_nodes = array ( ) ; $this ->_nodesCount = 0 ; } public function add ( $node , $weight = null ) { $this ->_nodes [ ] = $node ; $this ->_nodesCount ++; } public function remove ( $node ) { $this ->_nodes = array_filter ( $this ->_nodes , function ( $n ) use ( $node ) { return $n !== $node ; } ) ; $this ->_nodesCount = count ( $this ->_nodes ) ; } public function get ( $key ) { $count = $this ->_nodesCount ; if ( $count === 0 ) { throw new RuntimeException ( 'No connections' ) ; } return $this ->_nodes [ $count > 1 ? abs ( crc32 ( $key ) % $count ) : 0 ] ; } public function generateKey ( $value ) { return crc32 ( $value ) ; } }
$options = array ( 'key_distribution' => new NaiveDistributionStrategy ( ) , ) ; $redis = new Predis\Client ( $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 ( "Server '%s' has %d keys while server '%s' has %d keys.\n" , 'first' , $server1 [ 'db15' ] [ 'keys' ] , 'second' , $server2 [ 'db15' ] [ 'keys' ] ) ;
本文亦在微信公衆號【小道資訊】發佈,歡迎掃碼關注!