好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

redisphp案例 redis php实例

很多站长朋友们都不太清楚redisphp案例,今天小编就来给大家整理redisphp案例,希望对各位有所帮助,具体内容如下:

本文目录一览: 1、 php redis如何使用 2、 php使用redis的有序集合zset实现延迟队列 3、 订单超时,活动过期解决方案:php监听redis键重复触发引发事件 4、 如何用php+redis做订单到时间自动完成功能 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键重复触发引发事件

订单超时,活动过期解决方案:php监听redis键重复触发引发事件

Redis的2.8.0版本之后可用,键空间消息(Redis Keyspace Notifications),配合2.0.0版本之后的SUBSCRIBE 可以完成这个定时任务的操作了,定时的单位是秒。

1.我们先订阅频道称为 redisChat

2.现在,我们重新开启个redis客户端,然后在同一个频道redisChat发布消息,订阅者可以接收到消息。

接收到的消息如下:

3.Key过期事件的Redis配置

需要这里配置notify-keyspace-events的参数为“EX” .X代表了过期事件。notify-keyspace-events “Ex”保存配置后,重启Redis的服务,使配置生效。

PHP Redis实现订阅键空间通知

redis实例化类:

redis.class.php

1个

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18岁

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

//遇到类别重复的报错,所有叫Redis2

classRedis2  

{

    private$redis;

    publicfunction__construct($host= '127.0.0.1', $port= 6379)

    {

        $this->redis = newRedis();

        $this->redis->connect($host, $port);

    }

    publicfunctionsetex($key, $time, $val)

    {

        return$this->redis->setex($key, $time, $val);

    }

    publicfunctionset($key, $val)

    {

        return$this->redis->set($key, $val);

    }

    publicfunctionget($key)

    {

        return$this->redis->get($key);

    }

    publicfunctionexpire($key= null, $time= 0)

    {

        return$this->redis->expire($key, $time);

    }

    publicfunctionpsubscribe($patterns= array(), $callback)

    {

        $this->redis->psubscribe($patterns, $callback);

    }

    publicfunctionsetOption()

    {

        $this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);

    }

}

过期事件的订阅:

psubscribe.php

1个

2

3

4

5

6

7

8

9

10

11

12

13

14

15

require_once'./Redis.class.php';

$redis= new\Redis2();

// 解决Redis客户端订阅时候超时情况

$redis->setOption();

$redis->psubscribe(array('__keyevent@0__:expired'), 'keyCallback');

// 回调函数,这里写处理逻辑

functionkeyCallback($redis, $pattern, $chan, $msg)

{

    echo"Pattern: $pattern\n";

    echo"Channel: $chan\n";

    echo"Payl

    oad: $msg\n\n";

    //keyCallback为订阅事件后的回调函数,这里写业务处理逻辑,

    //比如前面提到的商品不支付自动撤单,这里就可以根据订单id,来实现自动撤单

}

设置过期事件:

index.php

1个

2

3

4

require_once'./Redis.class.php';

$redis= new\Redis2();

$order_id= 123;

$redis->setex('order_id',10,$order_id);

先用命令行模式执行 psubscribe.php

在浏览器访问 index.php

效果如下:

如何用php+redis做订单到时间自动完成功能

1、每分钟内要完成的订单id存到redis;

2、php做逻辑处理

3、配置crontab每分钟执行一次php,读取要完成的订单id;

关于redisphp案例的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于redisphp案例 redis php实例的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did197308
更新时间:2023-04-26   阅读:23次

上一篇: php继承封装多态 php是否支持多重继承

下一篇:php批量读取Excel php读取xlsx

相关资讯

最新资料更新

  • 1.php内存管理垃圾回收 php的垃圾回收机制是怎样的
  • 2.图片执行php代码 图片执行php代码是什么
  • 3.php手机端开发框架 php app开发框架
  • 4.php获取数组坐标 php获取数组的值
  • 5.php解析img PHP解析器
  • 6.php判断对象函数 php判断为空的方法有哪些
  • 7.php项目的更新 php版本升级对程序影响
  • 8.php引用vendor php 引用
  • 9.文章加载更多php php加载html
  • 10.数组对象转为数组php 对象数组转string
  • 11.iconv函数php iconv函数参数
  • 12.phphint插件下载 php 插件系统
  • 13.基于php在线聊天 php 在线聊天
  • 14.jq读取php变量 php读取html内容
  • 15.php文本转字节 php字符转换成数字
  • 16.mac安装php扩展 mac系统安装php环境
  • 17.php提示重复提交 php api防止重复提交
  • 18.php编程实验总结 php简单实训项目
  • 19.php区xml文件 php处理xml数据
  • 20.php判断08数字 php 判断数字

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]