废话不多说了,直接给大家贴代码了,具体代码如下所示:
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
|
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
/**
*
*/
class GoodsController extends Controller
{
public $enableCsrfValidation =false;
public function actionInfo()
{
$data =yii:: $app ->db->createCommand( "select * from goods " )->queryAll();
return $this ->render( 'index' ,[ 'data' => $data ]);
}
Public function actionXx()
{
$id =yii:: $app ->request->get( 'id' );
$data =yii:: $app ->db->createCommand( "select * from goods where id='$id'" )->queryOne();
return $this ->render( 'info' ,[ 'data' => $data ]);
}
Public function actionAdd()
{
$a =yii:: $app ->redis;
if (Yii:: $app ->request->isPost)
{
$data =yii:: $app ->request->post();
$res =yii:: $app ->db->createCommand()->insert( 'goods' , $data )->execute();
if ( $res )
{
for ( $i =0; $i < $data [ 'counts' ] ; $i ++)
{
$a ->lpush( 'goods' ,1);
}
return $this ->redirect( array ( 'goods/info' ));
}
else
{
echo "添加失败" ;
}
}
else
{
return $this ->render( 'add' );
}
}
public function actionOrder()
{
$redis =yii:: $app ->redis;
$count = $redis ->lpop( 'goods' );
if ( empty ( $count ))
{
echo "库存不足" ; die ;
}
$res =yii:: $app ->db->createCommand( "update goods set counts=counts-1 where id=1 and counts>0" )->execute();
if ( $res )
{
echo "秒杀成功" ;
}
else
{
echo "秒杀失败" ;
}
}
}
?>
|
总结
以上所述是小编给大家介绍的yii框架redis结合php实现秒杀效果(实例代码),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/qq_40794814/article/details/78352132