微擎 Redis开启,高并发测试

1、宝塔安装

软件管理-PHP-设置-安装扩展-redis

2、微擎配置

文件:data/config.php

$config['setting']['cache'] = 'redis'; [35行左右] 缓存改为redis。

最下面加上

// -------------------------- CONFIG REDIS --------------------------- //

$config['setting']['redis']['server'] = '127.0.0.1';//如果redis服务器在别的机器,请填写机器的IP地址。

$config['setting']['redis']['port'] = 6379;

$config['setting']['redis']['pconnect'] = 0;

$config['setting']['redis']['timeout'] = 1;

$config['setting']['redis']['auth'] = 'iwonmo'; //微擎

$config['setting']['redis']['requirepass'] = 'iwonmo';//人人用这个

auth 配置需要到redis的配置文档里进行配置,在微擎里如上定义只是方便引用。

3、更新微擎缓存

微擎后台-系统-更新缓存

4、查看是否开启成功

微擎后台-站点-性能优化

5、测试代码

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

public function build_sn(){

$yCode = array``(``'A'``, 'B'``, 'C'``, 'D'``, 'E'``, 'F'``, 'G'``, 'H'``, 'I'``, 'J'``);

$orderSn = $yCode``[rand(0,9)] . strtoupper``(``dechex``(``date``(``'m'``))) . date``(``'d'``) . substr``(time(), -5) . substr``(microtime(), 2, 5) . sprintf(``'%02d'``, rand(0, 99));

return $orderSn``;

}

public function doPageRedis_setRedPack(){

global $_W``;

$redisConfig = $_W``[``"config"``][``'setting'``][``'redis'``];

$redis = new Redis();

$redis``->pconnect(``$redisConfig``[``'server'``], $redisConfig``[``'port'``],``$redisConfig``[``'timeout'``]);

// 随机一个红包Key

$key = $this``->build_sn();

for (``$i = 1; $i <= 10; $i``++)

{

$redis``->lpush(``$key``, $i``);

}

// 写入到数据库

$RedPack_data = array``(

'num' => 10,

);

$result = pdo_insert(``'redis_test'``, $RedPack_data``);

// 返回前端信息 - 抢红包

if (!``empty``(``$result``)) {

echo "id:"``.pdo_insertid().``" RadKey:"``.``$key``;

}``else``{

echo "RadPack:Error"``;

}

}

public function doPageRedis_test(){

global $_W``,``$_GPC``;

$redisConfig = $_W``[``"config"``][``'setting'``][``'redis'``];

$redis = new Redis();

$redis``->pconnect(``$redisConfig``[``'server'``], $redisConfig``[``'port'``],``$redisConfig``[``'timeout'``]);

// 返回列表的长度 key 被解释为一个空列表,返回 0

if``(``$redis``->llen(``$_GPC``[``'RadKey'``]) == 0) return $this``->result(``$errno = 0, $message = 'No Pack'``);

// 移除并返回列表的第一个元素

$List = $redis``->lpop(``$_GPC``[``'RadKey'``]);

if``(``intval``(``$List``) > 0){

$result = pdo_update(``'redis_test'``, array``(``'num' => $List``-1), array``(``'id' => $_GPC``[``'id'``]));

if (!``empty``(``$result``)) return $this``->result(``$errno = 0, $message = 'Get Ok:'``.``$List``); else return $this``->result(``$errno = 0, $message = 'Get Error:'``.``$List``);

}``else

return $this``->result(``$errno = 1, $message = 'Error:High incidence of complications'``,``'llen(RadKey) == 0'``);

}

生成红包:Redis_setRedPack

拆开红包:Redis_test

POST:id=[红包ID]&RadKey=[RdisListKey]

使用:访问Redis_setRedPack创建红包,会返回ID和RdisListKey。继而访问Redis_test拆开红包。

Buy me a cup of coffee :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值