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 :)