摘要:是連接的操作庫,由于它完全使用編寫,大量使用命名空間以及閉包等功能,只支持以上版本,使用方便,但是性能一般,如果追求這塊的性能,可以改換語言編寫的擴展后性能會大幅提升比如使用擴展。
predis是php連接redis的操作庫,由于它完全使用php編寫,大量使用命名空間以及閉包等功能,只支持php5.3以上版本,使用方便,但是性能一般,如果追求這塊的性能,可以改換c語言編寫的php擴展后性能會大幅提升(比如使用C擴展phpredis?https://github.com/owlient/phpredis)。使用autoload加載相關庫,這邊重點就是為了require $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 , "database" => 15 ) ; $redis = new Client ( $server ) ;基礎操作
$redis -> set ( "library" , "predis" ) ;
$retval = $redis -> get ( "library" ) ;
echo $retval ; //顯示 "predis"
$redis -> setex ( "str" , 10 , "bar" ) ; //表示存儲有效期為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" ) ; //返回累加后的字符串長度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秒后超時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" ) ; //移動"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" ) ;
有序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之間(兩個)的元素
$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
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" ) ) ; //返回相應的值 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管理操作
$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 ( ) ;
set/get多個key-value$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 ) ;CAS,事務性操作
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!" , " " ;對存取的key加前綴,如: "nrk:"
$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 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" ] ) ;
本文亦在微信公眾號【小道資訊】發布,歡迎掃碼關注!
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/26214.html
摘要:即時交互的應用在現代的應用中很多場景都需要運用到即時通訊,比如說最常見的支付回調,與三方登錄。在生成事件時,已經自動添加了該命名空間,該契約只約束方法。會將事件中的允許公開訪問的數據通過給定的頻道發布出去。 即時交互的應用 在現代的 Web 應用中很多場景都需要運用到即時通訊,比如說最常見的支付回調,與三方登錄。這些業務場景都基本需要遵循以下流程: 客戶端觸發相關業務,并產生第三方應...
摘要:我們可以配置多個連接來解決這個問題。默認有個庫,在服務器中設置的值可以修改。先來看下的默認連接默認是有兩個連接的,分別是和。 導語 經過編譯安裝和安裝擴展之后,Redis 已經可以正常使用了。但是在 laravel 中還需要其他的操作。 安裝擴展 要想在 laravel 中使用 Redis,還需要安裝 predis 擴展。使用 composer require predis/pred...
閱讀 3603·2021-11-24 10:25
閱讀 2508·2021-11-24 09:38
閱讀 1217·2021-09-08 10:41
閱讀 2904·2021-09-01 10:42
閱讀 2569·2021-07-25 21:37
閱讀 1981·2019-08-30 15:56
閱讀 914·2019-08-30 15:55
閱讀 2750·2019-08-30 15:54