摘要:是连接的操作库,由于它完全使用编写,大量使用命名空间以及闭包等功能,只支持以上版本,使用方便,但是性能一般,如果追求这块的性能,可以改换语言编写的扩展后性能会大幅提升比如使用扩展。
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" ] ) ;
本文亦在微信公众号【小道资讯】发布,欢迎扫码关注!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/26214.html
摘要:即时交互的应用在现代的应用中很多场景都需要运用到即时通讯,比如说最常见的支付回调,与三方登录。在生成事件时,已经自动添加了该命名空间,该契约只约束方法。会将事件中的允许公开访问的数据通过给定的频道发布出去。 即时交互的应用 在现代的 Web 应用中很多场景都需要运用到即时通讯,比如说最常见的支付回调,与三方登录。这些业务场景都基本需要遵循以下流程: 客户端触发相关业务,并产生第三方应...
摘要:我们可以配置多个连接来解决这个问题。默认有个库,在服务器中设置的值可以修改。先来看下的默认连接默认是有两个连接的,分别是和。 导语 经过编译安装和安装扩展之后,Redis 已经可以正常使用了。但是在 laravel 中还需要其他的操作。 安装扩展 要想在 laravel 中使用 Redis,还需要安装 predis 扩展。使用 composer require predis/pred...
阅读 3622·2021-11-24 10:25
阅读 2548·2021-11-24 09:38
阅读 1237·2021-09-08 10:41
阅读 2919·2021-09-01 10:42
阅读 2600·2021-07-25 21:37
阅读 1996·2019-08-30 15:56
阅读 928·2019-08-30 15:55
阅读 2764·2019-08-30 15:54