摘要:是連接的操作庫,由于它完全使用編寫,大量使用命名空間以及閉包等功能,只支持以上版本,使用方便,但是性能一般,如果追求這塊的性能,可以改換語言編寫的擴(kuò)展后性能會(huì)大幅提升比如使用擴(kuò)展。
predis是php連接redis的操作庫,由于它完全使用php編寫,大量使用命名空間以及閉包等功能,只支持php5.3以上版本,使用方便,但是性能一般,如果追求這塊的性能,可以改換c語言編寫的php擴(kuò)展后性能會(huì)大幅提升(比如使用C擴(kuò)展phpredis?https://github.com/owlient/phpredis)。使用autoload加載相關(guān)庫,這邊重點(diǎn)就是為了require $file;
spl_autoload_register ( function ( $class ) { $file = __DIR__ . "/lib/Predis/" . $class . ".php" ; if ( file_exists ( $file ) ) { require $file ; return true ; } } ) ;配置連接的IP、端口、以及相應(yīng)的數(shù)據(jù)庫
$server = array ( "host" => "127.0.0.1" , "port" => 6379 , "database" => 15 ) ; $redis = new Client ( $server ) ;基礎(chǔ)操作
$redis -> set ( "library" , "predis" ) ;
$retval = $redis -> get ( "library" ) ;
echo $retval ; //顯示 "predis"
$redis -> setex ( "str" , 10 , "bar" ) ; //表示存儲(chǔ)有效期為10秒
$redis -> setnx ( "foo" , 12 ) ; //true
$redis -> setnx ( "foo" , 34 ) ; //false
$redis -> getset ( "foo" , 56 ) ; //返回34
$redis -> incr ( "foo" ) ; //foo為57
$redis -> incrby ( "foo" , 2 ) ; //foo為59
$redis -> exists ( "foo" ) ; //true
$redis -> del ( "foo" ) ; //true
$redis -> type ( "foo" ) ; //不存在,返回none
$redis -> set ( "str" , "test" ) ;
$redis -> type ( "str" ) ; //字符串,返回string
$redis -> append ( "str" , "_123" ) ; //返回累加后的字符串長(zhǎng)度8,此進(jìn)str為 "test_123"
$redis -> setrange ( "str" , 0 , "abc" ) ; //返回3,參數(shù)2為0時(shí)等同于set操作
$redis -> setrange ( "str" , 2 , "cd" ) ; //返回4,表示從第2個(gè)字符后替換,這時(shí)"str"為"abcd"
$redis -> substr ( "str" , 0 , 2 ) ; //表示從第0個(gè)起,取到第2個(gè)字符,共3個(gè),返回"abc"
$redis -> strlen ( "str" ) ; //返回4
$redis -> setbit ( "binary" , 31 , 1 ) ; //表示在第31位存入1,這邊可能會(huì)有大小端問題?不過沒關(guān)系,getbit 應(yīng)該不會(huì)有問題
$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 ) ; //設(shè)置有效期為1秒
$redis -> ttl ( "foo" ) ; //返回有效期值1s
$redis -> expire ( "foo" ) ; //取消expire行為
$redis -> dbsize ( ) ;
隊(duì)列操作$redis -> rpush ( "fooList" , "bar1" ) ; //返回一個(gè)列表的長(zhǎng)度1
$redis -> lpush ( "fooList" , "bar0" ) ; //返回一個(gè)列表的長(zhǎng)度2
$redis -> rpushx ( "fooList" , "bar2" ) ; //返回3,rpushx只對(duì)已存在的隊(duì)列做添加,否則返回0
$redis -> llen ( "fooList" ) ; //3
$redis -> lrange ( "fooList" , 0 , 1 ) ; //返回?cái)?shù)組包含第0個(gè)至第1個(gè)共2個(gè)元素
$redis -> lrange ( "fooList" , 0 ,- 1 ) ; //返回第0個(gè)至倒數(shù)第一個(gè),相當(dāng)于返回所有元素,注意redis中很多時(shí)候會(huì)用到負(fù)數(shù),下同
$redis -> lindex ( "fooList" , 1 ) ; //返回"bar1"
$redis -> lset ( "fooList" , 1 , "123" ) ; //修改位置1的元素,返回true
$redis -> lrem ( "fooList" , 1 , "_" ) ; //刪除隊(duì)列中左起(右起使用-1)1個(gè)字符"_"(若有)
$redis -> lpop ( "fooList" ) ; //"bar0"
$redis -> rpop ( "fooList" ) ; //"bar2"
$redis -> ltrim ( "fooList" , 0 , 1 ) ; //保留左邊起第0個(gè)至第1個(gè)元素
$redis -> rpush ( "list1" , "ab0" ) ;
$redis -> rpush ( "list1" , "ab1" ) ;
$redis -> rpush ( "list2" , "ab2" ) ;
$redis -> rpush ( "list2" , "ab3" ) ;
$redis -> rpoplpush ( "list1" , "list2" ) ; //結(jié)果list1 =>array("ab0"),list2 =>array("ab1","ab2","ab3")
$redis -> rpoplpush ( "list2" , "list2" ) ; //也適用于同一個(gè)隊(duì)列,把最后一個(gè)元素移到頭部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為空則一直等待,直到不為空時(shí)將第一元素彈出,10秒后超時(shí)set表操作
$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" ) ; //移動(dòng)"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"的內(nèi)容copy到"foo"中,并將"foo"轉(zhuǎn)為set表 $redis -> sinterstore ( "foo" , array ( "set1" , "set2" ) ) ; //將"set1"和"set2"中相同的元素copy到"foo"表中,覆蓋"foo"原有內(nèi)容
$redis -> srandmember ( "set1" ) ;
有序set表操作$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之間(兩個(gè))的元素
$redis -> zrange ( "zset1" , 0 ,- 1 ) ; //返回位置0和倒數(shù)第一個(gè)元素之間的元素(相當(dāng)于所有元素)
$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") //參數(shù)形式 $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條,結(jié)果為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" //其它參數(shù) $redis -> zunionstore ( "zset3" , array ( "zset1" , "zset2" ) , array ( "weights" => array ( 5 , 0 ) ) ) ; //weights參數(shù)表示權(quán)重,其中表示并集后值大于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"),返回刪除元素個(gè)數(shù)2
$redis -> zrank ( "zset1" , "ef" ) ; //返回0,因?yàn)樗堑谝粋€(gè)元素;zrevrank則返回1(最后一個(gè))
$redis -> zremrangebyrank ( "zset1" , 0 , 10 ) ; //刪除位置為0-10的元素,返回刪除的元素個(gè)數(shù)2
hash表操作$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" ) ) ; //返回相應(yīng)的值 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) //使用參數(shù),可組合使用 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個(gè)(這里的2是指?jìng)€(gè)數(shù),而不是位置),返回array(3,17) $redis -> sort ( "tab" , array ( "limit" => array ( "alpha" => true ) ) ) ; //按首字符排序返回array(17,2,3),因?yàn)?7的首字符是"1"所以排首位置 $redis -> sort ( "tab" , array ( "limit" => array ( "store" => "ordered" ) ) ) ; //表示永久性排序,返回元素個(gè)數(shù) $redis -> sort ( "tab" , array ( "limit" => array ( "get" => "pre_*" ) ) ) ; //使用了通配符"*"過濾元素,表示只返回以"pre_"開頭的元素redis管理操作
$redis -> select ( "mydb" ) ; //指定為mydb,不存在則創(chuàng)建
$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的服務(wù)器為從服務(wù)器
$redis -> slaveof ( ) ; //清除從服務(wù)器
$redis -> save ( ) ;
$redis -> bgsave ( ) ;
$redis -> bgrewriteaof ( ) ;
$redis -> lastsave ( ) ;
set/get多個(gè)key-value$mkv = array ( "usr:0001" => "First user" , "usr:0002" => "Second user" , "usr:0003" => "Third user" ) ; $redis -> mset ( $mkv ) ; //存儲(chǔ)多個(gè)key對(duì)應(yīng)的value $retval = $redis -> mget ( array_keys ( $mkv ) ) ; //獲取多個(gè)key對(duì)應(yīng)的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 ) ;CAS,事務(wù)性操作
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!" , " " ;對(duì)存取的key加前綴,如: "nrk:"
$redis -> getProfile ( ) -> setPreprocessor ( new KeyPrefixPreprocessor ( "nrk:" ) ) ;分布式存儲(chǔ)的一些方法
$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 PredisDistributionIDistributionStrategy ; 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 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 ( "Server "%s" has %d keys while server "%s" has %d keys. " , "first" , $server1 [ "db15" ] [ "keys" ] , "second" , $server2 [ "db15" ] [ "keys" ] ) ;
本文亦在微信公眾號(hào)【小道資訊】發(fā)布,歡迎掃碼關(guān)注!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/26214.html
摘要:即時(shí)交互的應(yīng)用在現(xiàn)代的應(yīng)用中很多場(chǎng)景都需要運(yùn)用到即時(shí)通訊,比如說最常見的支付回調(diào),與三方登錄。在生成事件時(shí),已經(jīng)自動(dòng)添加了該命名空間,該契約只約束方法。會(huì)將事件中的允許公開訪問的數(shù)據(jù)通過給定的頻道發(fā)布出去。 即時(shí)交互的應(yīng)用 在現(xiàn)代的 Web 應(yīng)用中很多場(chǎng)景都需要運(yùn)用到即時(shí)通訊,比如說最常見的支付回調(diào),與三方登錄。這些業(yè)務(wù)場(chǎng)景都基本需要遵循以下流程: 客戶端觸發(fā)相關(guān)業(yè)務(wù),并產(chǎn)生第三方應(yīng)...
摘要:我們可以配置多個(gè)連接來解決這個(gè)問題。默認(rèn)有個(gè)庫,在服務(wù)器中設(shè)置的值可以修改。先來看下的默認(rèn)連接默認(rèn)是有兩個(gè)連接的,分別是和。 導(dǎo)語 經(jīng)過編譯安裝和安裝擴(kuò)展之后,Redis 已經(jīng)可以正常使用了。但是在 laravel 中還需要其他的操作。 安裝擴(kuò)展 要想在 laravel 中使用 Redis,還需要安裝 predis 擴(kuò)展。使用 composer require predis/pred...
閱讀 3621·2021-11-24 10:25
閱讀 2546·2021-11-24 09:38
閱讀 1235·2021-09-08 10:41
閱讀 2919·2021-09-01 10:42
閱讀 2595·2021-07-25 21:37
閱讀 1995·2019-08-30 15:56
閱讀 926·2019-08-30 15:55
閱讀 2759·2019-08-30 15:54