很多站长朋友们都不太清楚redisphp中使用,今天小编就来给大家整理redisphp中使用,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php怎样使用redis缓存数据 2、 php redis如何使用 3、 php使用redis的有序集合zset实现延迟队列 4、 php怎么使用 redis pub/sub php怎样使用redis缓存数据<?php
/**
* Redis缓存操作
* @author hxm
* @version 1.0
* @since 2015.05.04
*/
class RCache extends Object implements CacheFace
{
private $redis = null; //redis对象
private $sId = 1; //servier服务ID
private $con = null;//链接资源
/**
* 初始化Redis
*
* @return Object
*/
public function __construct()
{
if ( !class_exists('Redis') )
{
throw new QException('PHP extension does not exist: Redis');
}
$this->redis = new Redis();
}
/**
* 链接memcahce服务
*
* @access private
* @param string $key 关键字
* @param string $value 缓存内容
* @return array
*/
private function connect( $sid )
{
$file = $this->CacheFile();
require $file;
if(! isset($cache) )
{
throw new QException('缓存配置文件不存在'.$file);
}
$server = $cache[$this->cacheId];
$sid = isset($sid) == 0 ? $this->sId : $sid;//memcache服务选择
if ( ! $server[$sid])
{
throw new QException('当前操作的缓存服务器配置文件不存在');
}希望能帮到你,我还在后盾网学习呢,有不会的可以问我,一会有空回答你。(^ω^)
php redis如何使用开始在
PHP
中使用
Redis
前,要确保已经安装了
redis
服务及
PHP
redis
驱动,且你的机器上能正常使用
PHP。
PHP安装redis扩展
/usr/local/php/bin/phpize
#php安装后的路径
./configure
--with-php-config=/usr/local/php/bin/php-config
make
make
install
修改php.ini文件
vi
/usr/local/php/lib/php.ini
增加如下内容:
extension_dir
=
"/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so
安装完成后重启php-fpm
或
apache。查看phpinfo信息,就能看到redis扩展。
连接到
redis
服务
<?php
//连接本地的
Redis
服务
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//查看服务是否运行
echo
"Server
is
running:
"
.
$redis->ping();
?>
执行脚本,输出结果为:
Connection
to
server
sucessfully
Server
is
running:
PONG
Redis
PHP
String(字符串)
实例
<?php
//连接本地的
Redis
服务
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//设置
redis
字符串数据
$redis->set("tutorial-name",
"Redis
tutorial");
//
获取存储的数据并输出
echo
"Stored
string
in
redis::
"
.
jedis.get("tutorial-name");
?>
执行脚本,输出结果为:
Connection
to
server
sucessfully
Stored
string
in
redis::
Redis
tutorial
Redis
PHP
List(列表)
实例
<?php
//连接本地的
Redis
服务
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//存储数据到列表中
$redis->lpush("tutorial-list",
"Redis");
$redis->lpush("tutorial-list",
"Mongodb");
$redis->lpush("tutorial-list",
"Mysql");
//
获取存储的数据并输出
$arList
=
$redis->lrange("tutorial-list",
,5);
echo
"Stored
string
in
redis::
"
print_r($arList);
?>
执行脚本,输出结果为:
Connection
to
server
sucessfully
Stored
string
in
redis::
Redis
Mongodb
Mysql
Redis
PHP
Keys
实例
<?php
//连接本地的
Redis
服务
$redis
=
new
Redis();
$redis->connect('127.0.0.1',
6379);
echo
"Connection
to
server
sucessfully";
//
获取数据并输出
$arList
=
$redis->keys("*");
echo
"Stored
keys
in
redis::
"
print_r($arList);
?>
执行脚本,输出结果为:
Connection
to
server
sucessfully
Stored
string
in
redis::
tutorial-name
tutorial-list
php使用redis的有序集合zset实现延迟队列延迟队列就是个带延迟功能的消息队列,相对于普通队列,它可以在指定时间消费掉消息。
我们通过redis的有序集合zset来实现简单的延迟队列,将消息数据序列化,作为zset的value,把消息处理时间作为score,每次通过zRangeByScore获取一条消息进行处理。
然后,我们写一个php脚本,用来处理队列中的任务。
php怎么使用 redis pub/sub一.场景介绍
最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知。经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用成本也比较低。 :grin:
二.什么是redis pub/sub
资料查看
大家在看我的blog的同时可以打开redis官方对于redis pub/sub的介绍,感觉看英文文档吃力的话 :cry: ,可以看redis中文网的翻译介绍.
Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能
1> 基于事件的系统中,Pub/Sub是目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统所要求的松散耦合的交互模式:订阅者(如客户端)以事件订阅的方式表达出它有兴趣接收的一个事件或一类事件;发布者(如服务器)可将订阅者感兴趣的事件随时通知相关订阅者。
2> 消息发布者,即publish客户端,无需独占链接,你可以在publish消息的同时,使用同一个redis-client链接进行其他操作(例如:INCR等)
3> 消息订阅者,即subscribe客户端,需要独占链接,即进行subscribe期间,redis-client无法穿插其他操作,此时client以阻塞的方式等待“publish端”的消息;这一点很好理解,因此subscribe端需要使用单独的链接,甚至需要在额外的线程中使用。
三.redis pub/sub的使用
下面我将配着实图(用我的本地机器环境)来为大家讲解redis的pub/sub怎么去使用 .
没有安装phpredis扩展的或者没有redis服务的,请参考我的另一篇blog,有详细的安装介绍,这里不再赘述了。
1、启动redis服务端:
[caption id="attachment_1777" align="alignnone" width="300"] php redis pub/sub(消息订阅系统)经验总结之一[/caption]
2、新开一个终端,启动redis客户端,并做为subscribe客户端(消息订阅者),订阅一个名字叫test的频道的频道信息:
3、启动redis客户端,并做为publish客户端(消息发布者),发布一个名字叫test的频道,信息是:hello,world
4、再切换到2步骤中的redis客户端窗口,会发现,已经订阅到了刚才发布的 'hello,world'消息:
5、模式匹配订阅
Redis 的Pub/Sub实现支持模式匹配。客户端可以订阅全风格的模式以便接收所有来自能匹配到给定模式的频道的消息。
比如,将接收所有发到 test.name,test.phone,test.address...等等的消息,该这样写:
[shell]PUBSCRIBE test.*[/shell]
在终端回车后,同时再新的窗口里分别发布两个频道的消息,名字分别为:test.name和test.phone,然后切换到订阅端的窗口里,结果如下图所示:
由上图可以看出,在订阅了test.*频道后,一共收到了 test.name和test.phone两个频道的消息,这就是模式匹配订阅。
那么取消订阅匹配该模式的客户端也比较简单:
[shell]PUNSUBSCRIBE test.*[/shell]
6、好,以上的这些简单的demo,就是关于redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)的最基本使用。说了这么多,跟php也没有挂上什么钩,别着急,重要的都往往最后出场。 :lol:
四.php使用redis的pub/sub(发布/订阅的信息系统)
这里我列出一些常用的命令:
phpredis的安装
redis的客户端连接支持多种语言。这里我用的是php的phpredis,它是用c语言编写的,目前已经作为php的一个模块扩展,没有安装的可以参考我的另一篇blog,已经安装的可以忽略此步骤.
命令手册
这里我列出一些常用的:
[shell]Redis::__construct构造函数
$redis = new Redis();
connect, open 链接redis服务
参数
host: string,服务地址
port: int,端口号
timeout: float,链接时长 (可选, 默认为 0 ,不限链接时间)
注: 在redis.conf中也有时间,默认为300
pconnect, popen 不会主动关闭的链接
参考上面
setOption 设置redis模式
getOption 查看redis设置的模式
ping 查看连接状态
get 得到某个key的值(string值)
如果该key不存在,return false
set 写入key 和 value(string值)
如果写入成功,return ture
setex 带生存时间的写入值
$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.
setnx 判断是否重复的,写入值<br />$redis->setnx('key', 'value');
$redis->setnx('key', 'value');
delete 删除指定key的值
返回已经删除key的个数(长整数)
$redis->delete('key1', 'key2');
$redis->delete(array('key3', 'key4', 'key5'));
<span style="font-size: 16px; line-height: 24px;">
[/shell]
更详细的使用请参考这里,我就不写太多,因为我要直接摞代码了. :lol:
publish(消息发布端):pub.php
[php]
/**
* redis sub(消息订阅端)
* @ blog: phping.sinaapp.com
* @date 2016-04-24 15:00
*/
$redis = new Redis();
// 第一个参数为redis服务器的ip,第二个为端口
$res = $redis->connect('127.0.0.1', 6379);
// test为发布的频道名称,hello,world为发布的消息
$res = $redis->publish('test','hello,world');
[/php]
subscribe(消息订阅端): sub.php
[php]
/**
* redis sub(消息订阅端)
* @ blog: phping.sinaapp.com
* @date 2016-04-24 15:00
*/
$redis = new Redis();
$res = $redis->pconnect('127.0.0.1', 6379,0);
$redis->subscribe(array('test'), 'callback');
// 回调函数,这里写处理逻辑
function callback($instance, $channelName, $message) {
echo $channelName, "==>", $message,PHP_EOL;
}
[/php]
开始订阅redis消息
前面已经提到过,消息订阅者,即subscribe客户端,需要独占链接,即进行subscribe期间,redis-client无法穿插其他操作,此时client以阻塞的方式等待“publish端”的消息,所以我们用命令行来执行:
则 订阅消息的redis客户端已经启动,随时等待发布过来的消息并订阅该消息.
发布redis消息
同样,命令行执行消息发布端的脚本即可:
[shell]
php pub.php
[/shell]
切换到消息订阅端的窗口发现终端有输出,如下图:
哈哈,是不是 收到了发布端发布的'hello,world'这条消息呢。
五.总结
在命令执行redis订阅端脚本时,发现在终端会输出:
[shell]
PHPFatalerror: Uncaughtexception'RedisException'withmessage'readerroronconnection'in…
[/shell]
这样的错误是什么原因呢?请看我下一篇blog,本篇主要是讲一些基本的语法,在下一篇我会着重讲解一些在实际开发中遇到过的哪些问题和跳过的哪些坑。。。 :mrgreen:
码字不易,望转载注明出处哈 ;)
关于redisphp中使用的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于redisphp中使用 php开启redis的详细内容...