Redis类:
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
|
<?php
namespace Utils;
use Phalcon\Config\Adapter\Ini as ConfigIni;
class Redis{
private static $redis1 ;
private static $session ;
/**
* 获取一个单例的redis对象
* @param string $name
* @return \Redis
*/
public static function getObj( $name = 'redis1' )
{
try {
if (! empty (self::$ $name )){
return self::$ $name ;
}
$config = new ConfigIni(APP_PATH. "/config" .ENV. "/redis.ini" );
self::$ $name = new \Redis();
self::$ $name ->connect( $config [ $name ][ 'host' ], $config [ $name ][ 'port' ],2);
if (isset( $config [ $name ][ 'password' ]) && ! empty ( $config [ $name ][ 'password' ])){
self::$ $name ->auth( $config [ $name ][ 'password' ]);
}
self::$ $name ->select( $config [ $name ][ 'database' ]);
} catch (\Exception $exception ){
self::$ $name = false;
}
return self::$ $name ;
}
};
|
定时任务:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/**
* 订单任务
*/
public function orderAction()
{
error_reporting (E_ALL & ~E_NOTICE);
$redis = Redis::getObj();
//获取数据库连接实例
$db = $this ->getDI()->getShared( 'db' );
while (true) {
print_r( ' -start- ' );
$order_status = 1;
file_put_contents (APP_PATH . "/../domain_order.log" , time());
try {
//防止长时间无任务导致MySQL超时
$db ->query( "select 1" );
//出列
$order_info = $redis ->lPop( 'order' );
if (! $order_info ) { //队列为空时暂停
echo ' -empty- ' ;
sleep(1);
continue ;
}
$order_info = json_decode( $order_info , true);
$model_order_info = NetUserOrder::findFirst([ 'order_sn = :order_sn:' , 'bind' =>[ 'order_sn' => $order_domain_info_save ->order_sn]]);
//未支付
if ( $model_order_info ->pay_status != 200){
echo 'no pay' ;
continue ;
}
//已操作
if ( $order_domain_info_save ->order_status == 3) {
echo ' -Operated- ' ;
continue ;
}
//事务开始
$db ->begin();
##
这里执行订单流程操作
##
$order_status = 3; //操作成功
//修改订单状态
$order_domain_info_save ->order_status = $order_status ;
$order_domain_info_save ->operation_time = time();
$order_domain_info_save ->update();
}
//提交事务
$db ->commit();
printf( '### succ order_id' . $order_info [ 'id' ] . ' ###' );
} catch (\Exception $e ) {
//回滚事务
$db ->rollback();
$order_status = 2; //操作失败
$order_domain_info_save ->order_status = $order_status ;
$order_domain_info_save ->operation_time = time();
$order_domain_info_save ->update();
printf( ' error ' . $e ->getMessage() . ' ' );
//异常,发送通知
Log::error( $e ->getMessage());
$redis ->hSet( 'order_domain_retry' , 'domain_retry_' . $order_info [ 'id' ], json_encode( $order_info ));
}
|
Redis常用队列方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//队列第一个 =>出列
$Redis ->lPop( $key );
//入到 =>队列最后
$Redis ->rPush( $key );
//队列最后一个 =>出列
$Redis ->rPop( $key );
//入到 =>队列第一个
$Redis ->rPop( $key );
//返回整个列表的值,不出列
$redis ->lRange( $key ,0,-1);
|
到此这篇关于PHP使用Redis队列执行定时任务实例讲解的文章就介绍到这了,更多相关PHP使用Redis队列执行定时任务内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_41049126/article/details/89355966